How do you set the max number of characters for an EditText in Android?

AndroidAndroid Edittext

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

In XML you can add this line to the EditText View where 140 is the maximum number of characters:

android:maxLength="140"

Solution 2 - Android

Dynamically:

editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_NUM) });

Via xml:

<EditText
    android:maxLength="@integer/max_edittext_length"

Solution 3 - Android

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

Solution 4 - Android

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"
        />

Solution 5 - Android

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

Solution 6 - Android

   <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"

Solution 7 - Android

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

Solution 8 - Android

Kotlin way

editText.filters = arrayOf(InputFilter.LengthFilter(maxLength))

Solution 9 - Android

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

Solution 10 - Android

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.

Solution 11 - Android

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

Solution 12 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbarcodereaderView Question on Stackoverflow
Solution 1 - AndroidMassimo VarioloView Answer on Stackoverflow
Solution 2 - AndroidgoRGonView Answer on Stackoverflow
Solution 3 - AndroidSuperlanderoView Answer on Stackoverflow
Solution 4 - AndroidZooMagicView Answer on Stackoverflow
Solution 5 - AndroidChiragView Answer on Stackoverflow
Solution 6 - AndroidShetty Suresh Babu.View Answer on Stackoverflow
Solution 7 - AndroidMilad MohamadiView Answer on Stackoverflow
Solution 8 - AndroidVladView Answer on Stackoverflow
Solution 9 - AndroidCamilo SotoView Answer on Stackoverflow
Solution 10 - AndroidArseniyView Answer on Stackoverflow
Solution 11 - AndroidLeo SView Answer on Stackoverflow
Solution 12 - AndroidAhmer Altaf 427View Answer on Stackoverflow