Specify the number of characters in edit text in android
Solution 1
Use following xml attributes to set maximum characters and digits to allow
android:digits
android:maxLength
For ex:
<EditText
android:id="@+id/et_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyz ."
android:maxLength="10" />
Here, This allows only lower case alphabets, space and dot (.)
Upate
set filter by java
InputFilter myFilter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(source.charAt(i))) {
return "";
}
if (i == start) {
return source.toUpperCase();
}
}
return source;
}
};
edit.setFilters(new InputFilter[]{myFilter});
Solution 2
android:maxLength="10"
This worked for me.
Solution 3
For the first part what you have to do is write:
android:maxLength="10"
as far as your second question is concerned writing only textPersonName is sufficient
android:inputType="textPersonName"
as referenced here input type
You have to use bit flags only when doing it programaticaly,which would correspond to:
setInputType(InputType.TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PERSON_NAME);
Solution 4
For setting the restriction on the number of characters you should use as already posted
android:maxLength="10"
in the EditText in XML File. And if you want the user to restrict to only alphabets then you will have to create a custom input filter in JAVA code as follows
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
Code taken from here.
Solution 5
Try this:
android:maxLength="10"
in EditText in XML file.
ZiZo
Updated on June 05, 2022Comments
-
ZiZo almost 2 years
I have a small trick on how to restrict the number of characters in an
edit text
in android..for example I want to put a limit say 10 characters for the name field, so it will not allow the user to enter more than 10 characters.Also, I have another problem in specifying the
inputtype
for text, I know the procedure which isandroid:inputType="text|textPersonName"
but it dosen't work since it allows the user to enter characters, numbers and also special characters.I will much appreciate your help.
Thanks in advance
-
ZiZo over 11 yearsthx so much this is worked perfectly. But how about the input type text, do you have any idea how to solve it?
-
ZiZo over 11 yearsoh this is perfect but for the filter code where I have to put it in order to be applied only on the edit text of the person name?
-
Antrromet over 11 yearsIn my last line edit.setFilters(new InputFilter[]{filter}); I am setting the filter to the EditText named edit. Similarly you put the filter on your EditText.
-
ZiZo over 11 yearsthe error is syntax error on token "setFilters", = expected after this token
-
sokie over 11 yearsi think it is enough to modify your xml to android:inputType="textPersonName" it is really not necesary to do it by code
-
sokie over 11 yearsif you really want to do it by code,you have to do it after you get your edittext from the xml: EditText editText = (EditText) findViewById(R.id.yourEditText); editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PERSON_NAME);
-
ZiZo over 11 yearswhen I do it using this attribute android:inputType="textPersonName" when the emulato runs it allows the user to enter alphabets, numbers and special characters. But when I d it using th line of code that you provided I got an error in the second line which is editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PERSON_NAME); a red line under setInputType, "|" and "."
-
Antrromet over 11 yearsyou need to write "edit.setFilters(new InputFilter[]{filter});" where "edit" is the name of your EditText. edit=(EditText)findViewById(R.id.editText1); edit.setFilters(new InputFilter[]{filter});
-
ZiZo over 11 yearsOh it works well with digits attribute, but is there a way to make the first letter uppercase and the rest lower?
-
ZiZo over 11 yearsok it works well but it doesn't work with maxLength since it allows the user to enter as many as characters he wants. Any help?
-
Jayabal over 11 yearsits not possible to do in xml, you've set filter, see updated answer.
-
Antrromet over 11 yearsIf android:maxLength="10" does not work then again you could apply a custom filter. Look at this link stackoverflow.com/questions/3285412/…
-
ZiZo over 11 yearsI did wat you've said but I got an error in return source.toUpperCase; the error under toUpperCase
-
ZiZo over 11 yearsthis is the xml <EditText android:id="@+id/edittext1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputType="textPersonName" />
-
ZiZo over 11 yearsand this is the main.java InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (!Character.isLetter(source.charAt(i))) { return ""; } }return null; }}; edit=(EditText)findViewById(R.id.edittext1); edit.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(5)});
-
ZiZo over 11 yearsbut I need the first letter to be uppercase. Could you help me?
-
Antrromet over 11 yearsUse this in your xml android:inputType="textCapSentences" or android:capitalize="sentences".
-
Jayabal over 11 yearscheck updated ans. use source.toUpperCase(); instead of source.toUpperCase;, if again returns any error post error log
-
Zubair Ahmed almost 9 years@ZiZo use
source.toString().toUpperCase();
as source is aCharSequence
type andtoUpperCase()
is accessable forString
type values.