0

I have take TextInputEditText for first name only accepts alphabets but i have got problem.

        <android.support.design.widget.TextInputLayout
            android:id="@+id/tvfnameinput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/etFirstname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                android:hint="firstname"
                android:imeOptions="actionNext"
                android:maxLength="12"
                android:maxLines="1"
                android:text="" />
        </android.support.design.widget.TextInputLayout>

As you see in above layout i have set maxlength 12 so if i have enter 12 numeric value then it's not accepts but it's count so i can't able to enter alphabets after entering 12 numeric value.

NOTE-: I want just alphabets and my code is working but when i entered numeric value then it's not accept but count as a maxlenght.

Edit-: android:imeOptions="actionNext" not show on Keyboard when i have set android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

enter image description here

Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
  • your layout is working fine on my device which runs nougat(API version 26). And I think you also need to add inputType="text" also. also I ran this on KitKat too and it's working fine. – Umair Jan 31 '18 at 05:58
  • @Umair i have set inputType="text" but problem is same, please read once more my problem. – Dileep Patel Jan 31 '18 at 06:05
  • that's what I am saying your code is working fine. Even if I enter like 50 digits they don't add and after that I add 12 alphabets it add them. Maybe your problem is some device specific. – Umair Jan 31 '18 at 06:09
  • @Umair no i am test 2 different device but problem is same – Dileep Patel Jan 31 '18 at 06:30

1 Answers1

1

EDIT

If you want to set only Alphabets then you can use InputFilter instead of digits. Remove this tag digit from your layout.

Here is the example that can help you:

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

Try this one. it is tested by myself and working like charm

editText.setFilters(new InputFilter[]{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();
        }
    }});

Input Filter with MaxLength

When using InputFilter the property of EditText is overridden then you can use max length with InputFiler.

Try this

 InputFilter[] inputFilters = new InputFilter[2];
    inputFilters[0] = 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();
        }
    };

    inputFilters[1] = new InputFilter.LengthFilter(12);

    editText.setFilters(inputFilters);
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40