Open In App

How to Set Minimum and Maximum Input Value in EditText in Android?

Last Updated : 11 Aug, 2021
Like Article

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.


<?xml version="1.0" encoding="utf-8"?>
    <!-- We will enter a number and see if it gets accepted -->
        android:hint="Enter number between 1-100"
        android:inputType="number" />

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.


import android.os.Bundle
import android.text.InputFilter
import android.text.Spanned
import android.widget.EditText
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and Initializing  edit text from the layout
        val editText = findViewById<EditText>(
        // Assigning filters
        editText.filters = arrayOf<InputFilter>(MinMaxFilter(1, 100))
    // Custom class to define min and max for the edit text
    inner class MinMaxFilter() : InputFilter {
        private var intMin: Int = 0
        private var intMax: Int = 0
        // Initialized
        constructor(minValue: Int, maxValue: Int) : this() {
            this.intMin = minValue
            this.intMax = maxValue
        override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dStart: Int, dEnd: Int): CharSequence? {
            try {
                val input = Integer.parseInt(dest.toString() + source.toString())
                if (isInRange(intMin, intMax, input)) {
                    return null
            } catch (e: NumberFormatException) {
            return ""
        // Check if input c is in between min a and max b and
        // returns corresponding boolean
        private fun isInRange(a: Int, b: Int, c: Int): Boolean {
            return if (b > a) c in a..b else c in b..a


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.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads