I'm using InputFilter like this to allow only alpha and numbers
private InputFilter[] inputFilters = new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; ++i) { if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches()) { return ""; } } return null; } } };
But problem is "android:maxLength" value in xml file is not working with this InputFilter
I think I need code in InputFilter to set max-length of EditText
Anyone has good idea for this?
Thanks
setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")}); This will allow user to enter values from 1 to 12 only. EDIT : Set your edittext with android:inputType="number" .
A simple one-liner would be:
myEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(10) }); //replace 10 with required length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With