I am currently using
android:digits="qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM "
in android XML in EditText and it is working perfectly. The user can enter only characters from a-z,A-Z and space(I don't want to allow the user to enter numbers or special characters). I would like to add this property to another Edit Text field programaticaly. Can anyone help me to add the above property programmaticaly. I have referred the following links but its of no use
Change android:digits programmatically How to block special characters in androids editText? no ANDROID:DIGITS
if I use
edtTxt.setKeyListener(DigitsKeyListener.getInstance("qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM"));
numeric keypad is getting opened.
Try the code below
edtTxt.setInputType(InputType.TYPE_CLASS_TEXT);
edtTxt.setFilters(new InputFilter[]{
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if (src.equals("")) {
return src;
}
if (src.toString().matches("[a-zA-Z ]+")) {
return src;
}
return "";
}
}
});
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