I have an edittext and I want to restrict the numerical values inserted in that edittext between 18 and 85. I am currently using this InputFilter over my edittext. But not useful for me
public class InputFilterMinMax implements InputFilter {
private int min, max;
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
try {
// Remove the string out of destination that is to be replaced
String replacement = source.subSequence(start, end).toString();
String newVal = dest.subSequence(0, dstart).toString()
+ replacement
+ dest.subSequence(dend, dest.length()).toString();
int input = Integer.parseInt(newVal);
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;
}
}
Please help me out.. Thanks