How do you set the max number of characters for an EditText in Android?

How do you set the max number of characters for an Android EditText input? I see setMaxLines, setMaxEMS, but nothing for the number of characters.


Solution 1:

In XML you can add this line to the EditText View where 140 is the maximum number of characters:

android:maxLength="140"

Solution 2:

Dynamically:

editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_NUM) });

Via xml:

<EditText
    android:maxLength="@integer/max_edittext_length"