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.
I extended @Pratik Sharmas code to use BigDecimal objects instead of ints so that it can accept larger numbers, and account for any formatting in the EditText that isn't a number (like currency formatting i.e. spaces, commas and periods)
EDIT: note that this implementation has 2 as the minimum significant figures set on the BigDecimal (see the MIN_SIG_FIG constant) as I used it for currency, so there was always 2 leading numbers before the decimal point. Alter the MIN_SIG_FIG constant as necessary for your own implementation.