android kotlin - EditText set max length programmatically

Setting Maximum Length for EditText in Android (Kotlin)

This code demonstrates two ways to set a maximum character limit for an EditText control in an Android application written in Kotlin.

Explanation:

The provided code includes three parts:

  1. MainActivity.kt: This is the main activity class that manages the UI and logic. It defines an EditText variable and retrieves its reference from the layout file (activity_main.xml). The onCreate method sets the maximum length of the EditText programmatically to 5 characters.

  2. Extension Function: The code defines an extension function for the EditText class named setMaxLength. This function takes an integer parameter representing the desired maximum length. It then creates an array of InputFilter with a single element of type InputFilter.LengthFilter and assigns the desired maxLength to it. Finally, it sets the filters property of the EditText to this newly created array.

  3. activity_main.xml: This is the layout file that defines the UI elements. It includes a single EditText with the ID "editText". This ID is used in the MainActivity.kt to retrieve the reference to the control.

Summary

By combining these approaches, you can achieve a flexible solution for limiting user input in your Android applications. The setMaxLength extension function provides a clean and reusable way to set the maximum length for any EditText instance. This approach is particularly useful when you need to dynamically set the limit based on specific conditions within your app.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.text.InputFilter
import android.widget.EditText


class MainActivity : Activity() {
    private lateinit var editText:EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        editText = findViewById(R.id.editText)

        // set edit text max length to 5 programmatically
        editText.setMaxLength(5)
    }
}


// extension function to set edit text maximum length
fun EditText.setMaxLength(maxLength: Int){
    filters = arrayOf<InputFilter>(
        InputFilter.LengthFilter(maxLength)
    )
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="32dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:inputType="text|textVisiblePassword"
        android:padding="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.12" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - EditText set max length programmatically
More android kotlin tutorials