I have an app with a single numberdecimal EditText. I want to make so that the user can not enter a value greater than 120.0. I do not know how to do this. However, I was able to make the input of numbers from 1 to 120 (without decimals).
Here is my code:
edittext.setFilters(new InputFilter[] {new InputFilterMinMax(1,120)});
public class InputFilterMinMax implements InputFilter {
private int min, max;
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int input = Integer.parseInt(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}