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.
please check this code
USe the below link for more details about edittext and the edittextfilteres with text watcher..
http://www.mobisoftinfotech.com/blog/android/android-edittext-setfilters-example-numeric-text-field-patterns-and-length-restriction/
First make this class :
Then use this from your Activity :
This will allow user to enter values from 1 to 12 only.
EDIT :
Set your edittext with
android:inputType="number"
.Thanks.
I found my own answer. It is very late now but I want to share it with you. I implement this interface:
And then implement it in this way inside your activity:
This is a very simple answer, if any better please tell me.
//still has some problem but Here you can use min, max at any range (positive or negative)
To add to Pratik's answer, here is a modified version where user can enter min 2 digits also , for example, 15 to 100:
Hope this helps. kindly dont downvote without reasons.
@Pratik Sharma
For support negative numbers, add the following code within the filter method:
Then use this from your Activity :
Set your edittext with: