How to create EditText accepts Alphabets only in android?

AndroidLayoutAndroid Edittext

Android Problem Overview


How can I enter only alphabets in EditText in android?

Android Solutions


Solution 1 - Android

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" />

Solution 2 - Android

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 !

Solution 3 - Android

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

Solution 4 - Android

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,";
					
				}

Solution 5 - Android

Through Xml you can do easily as type following code in xml (editText)...

android:digits="abcdefghijklmnopqrstuvwxyz"

only characters will be accepted...

Solution 6 - Android

Put code edittext xml file,

   android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Solution 7 - Android

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 "

Solution 8 - Android

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}); 

Solution 9 - Android

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 ..

Solution 10 - Android

If anybody still wants this, https://stackoverflow.com/questions/10894122/java-regex-for-support-unicode#10894689 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()

Solution 11 - Android

  • 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:

Auto complete Demo

Solution 12 - Android

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 ""
            }
        }
    )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUMAR-MOBITSOLUTIONSView Question on Stackoverflow
Solution 1 - AndroidSandeepView Answer on Stackoverflow
Solution 2 - AndroidSubhasView Answer on Stackoverflow
Solution 3 - Androidswetabh sumanView Answer on Stackoverflow
Solution 4 - AndroidUMAR-MOBITSOLUTIONSView Answer on Stackoverflow
Solution 5 - AndroidNaveed AshrafView Answer on Stackoverflow
Solution 6 - AndroidNajib.NjView Answer on Stackoverflow
Solution 7 - AndroidNaveen Kumar MView Answer on Stackoverflow
Solution 8 - AndroidUmar WaqasView Answer on Stackoverflow
Solution 9 - AndroidVinayak BevinakattiView Answer on Stackoverflow
Solution 10 - AndroidmdzekoView Answer on Stackoverflow
Solution 11 - Androidsafwan shaibView Answer on Stackoverflow
Solution 12 - AndroidYahya MView Answer on Stackoverflow