Search code examples
androidandroid-widget

android EditText maxLength allows user to type more


I've already set the maxLength attribute for an EditText widget in the xml layout file, and it does limit the display of characters.

But it still allows the user to press more letters on the keyboard, and backspacing deletes these extra presses rather than the last letter on the display.

I believe this is not how it's supposed to be. Pressing keys after the limit has been reached should stop accepting more input even if it is not displayed.

Any fix on this?

Update

Based on the answer below, for multiline EditText, add the textMultiLine attribute too.

android:inputType="textFilter|textMultiLine"

Solution

    1. Disable auto-suggestions(android:inputType="textFilter" or textNoSuggestions):
    2. Set maxLength

      <EditText
         android:id="@+id/editText"
         android:inputType="textFilter"
         android:maxLength="8"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
      

    I hope, it helps