I am trying to limit a numeric edittext only to a range of numbers. I have found some tutorials that use InputFilter to do this but once implemeted I cannot type into the EditText anymore.
As per tutorials I create a class as:
package africa.mykagovehicles;
import android.text.InputFilter;
import android.text.Spanned;
public class InputFilterMinMax implements InputFilter {
private int minimumValue;
private int maximumValue;
public InputFilterMinMax(int minimumValue, int maximumValue) {
this.minimumValue = minimumValue;
this.maximumValue = maximumValue;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int input = Integer.parseInt(dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length()));
if (isInRange(minimumValue, maximumValue, 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;
}
}
Then in the activity I set the InputFilter with:
txtYearOfManufacture.setFilters(new InputFilter[] {new InputFilterMinMax(1950, 2020)});
No clue why I can't type into the EditText. Any help appreciated.
Try to change the code as follows