android kotlin - Set EditText digits programmatically

Android Kotlin: Programmatically Setting Allowed Digits in EditText

This code demonstrates how to restrict user input in an EditText field using Kotlin in an Android application. It allows users to enter only specific digits and a decimal point.

Breakdown:

  1. Import Statements: The code imports necessary classes like Activity, Bundle, InputType, DigitsKeyListener, EditText, and TextView.

  2. MainActivity Class:

    • Declares variables editText and textView to hold references to the corresponding widgets in the layout.
    • In onCreate():
      • Sets the layout using setContentView().
      • Finds the EditText and TextView by their IDs using findViewById().
      • Sets allowed digits for the EditText:
        • inputType property is set to InputType.TYPE_CLASS_NUMBER to indicate numerical input.
        • DigitsKeyListener instance is created with the allowed characters ("012345.") and assigned to the keyListener property. This restricts input to these characters.
  3. activity_main.xml:

    • Defines the layout using a ConstraintLayout.
    • An EditText is defined with desired attributes like size, padding, and text size.
    • A TextView displays instructions below the EditText, informing the user about the allowed characters.

Summary

This code snippet showcases a practical approach to control user input in an Android app using Kotlin. By combining InputType and DigitsKeyListener, the developer ensures that only specific characters can be entered in the EditText field, enhancing data validation and user experience.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.text.InputType
import android.text.method.DigitsKeyListener
import android.widget.EditText
import android.widget.TextView


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

    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)
        textView = findViewById(R.id.textView)

        // set edit text digits programmatically
        editText.inputType = InputType.TYPE_CLASS_NUMBER
        editText.keyListener = DigitsKeyListener.getInstance("012345.")
    }
}
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:padding="12dp"
        android:textSize="30sp"
        android:fontFamily="sans-serif-condensed-medium"
        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" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:fontFamily="sans-serif-condensed-medium"
        android:gravity="center"
        android:padding="8dp"
        android:textColor="#483D8B"
        android:textSize="26sp"
        android:text="Allowed only '012345.'"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - Set EditText digits programmatically
More android kotlin tutorials