20

I have a TextView in my app that i want a user to be able to only enter alpha-numeric characters in. How can this be done? Thanks!

android-developer
  • 1,574
  • 4
  • 20
  • 27

4 Answers4

53

In the XML, put this:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
strange quark
  • 5,205
  • 7
  • 41
  • 53
  • 1
    Wow Android makes it so easy. iOS needs 1000 times more effort to achieve the same functionality. – Moe Apr 24 '12 at 15:15
  • 4
    I used this method for alpha numeric android:digits="abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" but Samsung galaxy y pro got problem it not allowing the Uppercase character(E,R,T,D,F,G,X,C,V) that has numbers. – Navaneethan Feb 06 '13 at 10:37
  • 2
    Wouldn't this solution be limited to english language only? [You could use something like @string/digits for this](https://developer.android.com/reference/android/widget/TextView.html#attr_android:digits), but that seems like a major hassle to have to do per every language. – Charles Madere Mar 07 '14 at 23:27
17

Here is a better solution......... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA

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))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter});
Ach J
  • 510
  • 5
  • 9
  • Much Better Solution by allowing Capital letters. – hemanth kumar Oct 17 '13 at 10:16
  • This solution works for the simple cases of individual characters being typed, but does not properly handle copy and pasting, or when other `InputFilters` are set on the `EditText`. I have posted a more complete solution. – Steven Byle Dec 22 '15 at 15:40
5

The InputFilter solution works well, and gives you full control to filter out input at a finer grain level than android:digits. The filter() method should return null if all characters are valid, or a CharSequence of only the valid characters if some characters are invalid. If multiple characters are copied and pasted in, and some are invalid, only the valid characters should be kept (@AchJ's solution will reject the entire paste if any characters a invalid).

public static class AlphaNumericInputFilter implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {

        // Only keep characters that are alphanumeric
        StringBuilder builder = new StringBuilder();
        for (int i = start; i < end; i++) {
            char c = source.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                builder.append(c);
            }
        }

        // If all characters are valid, return null, otherwise only return the filtered characters
        boolean allCharactersValid = (builder.length() == end - start);
        return allCharactersValid ? null : builder.toString();
    }
}

Also, when setting your InputFilter, you must make sure not to overwrite other InputFilters set on your EditText; these could be set in XML, like android:maxLength. You must also consider the order that the InputFilters are set. When used in conjunction with a length filter, your custom filter should be inserted before the length filter, that way pasted text applies the custom filter before the length filter (@AchJ's solution will overwrite all other InputFilters and only apply the custom one).

    // Apply the filters to control the input (alphanumeric)
    ArrayList<InputFilter> curInputFilters = new ArrayList<InputFilter>(Arrays.asList(editText.getFilters()));
    curInputFilters.add(0, new AlphaNumericInputFilter());
    InputFilter[] newInputFilters = curInputFilters.toArray(new InputFilter[curInputFilters.size()]);
    editText.setFilters(newInputFilters);
Steven Byle
  • 13,149
  • 4
  • 45
  • 57
  • to simplify the setFilters I would do : editText.setFilters(new InputFilter[]{new CurrencyFormatInputFilter()}); – Rocel Jan 27 '16 at 11:37
  • 1
    @Rocel please read my explanation carefully: "_you must make sure not to overwrite other InputFilters set on your EditText; these could be set in XML, like `android:maxLength`._" Your suggestion would overwrite any other `InputFilter`, just like @AchJ's solution, which is *not* what we want to do here. – Steven Byle Jan 27 '16 at 14:01
-9

This should work:

textView.setInputType(InputType.TYPE_CLASS_NUMBER);