Friday, September 4, 2020

android kotlin - EditText allow only certain characters

MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import android.text.InputFilter
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


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

        // allow certain characters only in edit text
        val accepted = "abcABC012$*#"
        editText.certainCharactersOnly(accepted)

        // show accepted characters in text view
        textView.text = "Accepted only '$accepted'"
    }
}


// extension function to allow certain characters only in edit text
fun EditText.certainCharactersOnly(accepted:String){
    val filter = InputFilter { source, start, end, dest, dstart, dend ->
        var includesInvalidCharacter = false
        val stringBuilder = StringBuilder()
        val destLength = dend - dstart + 1
        val adjustStart = source.length - destLength
        for (i in start until end) {
            val sourceChar = source[i]
            if (
                accepted.contains(sourceChar)
            ) {
                if (i >= adjustStart) stringBuilder.append(sourceChar)
            } else includesInvalidCharacter = true
        }
        if (includesInvalidCharacter) stringBuilder else 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FEFEFA"
    tools:context=".MainActivity">

    <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"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>