1

I want to restrict EditText to only get Text, not any number. I have tried this:

1-- In XML:

        <EditText android:id="@+id/editText1" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:hint="@string/search_prompt"
        android:inputType="text"/>

This did not work for me.

2-- in Java code got reference Text Only Input EditText android

myEditText.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);

but this also doesn't worked.

Community
  • 1
  • 1
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

2 Answers2

14

you can do that in xml using

    <EditText
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" />
Duran Jayson
  • 327
  • 3
  • 6
9

you can use InputFilter to restrict EditText to only get text not to get nummbers as:

InputFilter filtertxt = 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.isLetter(source.charAt(i))) { 
                            return ""; 
                    } 
            } 
            return null; 
     } 
}; 

editxt.setFilters(new InputFilter[]{filtertxt});

EDIT : you can create an char array inside InputFilter which you want accepted by Edittext as or you can also use regex validation

        InputFilter[] Textfilters = new InputFilter[1];
        Textfilters[0] = new InputFilter(){
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if (end > start) {

                char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','.'};

                for (int index = start; index < end; index++) {
                    if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) {
                    return "";
                      }
                    }
                 }
                    return null;
            }

        };
editxt.setFilters(Textfilters);
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213