How to EditText min length in Android Kotlin

How to EditText min length in Android Kotlin - Hello friend inabnomaniiyaha, In the article that you read this time with the title How to EditText min length in Android Kotlin, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel kotlin,what we write you can understand. Alright, happy reading.

Judul : How to EditText min length in Android Kotlin
link : How to EditText min length in Android Kotlin

Baca juga


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

 



That's the article How to EditText min length in Android Kotlin

That's it for the article How to EditText min length in Android Kotlin this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the article How to EditText min length in Android Kotlin with link addresshttps://inabnonapudyawanabing.blogspot.com/2020/09/how-to-edittext-min-length-in-android.html

0 Response to " How to EditText min length in Android Kotlin"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...