I want to define a min and max value for an EditText
.
For example: if any person tries to enter a month value in it, the value must be between 1-12.
I can do it by using TextWatcher
but I want to know if there is any other way to do it in layout file or elsewhere.
Edit:
I don't want to limit character count. I want to limit the value. For example, if I limit month EditText
w characters when I enter 12 it will accept it but if I enter 22 it mustn't accept it while I am entering.
If you need range with negative numbers like -90:90, you can use this solution.
Extension of Pratik's and Zac's answer. Zac fixed a small bug of Pratik's in his answer. But I notcied that code doesn't support negative values, it will throw a NumberFormatException. To fix that, and allow the MIN to be negative, use the following code.
Add this line (In bold) between the other two lines:
newVal = newVal.substring(0, dstart) + source.toString()+ newVal.substring(dstart, newVal.length());
if(newVal.equalsIgnoreCase("-") && min < 0)return null;
int input = Integer.parseInt(newVal);
this is my code max=100, min=0
xml
java
I know there are a million answers to this already, with one accepted. However, there are numerous bugs in the accepted answer and most of the rest simply fix one (or maybe two) of them, without expanding to all possible use cases.
So I basically compiled most of the bug fixes suggested in support answers as well as adding a method to allow continuous input of numbers outside of the range in the direction of 0 (if the range doesn't start at 0), at least until it's certain that it can no longer be in the range. Because to be clear, this is the only time that really causes trouble with many of the other solutions.
Here's the fix:
Note that some input cases are impossible to handle "actively" (i.e., as the user is inputting it), so we must ignore them and handle them after the user is done editing the text.
Here's how you might use it:
Now, when the user tries to type in a number closer to 0 than the range allows, one of two things will happen:
If
min
andmax
have the same number of digits, they won't be allowed to input it at all once they get to the final digit.If a number outside of the range is left in the field when the text loses focus, it will automatically be adjusted to the closest boundary.
And of course, the user will never be allowed to input a value farther from 0 than the range allows, nor is it possible for a number like that to "accidentally" be in the text field for this reason.
Known Issue(s?)
EditText
loses focus when the user is done with it.The other option is sanitizing when the user hits the "done"/return key, but in many or even most cases, this causes a loss of focus anyways.
However, closing the soft keyboard will not automatically un-focus the element. I'm sure 99.99% of Android developers wish it would (and that focus handling on
EditText
elements was less of a quagmire in general), but as of yet there is no built-in functionality for it. The easiest method that I've found to get around this, if you need to, is to extendEditText
something like this:This will "trick" the filter into sanitizing the input even though the view hasn't actually lost focus. If the view happens to later lose focus on its own, the input sanitation will trigger again, but nothing will change since it was already fixed.
Closing
Whew. That was a lot. What originally seemed like it would be a pretty trivially easy problem ended up uncovering many little ugly pieces of vanilla Android (at least in Java). And once again, you only need to add the listener and extend
EditText
if your range does not include 0 in some way. (And realistically, if your range doesn't include 0 but starts at 1 or -1, you also won't run into problems.)As a last note, this only works for ints. There is certainly a way to implement it to work with decimals (
double
,float
), but since neither I nor the original asker have a need for that, I don't particularly want to get all that deep into it. It would be very easy to simply use the post-completion filtering along with the following lines:You would only have to change from
int
tofloat
(ordouble
), allow insertion of a single.
(or,
, depending upon country?), and parse as one of the decimal types instead of anint
.That handles most of the work anyways, so it would work very similarly.
I made a simpler way to set a min/max to an Edittext. I use arithmetic keypad and I work with this method:
The method accepts all of your values but if a value of users in not adhere to your limits it will be set automatically to the min/max limit. For ex. limit limin=10, limax =80 if the user sets 8, automatically 10 is saved to a variable and EditText is set to 10.