How do you set the max number of characters for an Android EditText input? I see setMaxLines, setMaxEMS, but nothing for the number of characters.
-
Also, the SO answer [here][1] to set the max length dynamically [1]: http://stackoverflow.com/a/4145983/530513 – David O'Meara Aug 07 '12 at 01:41
12 Answers
In XML you can add this line to the EditText
View
where 140 is the maximum number of characters:
android:maxLength="140"

- 3,706
- 34
- 55

- 4,669
- 6
- 38
- 64
-
can you lead me how can we achieve this by using the properties tab in the eclipse's xml graphical view? – Nitesh Verma Oct 08 '13 at 06:50
-
-
Dynamically:
editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_NUM) });
Via xml:
<EditText
android:maxLength="@integer/max_edittext_length"

- 4,402
- 2
- 43
- 45
You can use a InputFilter, that's the way:
EditText myEditText = (EditText) findViewById(R.id.editText1);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(10); //Filter to 10 characters
myEditText .setFilters(filters);

- 27,862
- 20
- 113
- 121

- 692
- 6
- 11
I always set the max like this:
<EditText
android:id="@+id/edit_blaze_it
android:layout_width="0dp"
android:layout_height="@dimen/too_high"
<!-- This is the line you need to write to set the max-->
android:maxLength="420"
/>

- 616
- 7
- 15
It is working for me.
etMsgDescription.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maximum_character)});
etMsgDescription.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
tvCharacterLimite.setText(" "+String.valueOf(maximum_character - etMsgDescription.getText().length()));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

- 2,321
- 24
- 36
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter device name"
android:maxLength="10"
android:inputType="textFilter"
android:singleLine="true"/>
InputType has to set "textFilter"
android:inputType="textFilter"

- 708
- 1
- 6
- 9
use this function anywhere easily:
public static void setEditTextMaxLength(EditText editText, int length) {
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(length);
editText.setFilters(FilterArray);
}

- 127
- 1
- 10
It worked for me this way, it's the best I've found. It is for a max length of 200 characters
editObservations.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editObservations.getText().length() >= 201){
String str = editObservations.getText().toString().substring(0, 200);
editObservations.setText(str);
editObservations.setSelection(str.length());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});

- 2,435
- 7
- 34
- 60

- 81
- 1
- 2
Obviously you can use maxLength in xml or InputFilter.LengthFilter as answered above. But for me in some cases, it was not enough. I created a class for more flexible settings of EditText: https://github.com/devapro/NumberWatcher It is implementation only for numbers input, but you can change it for any of the types.

- 51
- 1
- 6
it doesn't work from XML with maxLenght I used this code, you can limit the number of characters
String editorName = mEditorNameNd.getText().toString().substring(0, Math.min(mEditorNameNd.length(), 15));

- 159
- 9
You could add a validation hook to a submit or change event.
if (EditText.length() <=10){
Toast.makeText(MainAcitvity.this, "Enter Valid Number", Toast.LENGTH_SHORT).show();
contact.setError("Enter Valid");
return false;
}

- 3,256
- 5
- 50
- 67

- 1
- 1
-
2Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 10:55