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:
Import Statements: The code imports necessary classes like
Activity
,Bundle
,InputType
,DigitsKeyListener
,EditText
, andTextView
.MainActivity Class:
- Declares variables
editText
andtextView
to hold references to the corresponding widgets in the layout. - In
onCreate()
:- Sets the layout using
setContentView()
. - Finds the
EditText
andTextView
by their IDs usingfindViewById()
. - Sets allowed digits for the
EditText
:inputType
property is set toInputType.TYPE_CLASS_NUMBER
to indicate numerical input.DigitsKeyListener
instance is created with the allowed characters ("012345.") and assigned to thekeyListener
property. This restricts input to these characters.
- Sets the layout using
- Declares variables
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 theEditText
, informing the user about the allowed characters.
- Defines the layout using a
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 - Show soft keyboard when EditText is focused
- android kotlin - EditText hide error
- android kotlin - EditText allow only certain characters
- android kotlin - EditText allow only positive numbers
- android kotlin - Enable disable bluetooth programmatically
- android kotlin - Change screen orientation programmatically
- android kotlin - Change orientation without restarting activity
- android kotlin - RecyclerView add remove item
- android kotlin - RecyclerView GridLayoutManager example
- android kotlin - RecyclerView StaggeredGridLayoutManager example
- android kotlin - RadioGroup onCheckedChange listener
- android kotlin - Get RadioGroup selected item
- android kotlin - RadioButton onClick event