Android EditText Max Length

AndroidAndroid Edittext

Android Problem Overview


I set up a max length of text in an EditText field.

<EditText
	        android:id="@+id/courseDescriptionField"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:inputType="textMultiLine"
	        android:lines="5"
	        android:maxLength="@integer/max_course_description_limit"
	        android:gravity="left|top" >
	    </EditText>

The problem for me however is, that, text STOPS appearing after 140 characters, but it still continues to type, except that text just doesn't appear, but however, it does appear in the "buffer" (meaning the suggestion thing) if that's what you would call it.

As a side note, I am using a TextWatcher to keep track of the limit. Is there any way to completely limit the amount of text, so that when there are 140 characters, nothing happens when something other than backspace/delete is pressed?

Android Solutions


Solution 1 - Android

Possible duplicate of https://stackoverflow.com/questions/3285412/limit-text-length-of-edittext-in-android

Use android:maxLength="140"

That should work. :)

Hope that helps

Solution 2 - Android

I had the same problem.

Here is a workaround

android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="6"

Thx to https://stackoverflow.com/questions/3789905/how-can-i-turnoff-suggestions-in-edittext

Solution 3 - Android

EditText editText= ....;
InputFilter[] fa= new InputFilter[1];
fa[0] = new InputFilter.LengthFilter(8);
editText.setFilters(fa);

Solution 4 - Android

I had the same problem. It works perfectly fine when you add this:

android:inputType="textFilter"

to your EditText.

Solution 5 - Android

You may try this

 EditText et = (EditText) findViewById(R.id.myeditText);
    et.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(140) }); // maximum length is 140

Solution 6 - Android

If you used maxLength = 6 , some times what you are entering those characters are added in top of the keyboard called suggestions. So when you deleting entered letters that time it will delete suggestions first and then actual text inside EditText. For that you need to remove the suggestions.just add

android:inputType="textNoSuggestions"` 

or

android:inputType="textFilter"

It will remove those suggestions.

Solution 7 - Android

If you want to see a counter label you can use app:counterEnabled and android:maxLength, like:

<android.support.design.widget.TextInputLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:counterEnabled="true">

  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLength="420" />

</android.support.design.widget.TextInputLayout>

DO NOT set app:counterMaxLength on TextInputLayout because it will conflict with android:maxLength resulting into the issue of invisible chars after the text hits the size limit.

Solution 8 - Android

For me this solution works:

edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

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
QuestionJon WeiView Question on Stackoverflow
Solution 1 - AndroidUshal NaidooView Answer on Stackoverflow
Solution 2 - AndroidmrroboaatView Answer on Stackoverflow
Solution 3 - AndroidSamiraView Answer on Stackoverflow
Solution 4 - AndroidAkeshwar JhaView Answer on Stackoverflow
Solution 5 - AndroidPremkumar ManipillaiView Answer on Stackoverflow
Solution 6 - AndroidvenkatView Answer on Stackoverflow
Solution 7 - AndroidDaniel Gomez RicoView Answer on Stackoverflow
Solution 8 - AndroidWojtekView Answer on Stackoverflow