Android Kotlin: EditText numbers only programmatically

Android Kotlin - Programmatically Restrict EditText Input to Numbers

This code demonstrates how to allow only numerical input (positive, negative, decimals) into an EditText programmatically in an Android Kotlin application.

The code consists of two parts:

  1. MainActivity.kt: This file contains the main activity class responsible for handling user interaction and setting up the UI.
  2. EditText Extension Function: This code defines an extension function that can be used on any EditText instance to restrict its input to numbers.

Breakdown of MainActivity.kt

  • The MainActivity class inherits from the Activity class, which is the base class for all activities in Android.
  • Inside onCreate, the code first inflates the layout file (activity_main.xml) using setContentView.
  • It then retrieves a reference to the EditText view using findViewById and stores it in the editText variable.
  • Finally, the inputNumbersOnly function is called on the editText object to restrict its input to numbers.

Breakdown of EditText Extension Function

  • This extension function is defined to be used with any EditText object.
  • It sets the inputType property of the EditText to a combination of flags:
    • InputType.TYPE_CLASS_NUMBER: This allows the input of numbers.
    • InputType.TYPE_NUMBER_FLAG_DECIMAL: This allows decimal numbers.
    • InputType.TYPE_NUMBER_FLAG_SIGNED: This allows both positive and negative numbers.

Summary

By combining the functionality of MainActivity.kt and the extension function, this code snippet ensures that the user can only enter numbers (positive, negative, and decimals) into the designated EditText field. This improves the user experience by guiding the user towards valid input and preventing unexpected errors during data entry.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.text.InputType
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)

        // allow positive and negative decimal or integer numbers only
        editText.inputNumbersOnly()
    }
}


// extension function to set edit text multiple input types
// allow to input positive and negative decimal and integer numbers
fun EditText.inputNumbersOnly(){
    inputType =
        InputType.TYPE_CLASS_NUMBER or // allow numbers
                // allow decimal numbers
                InputType.TYPE_NUMBER_FLAG_DECIMAL or
                // allow positive and negative numbers
                InputType.TYPE_NUMBER_FLAG_SIGNED
}
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"
        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 numbers only programmatically
More android kotlin tutorials