I want to restrict the edittext input to -99.99. But my inputfilter prevents me to enter "-". Could you guys help me to check what's wrong with it.
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 {
double input = Double.valueOf(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
mainactivity
text.setFilters(new InputFilter[]{new InputFilterMinMax("-99", "99")});
xml
android:inputType="numberDecimal|numberSigned"
android:digits="0123456789+-."
thanks :)
Then you can change the data type to string to allow it, followed by extracting the relevant numbers (depending how clean coded you want).
At the moment, it is Double...so it wouldn't take negative.