9

I know there are a couple of questions already been asked, but I have a little different query.

How do we put a limit on the maximum number of characters to be inputted in EditText? Whether I do this

android:maxLength="7"

or this

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

I can still type into the text field, though it's not visible and not taken into account. If I keep typing, the input becomes invisible after the limit, but the test-suggestions keep building for higher number of letters.

Suppose my limit is 7 and I typed 10 letters, then I would only see the first 7 letters, but text-suggestions will show 10 letter words. Furthermore, I need to press backspace 3 times to actually start deleting letters from my shown 7-letter word.

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

PS: Needless to say, but though I don't want input to be inputted after 7 letters, I would allow delete or backspace.

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
  • **Suppose my limit is 7 and I typed 10 letters, then I would only see the first 7 letters, but text-suggestions will show 10 letter words. Furthermore, I need to press backspace 3 times to actually start deleting letters from my shown 7-letter word** - True. I tried the same and found the exact case – Narendra Singh Jan 29 '16 at 16:38

4 Answers4

17

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

 android:inputType="textFilter"

I tried the same and it worked for me.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
7

try this,

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

OR

edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

OR

android:inputType="textFilter"
Madhur
  • 3,303
  • 20
  • 29
  • Thanks for the answer, but my concern is not about suggestions. It's mainly about stopping the input. – Akeshwar Jha Jan 29 '16 at 16:28
  • yup thats a bug so once you modified the input type maxLength would work perfectly, preventing users from typing more buffer. – Madhur Jan 29 '16 at 16:30
  • look out for this http://stackoverflow.com/questions/12083183/android-edittext-max-length/19222238#19222238 – Madhur Jan 29 '16 at 16:31
3

This finally works for me:

edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Wojtek
  • 1,210
  • 3
  • 18
  • 23
-1

As @Madhur sugested, in your EditText xml add next line:

Documentation

android:maxLength="7"
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • Thanks, but that's already what I'm doing rightnow. I need help with stopping the input as I've mentioned in my question. – Akeshwar Jha Jan 29 '16 at 16:30
  • Input should be stoped after that line, you can input onlu from 0-6 chars in your edit text. – Stanojkovic Jan 29 '16 at 16:34
  • 1
    **Suppose my limit is 7 and I typed 10 letters, then I would only see the first 7 letters, but text-suggestions will show 10 letter words. Furthermore, I need to press backspace 3 times to actually start deleting letters from my shown 7-letter word**. See what the OP is saying – Narendra Singh Jan 29 '16 at 16:49