How to Set Minimum and Maximum Input Value in EditText in Android?
In Android, EditText is a subclass of TextView which is configured to be editable. EditText is used for giving textual input such as characters, strings, numbers, etc. It has no limitation on the type of input unless explicitly attributed. Meaning we can attribute the EditText to accept only a string or a number. On top of this, we can create methods to accept only a certain type of value to facilitate the desired task.
In this article, we will show you how you could set a minimum and a maximum input value in the EditText.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add EditText with inputType as a number in the layout.
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. Creating an inner class to set minimum and maximum input values to the EditText.
You can see that if we try to give an input between 1-100, we are able to see it typed in the EditText. However, if we try to give 0 or something above 100, the input is not accepted and therefore not seen displayed or typed in the EditText.