How can I enter only alphabets in EditText in android?
- 
                    Kotlin Solution here - https://stackoverflow.com/a/52947835/3333878 – abitcode Oct 23 '18 at 11:32
14 Answers
Add this line with your EditText tag.
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Your EditText tag should look like:
<EditText
        android:id="@+id/editText1"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
- 
                    12
- 
                    8
- 
                    1extra add this line for suggestion android:inputType="text|textNoSuggestions" – msevgi Oct 21 '15 at 11:30
- 
                    after char insertion if we try to add number it repeat previous char – Rahul Chaudhary Mar 25 '16 at 08:19
- 
                    5use this to allow spaces too: `android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "` – vishalknishad Aug 23 '17 at 10:15
- 
                    HI @Sandeep when i entered numeric value then it's not show but count so problem arise when i have set maxLength.. – Dileep Patel Jan 31 '18 at 06:33
- 
                    @vishalknishad If you put space at the end of a string it will be automatically trimmed off, but rather put the space in between your strings like this `abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ` – Alaa AbuZarifa Dec 30 '20 at 12:15
- 
                    
- 
                    Did anyone get abnormal behavior of blinking cursor? Blinking cursor is stuck at the start of the textbox. – Muhammad Saqib Aug 22 '21 at 11:54
- 
                    
- 
                    
edittext.setFilters(new InputFilter[] {
    new InputFilter() {
        public CharSequence filter(CharSequence src, int start,
                int end, Spanned dst, int dstart, int dend) {
            if(src.equals("")){ // for backspace
                return src;
            }
            if(src.toString().matches("[a-zA-Z ]+")){
                return src;
            }
            return edittext.getText().toString();
        }
    }
});
please test thoroughly though !
 
    
    - 4,924
- 2
- 37
- 43
 
    
    - 647
- 1
- 5
- 3
- 
                    11It didn't work for me. i did the same but when i enter numbers or special characters into EditText, it will remove all the characters which enter earlier. Why it is happen so? – Nayana_Das Nov 07 '15 at 05:59
- 
                    @Nayana_Das That might be because of this line `return "";` . Replace that line with some text without the invalid character instead of returning an empty string. – Rahul Chowdhury Nov 14 '16 at 09:22
- 
                    1Didn't work for me. When I type a number, if duplicates the text which is in the EditText. So if I had written "aba", it will put "abaaba" in the EditText. – FabioR Aug 05 '20 at 15:09
- 
                    when I press ( ) or number then it duplicates the data and this is a serious problem with that answer. Go for @swetabhSuman's answer. – Vijay Oct 08 '20 at 13:42
For those who want that their editText should accept only alphabets and space (neither numerics nor any special characters), then one can use this InputFilter.
Here I have created a method named getEditTextFilter() and written the InputFilter inside it.
public static InputFilter getEditTextFilter() {
        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)) // put your condition here
                        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) {
                Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
                Matcher ms = ps.matcher(String.valueOf(c));
                return ms.matches();
            }
        };
    }
Attach this inputFilter to your editText after finding it, like this :
mEditText.setFilters(new InputFilter[]{getEditTextFilter()});
The original credit goes to @UMAR who gave the idea of validating using regular expression and @KamilSeweryn
 
    
    - 1,949
- 2
- 19
- 24
- 
                    10The Best answer. Have no idea why no more upvotes. android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" is the baddest solution cause android:imeOption not working with it. – Rahim Rahimov May 04 '17 at 13:08
- 
                    
- 
                    As @mdzeko suggested: "^[a-zA-Z ]+$" -> "^[\\p{L} ]+$" It works for me for Unicode. – Dark Sep 02 '19 at 11:41
- 
                    1Thank you man! You save my day! But it's not working on some devices (includes my - xiaomi redmi note 2), need remove suggestions for `EditText`. And the only way which work it's set `android:inputType="textVisiblePassword"`. Also I change `isCharAllowed(c)` function and make it without **Regex**. My way: `allowSymbols.contains(c, ignoreCase = true)`, where `allowSymbols` - a `string` with allowed characters. – SerjantArbuz Feb 12 '22 at 17:48
- 
                    1This works and also helped me in discovering my own regex to use to get my solution, Thanks – Neo Mar 05 '23 at 16:17
EditText state = (EditText) findViewById(R.id.txtState);
                Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
                Matcher ms = ps.matcher(state.getText().toString());
                boolean bs = ms.matches();
                if (bs == false) {
                    if (ErrorMessage.contains("invalid"))
                        ErrorMessage = ErrorMessage + "state,";
                    else
                        ErrorMessage = ErrorMessage + "invalid state,";
                }
 
    
    - 77,236
- 95
- 209
- 278
Through Xml you can do easily as type following code in xml (editText)...
android:digits="abcdefghijklmnopqrstuvwxyz"
only characters will be accepted...
 
    
    - 51,061
- 28
- 99
- 211
 
    
    - 295
- 2
- 6
Put code edittext xml file,
   android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
    
    - 3,706
- 1
- 25
- 39
For spaces, you can add single space in the digits. If you need any special characters like the dot, a comma also you can add to this list
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
 
    
    - 7,497
- 7
- 60
- 74
Allow only Alphabets in EditText android:
InputFilter letterFilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String filtered = "";
            for (int i = start; i < end; i++) {
                char character = source.charAt(i);
                if (!Character.isWhitespace(character)&&Character.isLetter(character)) {
                    filtered += character;
                }
            }
            return filtered;
        }
    };
editText.setFilters(new InputFilter[]{letterFilter}); 
 
    
    - 170
- 2
- 8
- 
                    This worked as per my requirements after a couple more customizations. +1 – Shahood ul Hassan Dec 06 '19 at 03:43
Try This
<EditText
  android:id="@+id/EditText1"
  android:text=""
  android:inputType="text|textNoSuggestions"
  android:textSize="18sp"
  android:layout_width="80dp"
  android:layout_height="43dp">
</EditText>
Other inputType can be found Here ..
 
    
    - 2,169
- 4
- 33
- 59
 
    
    - 40,205
- 25
- 108
- 139
If anybody still wants this, Java regex for support Unicode? is a good one. It's for when you want ONLY letters (no matter what encoding - japaneese, sweedish) iside an EditText. Later, you can check it using Matcher and Pattern.compile() 
- Just use the attribute android:inputType="text"in your xml file
EX: if you want the user to provide an email address text:
 <EditText
    android:id="@+id/user_address"
    android:inputType="textEmailAddress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
feel free to check many other input attributes using android studio's autocomplete feature:
 
    
    - 11
- 2
Simple answer
EditText editTextName = findViewById(R.id.editTextName);
String name = editTextName.getText().toString().trim();
if(!name.matches("[a-zA-Z ]+")) {
editTextName.setError("Please enter only letters");
}
 
    
    - 374
- 4
- 11
EditText editText = (EditText) findViewById(R.id.et_name);
editText.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;
                }
                String str = src.toString();
                str = str.replaceAll("[^a-zA-Z ]", "");
                return str;
            }
        }
});
 
    
    - 315
- 4
- 6
Try This Method
For Java :
EditText yourEditText = (EditText) findViewById(R.id.yourEditText);
yourEditText.setFilters(new InputFilter[] {
new InputFilter() {
    @Override
    public CharSequence filter(CharSequence cs, int start,
                int end, Spanned spanned, int dStart, int dEnd) {
        // TODO Auto-generated method stub
        if(cs.equals("")){ // for backspace
             return cs;
        }
        if(cs.toString().matches("[a-zA-Z ]+")){
             return cs;
        }
        return "";
    }
}});
For Kotlin :
 val yourEditText = findViewById<View>(android.R.id.yourEditText) as EditText
    val reges = Regex("^[0-9a-zA-Z ]+$")
    //this will allow user to only write letter and white space
    yourEditText.filters = arrayOf<InputFilter>(
        object : InputFilter {
            override fun filter(
                cs: CharSequence, start: Int,
                end: Int, spanned: Spanned?, dStart: Int, dEnd: Int,
            ): CharSequence? {
                if (cs == "") { // for backspace
                    return cs
                }
                return if (cs.toString().matches(reges)) {
                    cs
                } else ""
            }
        }
    )
 
    
    - 392
- 3
- 13
 
     
     
     
    