android kotlin - EditText allow only characters

This code demonstrates how to restrict user input in an EditText to only characters and whitespace using Kotlin in an Android application.

The code defines an InputFilter that checks each character entered into the EditText. If the character is a letter or a space, it is allowed. Otherwise, it is filtered out.

Here's a breakdown of the code:

  1. MainActivity.kt

    • This class defines the main activity of the application.
    • In the onCreate method, it retrieves a reference to the EditText widget from the layout using findViewById.
    • It then creates an InputFilter instance and assigns it to the filters property of the EditText. This InputFilter is responsible for checking the characters entered into the EditText.
  2. InputFilter

    • The InputFilter is a class that is used to filter text input in Android.
    • The filter method of the InputFilter is called whenever a character is entered into the EditText.
    • The filter method takes six arguments:
      • charSequence: The text that was entered.
      • start: The starting index of the text that was entered.
      • end: The ending index of the text that was entered.
      • spanned: The current text in the EditText.
      • source: The source of the text that was entered.
      • flags: Flags associated with the event.
    • In this case, the filter method checks if the entered character is a space or a letter. If it is, it returns the character sequence. Otherwise, it returns an empty string, which effectively filters out the character.
  3. activity_main.xml

    • This file defines the layout of the activity.
    • It contains two EditText widgets. The first EditText can accept any type of text, while the second EditText is restricted to only characters and whitespace using the android:digits property. However, this approach is less flexible than using an InputFilter because it only allows you to specify a predefined set of characters.

In summary, this code provides a way to ensure that users can only enter characters and whitespace into a specific EditText widget in an Android application. This can be useful for situations where you want to collect data that only contains letters and spaces.


MainActivity.kt

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 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 characters/alphabets/letters with white space
        // programmatically accepts both uppercase and lowercase letters
        editText2.filters = arrayOf(filter)
    }
}


// input filter to allow only characters and whitespace
val filter = InputFilter { charSequence, i, i2, spanned, i3, i4 ->
    if (charSequence == "") { // for backspace
        return@InputFilter charSequence
    }
    if (charSequence.toString().matches("[a-zA-Z ]+".toRegex())) {
        charSequence
    } else ""
}
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 characters and whitespace 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:digits=
            "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:inputType="text|textVisiblePassword"
        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:inputType="text|textVisiblePassword"
        android:padding="12dp"
        android:textSize="30sp"
        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 characters
More android kotlin tutorials