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:
MainActivity.kt: This is the main activity class that manages the UI and logic. It defines an
EditTextvariable and retrieves its reference from the layout file (activity_main.xml). TheonCreatemethod sets the maximum length of the EditText programmatically to 5 characters.Extension Function: The code defines an extension function for the
EditTextclass namedsetMaxLength. This function takes an integer parameter representing the desired maximum length. It then creates an array ofInputFilterwith a single element of typeInputFilter.LengthFilterand assigns the desiredmaxLengthto it. Finally, it sets thefiltersproperty of theEditTextto this newly created array.activity_main.xml: This is the layout file that defines the UI elements. It includes a single
EditTextwith the ID "editText". This ID is used in theMainActivity.ktto 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.
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)
    )
}
<?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 limit number range
 - android kotlin - EditText input filter decimal
 - android kotlin - EditText rounded corners programmatically
 - android kotlin - EditText border color programmatically
 - android kotlin - ImageView rounded corners programmatically
 - android kotlin - ImageView set image from drawable
 - android kotlin - ImageView set image from url
 - android kotlin - Get battery percentage programmatically
 - android kotlin - Get battery level programmatically
 - android kotlin - Get battery voltage programmatically
 - android kotlin - Get battery status programmatically
 - android kotlin - On back button pressed example
 - android kotlin - Get string resource by name
 - android kotlin - Get API level programmatically
 - android kotlin - Play default ringtone programmatically