Tuesday, September 1, 2020

How to EditText min length in Android Kotlin

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        // apply edit text filter minimum characters length

        editText.filterMinLength(6)

    }

}

// extension function to filter edit text minimum characters length

fun EditText.filterMinLength(min: Int){

    // check minimum length on focus change

    onFocusChangeListener = View.OnFocusChangeListener { view, b ->

        if (!b) { setLengthError(min) }

    }

    // check minimum length on keyboard done click

    setOnEditorActionListener { v, actionId, event ->

        if (actionId == EditorInfo.IME_ACTION_DONE) { setLengthError(min) }

        false

    }

    // check minimum length on enter key press

    setOnKeyListener { p0, p1, p2 ->

        if (p2.action == KeyEvent.ACTION_DOWN && p1 == KeyEvent.KEYCODE_ENTER) {

            setLengthError(min)

            true

        } else {

            false

        }

    }

}

// extension function to check edit text minimum length of characters

fun EditText.setLengthError(min: Int){

    error = try {

        val value = text.toString().trim()

        if (value.length < min){

            "minimum length $min characters."

        }else{

            // hide soft keyboard

            context.hideSoftKeyboard(this)


            Toast.makeText(context,"You submitted : $value",

                Toast.LENGTH_SHORT).show()

            null

        }

    }catch (e: Exception){

        "minimum length $min characters."

    }

}

// extension function to hide soft keyboard programmatically

fun Context.hideSoftKeyboard(editText: EditText){

    (getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager).apply {

        hideSoftInputFromWindow(editText.windowToken, 0)

    }

}

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"

    android:clickable="true"

    android:focusable="true"

    android:focusableInTouchMode="true"

    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:inputType="text|textVisiblePassword"

        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>

How to EditText min length in Android Kotlin

 

No comments:

Post a Comment