Specify the number of characters in edit text in android

18,563

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.

Share:
18,563
ZiZo
Author by

ZiZo

Updated on June 05, 2022

Comments

  • ZiZo
    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 is android: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
    ZiZo over 11 years
    thx so much this is worked perfectly. But how about the input type text, do you have any idea how to solve it?
  • ZiZo
    ZiZo over 11 years
    oh 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
    Antrromet over 11 years
    In 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
    ZiZo over 11 years
    the error is syntax error on token "setFilters", = expected after this token
  • sokie
    sokie over 11 years
    i think it is enough to modify your xml to android:inputType="textPersonName" it is really not necesary to do it by code
  • sokie
    sokie over 11 years
    if 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
    ZiZo over 11 years
    when 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
    Antrromet over 11 years
    you 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
    ZiZo over 11 years
    Oh it works well with digits attribute, but is there a way to make the first letter uppercase and the rest lower?
  • ZiZo
    ZiZo over 11 years
    ok 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
    Jayabal over 11 years
    its not possible to do in xml, you've set filter, see updated answer.
  • Antrromet
    Antrromet over 11 years
    If android:maxLength="10" does not work then again you could apply a custom filter. Look at this link stackoverflow.com/questions/3285412/…
  • ZiZo
    ZiZo over 11 years
    I did wat you've said but I got an error in return source.toUpperCase; the error under toUpperCase
  • ZiZo
    ZiZo over 11 years
    this is the xml <EditText android:id="@+id/edittext1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputType="textPersonName" />
  • ZiZo
    ZiZo over 11 years
    and 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
    ZiZo over 11 years
    but I need the first letter to be uppercase. Could you help me?
  • Antrromet
    Antrromet over 11 years
    Use this in your xml android:inputType="textCapSentences" or android:capitalize="sentences".
  • Jayabal
    Jayabal over 11 years
    check updated ans. use source.toUpperCase(); instead of source.toUpperCase;, if again returns any error post error log
  • Zubair Ahmed
    Zubair Ahmed almost 9 years
    @ZiZo use source.toString().toUpperCase(); as source is a CharSequence type and toUpperCase() is accessable for String type values.