Question Detail

How to restrict EditText to accept only Text Values not Numbers in android?

6 years ago Views 4656 Visit Post Reply

In my Registration form Name, field (EditText) accepting Number values but Name can't be a Number. How to restrict Edit Text to restrict to get only Text Values and denial Number Value? Numbers should not enter in Name Field.

Please suggest any Working Example for Restrict EditText to only get Text.

 

I have tried below code but not working 

<EditText
    android:id="@+id/name_EditText_signup_ID"
    style="@style/edittextStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/top_right_round_edge_input"
    android:hint="Full Name"
    android:inputType="textPersonName"
    android:padding="@dimen/fieldPadding"/>


Thread Reply

Hemant Sharma

- 6 years ago

Try this Solution:

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/top_right_round_edge_input"
    android:hint="Full Name"
    android:inputType="textPersonName"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>

if you want to set EditText Input value programmatically in your Java file / From Code:

editTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

Hemant Sharma

- 6 years ago

Try this Solution:

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/top_right_round_edge_input"
    android:hint="Full Name"
    android:inputType="textPersonName"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>

if you want to set EditText Input value programmatically in your Java file / From Code:

editTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

alex levine

- 6 years ago

This is also working but I am not able to add space to

editTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

 

For deny Numbers and accept the only Alphabet in EditText I have to use setFilters 

name_EditText_profile.setFilters(new InputFilter[]{acceptonlyAlphabetValuesnotNumbersMethod()});

 

//Accept Only Alphabet in EditText
public static InputFilter acceptonlyAlphabetValuesnotNumbersMethod() {
    return new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            boolean isCheck = 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 {
                    isCheck = false;
                }
            }
            if (isCheck)
                return null;
            else {
                if (source instanceof Spanned) {
                    SpannableString spannableString = new SpannableString(sb);
                    TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, spannableString, 0);
                    return spannableString;
                } else {
                    return sb;
                }
            }
        }

        private boolean isCharAllowed(char c) {
            Pattern pattern = Pattern.compile("^[a-zA-Z ]+$");
            Matcher match = pattern.matcher(String.valueOf(c));
            return match.matches();
        }
    };
}