9

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?

izikgo
  • 479
  • 7
  • 12
  • I know this is a year old, but I'm trying to figure out if there's anything that can be done in this situation as well. I'm guessing there isn't, because each keyboard handles that preview display itself, but maybe there's something. Definitely seems like a weird UX because of the disparity between the preview and what's actually in the EditText. – codebaum Mar 30 '15 at 16:33
  • 3
    Does this answer your question? [What's the best way to limit text length of EditText in Android](https://stackoverflow.com/questions/3285412/whats-the-best-way-to-limit-text-length-of-edittext-in-android) – Josh Correia Jul 16 '20 at 19:08

5 Answers5

5

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);
wrozwad
  • 2,610
  • 1
  • 30
  • 38
4

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

You can set this property also at runtime by assigning

youredittext.setMaxLength(8);

You told that you have your edit-text inside alert dialog box so set this at runtime and give a try.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
0

SEE THIS

TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(filterArray);
Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108
0

See this answer for a partial workaround: https://stackoverflow.com/a/19222238/933656

You will be able to immediately delete the last character in the EditText because there is no preview for the keyboard to show (assuming all third-party keyboards respect the textNoSuggestions flag). The only problem is that you lose the suggestion text.

Community
  • 1
  • 1
codebaum
  • 103
  • 6