Android: Set Min & Max Value An EditText Accepts

Forcing an EditText to accept a number value from a certain range entails the use of filters using the InputFilter class. I created a custom made class that lets you set an EditText to accept only values between a minimum and maximum value.

Here is the class.

To use the class, do it like this which sets an EditText to accept any number between 1 and 100.

There! Now you should be able to have your EditText accept only your desired values. Just be sure you set your EditText widget to accept only numbers.

Related Posts Plugin for WordPress, Blogger...

24 comments

  1. Nice one, just what I need.

    If you add the following at the beginning of the try {} you can even enter negative numbers. 😉

    String result = dest.toString() + source.toString();
    if (result.equals(“-“)) {
    return null;
    }

  2. Code is a bit buggy, substitute this:

    String startString = dest.toString().substring(0, dstart);
    String insert = source.toString();
    String endString = dest.toString().substring(dend);
    String parseThis = startString+insert+endString;
    int input = int.parseInteger(parseThis);

    for

    int input = Integer.parseInt(dest.toString() + source.toString());

  3. ^^^^^^^^^Scratch that, for real this time…

    Code is a bit buggy, substitute this:
    String startString = dest.toString().substring(0, dstart);
    String insert = source.toString();
    String endString = dest.toString().substring(dend);
    String parseThis = startString+insert+endString;
    int input = Integer.parseInteger(parseThis);
    for
    int input = Integer.parseInt(dest.toString() + source.toString());

    The original code only worked if new characters were inserted at the end of the field. This will work no matter where they are put.

    1. Am getting an error in the InputFilter. it says cannot resolve symbol InputFilter.
      can you please help me out with this.?

  4. The post is quite old, but I have a question. Why this stops working if I use a large interval (in my example 100-10000)?

  5. No, I get no exception or error, I just can’t input any value. I’ve made some tries: the problem is not the large range but big values themselves. For example, a range of (1000-1001) doesn’t work.

  6. I think I understand, the problem is the minimum, I can’t insert any value because the minimum is higher than 9, true?

  7. See my answer here: http://stackoverflow.com/a/13812853/910718

    This implementation has bugs, and is fundamentally flawed in the general case. You can’t check what the user has input BEFORE they have finished inputting it, this is why the minimum value check has issues when min > 1. You need to allow the user tell you they are done inputting their number before you can tell them their number is invalid because it is too small. I use changing focus to indicate that the user has finished inputting their number.

    Maximum value check works with this set up because numbers increase monotonically with each additional digit. Therefore, even before the user has finished entering their number, you can tell them you know it is already too large to be valid.

  8. This code snippet is amazing. Helped me a lot, thanks.

    But, can you also provide a code that doesn’t affects the “Decimal TextEdits” ?
    As this one prevents the user from entering numbers with decimal places, only integer can be entered!

  9. @alaaelrifaie:disqus sorry, got no time for this. what you can do though is change the isInRange to float parameters, and parse in the filter class, parse them as Float

      1. i tried but min value validation is not working, its takes even less then 20 values also.
        only max works fine.

Leave a Reply

Your email address will not be published. Required fields are marked *