How to set maximum character count for EditText in Android?

How to set maximum number of characters for Android EditText login? I see setMaxLines, setMaxEMS, but nothing for the number of characters.

+81
android android-edittext
May 20 '11 at
source share
6 answers

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

 android:maxLength="140" 
+189
Aug 02 '11 at 2:30 p.m.
source share

You can use InputFilter to:

 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); 
+38
Apr 18 '13 at 0:20
source share

Dynamically:

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

Via xml:

 <EditText android:maxLength="@integer/max_length" 
+32
May 20 '14 at 10:33
source share

He works 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 } }); 
+3
Apr 25 '14 at 9:59
source share

I always set max as follows:

  <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" /> 
+1
Nov 23 '17 at 12:45
source share



All Articles