How to make EditText accept input in format:
4digit 4digit 4digit 4digit
I tried Custom format edit text input android to acc
Not sure the TextWatcher is the right thing to use - we should use InputFilter
According to Android documentation, TextWatcher should be used for an external usage example : one [EditView] for password input + one [TextView] view which displays "weak", "strong", etc...
For Credit Card Format I am using InputFilter:
public class CreditCardInputFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (dest != null & dest.toString().trim().length() > 24) return null;
if (source.length() == 1 && (dstart == 4 || dstart == 9 || dstart == 14))
return " " + new String(source.toString());
return null; // keep original
}
}
And combine with a length filter (Android SDK) :
mEditCardNumber.setFilters(new InputFilter[]{
new InputFilter.LengthFilter(24),
new CreditCardInputFilter(),
});
This handle the case when typing and removing a digit.
(!) But this does not handle the case for a copy/paste of an entire string, this one should be done in a different InputFilter class
Hope it helps !