I am developing an application for keyboard, but i am geting an issue. I want to restrict/block some special character from soft keyboard in EditText in android programmatic
Unfortunately the accepted solution doesn't work in all the cases. The proper solution would be to use the following InputFilter:
private InputFilter filter = new InputFilter() {
// An example pattern that restricts the input only to the lowercase letters
private static final Pattern restrictedChars = Pattern.compile("[a-z]*")
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
final CharSequence replacementText = source.subSequence(start, end);
final CharSequence replacedText = dest.subSequence(dstart, dend);
if (source == null || restrictedChars.matcher(replacementText).matches()) {
return null; // Accept the original replacement
}
return replacedText; // Keep the original text
}
};
This solution differs from the accepted one in that it solves the following problems: