android kotlin - EditText space validation

The code defines an EditText widget that restricts the user from entering a space character at the beginning of the text. It achieves this by using a custom ignoreFirstWhiteSpace extension function.

Here's a breakdown of the code:

  1. MainActivity.kt:

    • This file defines the main Activity class for the Android application.
    • The onCreate method is called when the activity is first created.
    • Inside the onCreate method, the code:
      • Gets a reference to the EditText widget from the activity's layout using findViewById.
      • Calls the ignoreFirstWhiteSpace extension function on the EditText to prevent the user from entering a space at the beginning of the text.
  2. ignoreFirstWhiteSpace extension function:

    • This extension function is defined on the EditText class.
    • It takes an InputFilter lambda as an argument.
    • The InputFilter lambda is used to filter out any space characters that are entered at the beginning of the text.
    • The filter iterates over the characters being entered and checks if any of them are whitespace characters.
    • If a whitespace character is found at the beginning of the text (i.e., dstart is 0), the filter returns an empty string, effectively preventing the space character from being entered.
  3. activity_main.xml:

    • This file defines the layout for the activity.
    • It contains an EditText widget with the ID editText.

In summary, this code snippet demonstrates how to use a custom InputFilter to restrict the user from entering a space character at the beginning of an EditText widget in an Android Kotlin application.


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 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)

        // prevent user to enter first space on edit text
        // user can input space after entering any other character
        editText.ignoreFirstWhiteSpace()
    }
}


// ignore entering first space on edit text
// extension function to prevent input first space on edit text
fun EditText.ignoreFirstWhiteSpace() {
    val filter = InputFilter { source, start, end, dest, dstart, dend ->
        for (i in start until end) {
            if (Character.isWhitespace(source[i])) {
                if (dstart == 0) return@InputFilter ""
            }
        }
        null
    }

    filters = arrayOf(filter)
}
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"
    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:inputType="text|textVisiblePassword"
        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" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - EditText space validation
More android kotlin tutorials