Skip to main content

Posts

Showing posts from August, 2020

Android Kotlin: How to set EditText min length

Android Kotlin - Enforcing Minimum Characters in Edit Text This code demonstrates how to restrict the minimum number of characters allowed for user input in an EditText field within an Android application written in Kotlin. It achieves this functionality by creating a custom extension function for the EditText class. The code is divided into three parts: MainActivity.kt: This file defines the main activity class responsible for handling the user interface and logic. It retrieves the EditText reference from the layout and applies the filterMinLength extension function with the desired minimum character length. Extension Functions: The code defines three extension functions: filterMinLength(min: Int) : This function attaches three listeners to the EditText: onFocusChangeListener : Checks the text length when the EditText loses focus. setOnEditorActionListener : Checks the text length when the user presses the "Done" button on the keyboard. setOnKeyListener : Check...

android kotlin - EditText limit number range

Limiting Number Range Input in an EditText (Android Kotlin) This code demonstrates a way to restrict the number range a user can enter within an EditText in an Android application written in Kotlin. It allows for a user-defined minimum and maximum value. The code is divided into separate functionalities: Setting the Range: The inputFilterNumberRange extension function simplifies setting the allowed range for the EditText. It utilizes two other extension functions: filterMin and InputFilterMax . Enforcing Minimum Value: The filterMin function ensures that the user-entered value doesn't fall below the minimum limit. It achieves this by: Setting an onFocusChangeListener that checks the input on focus loss. Setting an onEditorActionListener that checks the input on pressing "Done" on the keyboard. If the input is less than the minimum, it sets the text to the minimum value and changes the underline color to red for visual feedback. Enforcing Maximum Value: ...