94

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?

Jon Wei
  • 1,481
  • 2
  • 10
  • 18

8 Answers8

108

Possible duplicate of What's the best way to limit text length of EditText in Android

Use android:maxLength="140"

That should work. :)

starball
  • 20,030
  • 7
  • 43
  • 238
Ushal Naidoo
  • 2,704
  • 1
  • 24
  • 37
  • 2
    I have same problem but this is not work for me – Mansi Jan 23 '13 at 07:16
  • What's happening in yours? ask a new question and put up your code in it and I will have a look. put a link to your question here so I can find it – Ushal Naidoo Jan 23 '13 at 23:23
  • Same problem ! Is there a solution ? – mrroboaat Oct 07 '13 at 10:06
  • 8
    I don't think this is a duplicate. Can anyone explain why this would work versus using an app constant? Seems like having the char limit as a value in xml shouldn't change how the text buffer works. – Elliott Jan 13 '14 at 19:48
45

I had the same problem.

Here is a workaround

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

Thx to How can I turnoff suggestions in EditText?

Community
  • 1
  • 1
mrroboaat
  • 5,602
  • 7
  • 37
  • 65
  • That is funny that modifying the inputType allows maxLength to work properly, preventing users from typing more buffer. I wish Google fix this. – Steve Ham Mar 03 '15 at 12:31
  • 1
    It looks like only textNoSuggestions is needed to delete letters when pressing backkey. – Steve Ham Mar 05 '15 at 05:28
  • 1
    This is strange, wonder if google knows of this bug! – Skynet Aug 14 '15 at 11:03
  • 2
    this should be the accepted answer, limiting the maxLength only doesn't do it – Nactus Sep 29 '15 at 02:14
  • You have no control over the software keyboard. The effect of various features and flags on the software keyboard is implementation-dependent. You only control the EditText itself. – j__m Nov 11 '15 at 00:02
  • This is the correct answer! The buffer doesn't keep filling after the "maxLength" limit has been reached! – Martin Konecny Nov 11 '15 at 03:42
  • 5
    Hi, I'm the guy that posted the "Accepted" answer. I agree with @Nactus. This should be the accepted answer. My answer doesn't solve the problem completely. – Ushal Naidoo May 26 '16 at 22:20
  • nice answer... working good. – venkat Sep 01 '16 at 09:35
  • @SeungUnHam Actually for me it's the other way around. I only needed `textVisiblePassword` to make it work. However, `textCapWords` has no effect now on some phones.. – Alaa M. Dec 18 '16 at 19:51
  • 4
    This is the workaround to use when you don't need suggestions. However, if you need both the length limitation and suggestions this doesn't work. This issue exists since pretty much the inception of Android and Google doesn't care about fixing it. Sad.. – Dennis K Jul 28 '17 at 17:26
  • @DennisK, I suppose my ugly solution can help: https://stackoverflow.com/a/58372842/2914140. – CoolMind Dec 09 '19 at 16:42
18
EditText editText= ....;
InputFilter[] fa= new InputFilter[1];
fa[0] = new InputFilter.LengthFilter(8);
editText.setFilters(fa);
Samira
  • 844
  • 13
  • 17
  • 2
    Single line: `editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) });` – Basil Sep 30 '19 at 22:29
7

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

android:inputType="textFilter"

to your EditText.

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
6

You may try this

 EditText et = (EditText) findViewById(R.id.myeditText);
    et.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(140) }); // maximum length is 140
Premkumar Manipillai
  • 2,121
  • 23
  • 24
2

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.

venkat
  • 157
  • 3
  • 15
1

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.

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
0

For me this solution works:

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