Skip to main content

Posts

Android Kotlin: EditText input filter decimal

This code demonstrates how to create a custom input filter for an EditText in an Android application written in Kotlin. This filter restricts the user's input to decimal numbers with a specific format. The code is divided into three parts: MainActivity.kt: This file defines the main activity of the application. It fetches references to the EditText and TextView widgets from the layout and applies the inputFilterDecimal extension function to the EditText. It also displays information about the maximum digits and decimal places allowed. inputFilterDecimal extension function: This function is an extension for the EditText class. It takes two arguments: maxDigitsIncludingPoint and maxDecimalPlaces . It sets an array of filters to the EditText, where the only element is a DecimalDigitsInputFilter object instantiated with the provided arguments. In case of a PatternSyntaxException , the function disables the EditText and sets its hint to the exception message. DecimalD...

android kotlin - EditText set max length programmatically

Setting Maximum Length for EditText in Android (Kotlin) This code demonstrates two ways to set a maximum character limit for an EditText control in an Android application written in Kotlin. Explanation: The provided code includes three parts: MainActivity.kt: This is the main activity class that manages the UI and logic. It defines an EditText variable and retrieves its reference from the layout file ( activity_main.xml ). The onCreate method sets the maximum length of the EditText programmatically to 5 characters. Extension Function: The code defines an extension function for the EditText class named setMaxLength . This function takes an integer parameter representing the desired maximum length. It then creates an array of InputFilter with a single element of type InputFilter.LengthFilter and assigns the desired maxLength to it. Finally, it sets the filters property of the EditText to this newly created array. activity_main.xml: This is the layout file that defin...