android kotlin - EditText allow only positive numbers

Android Kotlin - Allowing Positive Numbers and Decimals in EditText

This code demonstrates two ways to restrict user input in an EditText to only positive numbers and decimals in an Android application written in Kotlin.

Explanation:

  1. MainActivity.kt:

    • This file defines the main activity of the application.
    • It fetches the reference to an EditText widget (editText2) from the layout using findViewById.
    • The positiveNumbersOnly extension function is called on editText2 to configure it.
  2. positiveNumbersOnly Extension Function:

    • This function is designed to be used with any EditText object.
    • It sets two properties of the EditText:
      • inputType: This is set to InputType.TYPE_CLASS_NUMBER which instructs the system to display a number-only soft keyboard when the user taps on the EditText.
      • keyListener: This is set to a DigitsKeyListener instance configured to allow only digits from 0 to 9 and a decimal point (".") as valid input characters.
  3. activity_main.xml:

    • This file defines the layout of the activity.
    • It includes two EditText widgets:
      • editText: This EditText demonstrates setting the input restrictions directly in the layout file using the android:inputType and android:digits attributes. Both attributes achieve the same functionality as the positiveNumbersOnly extension function.
      • editText2: This EditText is configured programmatically in the code using the extension function.

Summary:

This code provides two options for restricting user input in an EditText to positive numbers and decimals. You can choose to configure the EditText directly in the layout or use the reusable positiveNumbersOnly extension function for a more programmatic approach.


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


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

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

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

        // allow only positive numbers and decimal numbers programmatically
        editText2.positiveNumbersOnly()
    }
}


// extension function to allow only positive numbers and decimal numbers
fun EditText.positiveNumbersOnly(){
    // numbers only soft keyboard
    inputType = InputType.TYPE_CLASS_NUMBER
    // specify the only accepted digits with point
    keyListener = DigitsKeyListener.getInstance("0123456789.")
}
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">

    <!-- allow only positive numbers and decimal numbers in xml -->
    <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:inputType="number"
        android:digits="0123456789."
        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" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="12dp"
        android:padding="12dp"
        android:textSize="30sp"
        android:fontFamily="sans-serif-condensed-medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - EditText allow only positive numbers
More android kotlin tutorials