Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to user android:digits programmatically [duplicate]

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.

like image 668
Anudeep Avatar asked Jan 04 '16 08:01

Anudeep


1 Answers

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 "";
                    }
                }
        });
like image 123
Febi M Felix Avatar answered Oct 03 '22 12:10

Febi M Felix