Remove maxLength set from EditText

1.9k views Asked by At

As we know, maxLength for EditText is set by:

android:maxLength="19"

or programmatically. My question is that how can I remove this maxLength programmatically if it was set to certain length? I want to set maxLength to the maximum length possible.

5

There are 5 answers

1
Onix On

Just try to set a maxLength property to -1

android:maxLength="-1"

or programmatically

int maxLengthofEditText = -1
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});
0
Alesandro Giordano On

In android there is EditText and sometimes you may want to limit the character input. EditText in XML layout would give you android:maxLength to do this thing but in codes you might wonder why there isn't any setMaxLength function. The reason behind this is that when you want to restrict the EditText to accept certain value, you have to filter them and this would be invoke by setFilters and thus to make our EditText to have a fixed size we shall.

EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);
0
Divyesh Rudani On

Just try to set empty InputFilter Array for remove the Max Length.

etEmail.filters = arrayOf<InputFilter>()
0
Mohammad Reza On

You can also add this bellow code to your ext file and use it away :-)

fun styleLimitText(text: String, length: Int): String {
    if (text.length > length) return text.substring(0, length) + "..."
    return text
}
0
Abhi Plx On

In Java

edittext.setFilters(new InputFilter[] { });