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