Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter the input of EditText?

I want to filter the input of an EditText, only digits and letters are allowed, first I use TextWatcher to deal with the last input character, but when you move the cursor or you past some content to the EditText, this method failed, now I want to know is there a way to filter the illegal input and give the user a feedback.

like image 283
twlkyao Avatar asked May 15 '15 12:05

twlkyao


People also ask

What is the use of input filter?

The InputFilter Interface has one method, filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) , and it provides you with all the information you need to know about which characters were entered into the EditText it is assigned to.

How do you make EditText read only?

The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.

How do I change my EditText value?

Set the Text of Android EditText In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file. Following is the example to set the text of TextView control while declaring it in XML Layout file.


3 Answers

Add InputFilter to your EditText & provide a Toast for user . This code snippet will help you.

 InputFilter filter = new InputFilter() {
                public CharSequence filter(CharSequence source, int start, int end,
                        Spanned dest, int dstart, int dend) {
                    for (int i = start; i < end; i++) {
                        if (!Character.isLetterOrDigit(source.charAt(i))) { // Accept only letter & digits ; otherwise just return
                            Toast.makeText(context,"Invalid Input",Toast.LENGTH_SHORT).show();
                            return "";
                        }
                    }
                    return null;
                }

            };

        editText.setFilters(new InputFilter[] { filter });
like image 54
Don Chakkappan Avatar answered Oct 27 '22 01:10

Don Chakkappan


Have you tried the xml way? It will look something like this

<EditText 
  android:id="@+id/editText"
  android:inputType="text" 
  android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm" 
  android:hint="Only letters and numbers allowed.."
/>
like image 28
Bidhan Avatar answered Oct 27 '22 01:10

Bidhan


Work on all Android Versions:

public static InputFilter getOnlyCharactersFilter() {
    return getCustomInputFilter(true, false, false);
}

public static InputFilter getCharactersAndDigitsFilter() {
    return getCustomInputFilter(true, true, false);
}

public static InputFilter getCustomInputFilter(final boolean allowCharacters, final boolean allowDigits, final boolean allowSpaceChar) {
    return new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            boolean keepOriginal = true;
            StringBuilder sb = new StringBuilder(end - start);
            for (int i = start; i < end; i++) {
                char c = source.charAt(i);
                if (isCharAllowed(c)) {
                    sb.append(c);
                } else {
                    keepOriginal = false;
                }
            }
            if (keepOriginal) {
                return null;
            } else {
                if (source instanceof Spanned) {
                    SpannableString sp = new SpannableString(sb);
                    TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
                    return sp;
                } else {
                    return sb;
                }
            }
        }

        private boolean isCharAllowed(char c) {
            if (Character.isLetter(c) && allowCharacters) {
                return true;
            }
            if (Character.isDigit(c) && allowDigits) {
                return true;
            }
            if (Character.isSpaceChar(c) && allowSpaceChar) {
                return true;
            }
            return false;
        }
    };
}

Now you can use this filer like:

 //Accept Characters Only
edit_text.setFilters(new InputFilter[]{getOnlyCharactersFilter()});

//Accept Digits and Characters
edit_text.setFilters(new InputFilter[]{getCharactersAndDigitsFilter()});

//Accept Digits and Characters and SpaceBar
edit_text.setFilters(new InputFilter[]{getCustomInputFilter(true,true,true)});
like image 25
Salam El-Banna Avatar answered Oct 26 '22 23:10

Salam El-Banna