Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from inserting more characters after size limit is reached in EditText box [duplicate]

I want to prevent the user from inserting more letters than a certain amount to an alert dialog that contain an Edit-text instance. I try to use Input filter. Length Filter, but that only displays the string up to the requested length. The user can still write more characters, and when I try to delete characters for example, nothing happens until I delete enough characters for the word to be shorter than the limit (the like keyboard remembers the keys).

EDIT: To be more clear, I've already tried using InputFilter. That indeed enforces a string size that is passed to the EditText, but as I said I can still continue to write letters with the keyboard which are not displayed in the text box. When I hit delete, it deletes the extra letters first and only after enough letters have been deleted, I start to see the letters in the text box deleting. My requested scenario: set the string limit to 10. Hit 15 characters in my keyboard. Then hit backspace and see the last letter in the text box get deleted. I hope this is more clear now.

Can someone help?

like image 996
izikgo Avatar asked Mar 26 '14 12:03

izikgo


2 Answers

Probably it's to late but if someone have the same problem.

In layout:

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

Programatically:

setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
like image 92
wrozwad Avatar answered Oct 18 '22 15:10

wrozwad


You can use following propery of EditText in AndroidManifest.xml

android:maxLength="8"

Now user can not add more than 8 characters in the EditText.

like image 39
Lucifer Avatar answered Oct 18 '22 16:10

Lucifer