How to stop accepting input after the length of the EditText crosses the limit

I know that a few questions have already been asked, but I have a slightly different request.

How to set a limit on the maximum number of characters entered in EditText ? I'm doing it

 android:maxLength="7" 

or

 ((EditText)findViewById(R.id.zip)).setFilters(new InputFilter[] { new InputFilter.LengthFilter(7) }); 

I can still enter a text box, although it is not visible and not taken into account. If I continue to type, the input becomes invisible after the limit, but test-suggestions are built for more letters.

Suppose my limit is 7, and I typed 10 letters, then I will see only the first 7 letters, but text sentences will show 10 letter words. In addition, I need to press 3 times back to actually start deleting letters from my 7-letter word.

So my question is: is there a way to stop accepting input after a certain character length?

PS : Needless to say, but although I do not want to enter input after 7 letters, I would allow deletion or backspace.

+7
source share
5 answers

Just add this to your EditText. This will definitely work.

  android:inputType="textFilter" 

I tried the same thing and it worked for me.

+14
source

try it,

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

OR

 edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 

OR

 android:inputType="textFilter" 
+4
source
 android:inputType="textFilter" 
+2
source

This finally works for me:

 edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 
+1
source

As @Madhur said, in your EditText text editor, add the following line:

Documentation

 android:maxLength="7" 
-1
source

Source: https://habr.com/ru/post/1241830/


All Articles