I want to set a 1 character length in EditText but just in one part of condition. There is a way to set max length via XML but this will be applied for the whole condition. How to set max length programmatically?
public void setEditTextMaxLength(EditText editText, int length) {
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(length);
editText.setFilters(FilterArray);
}
You can add this in your XML:
android:maxLength="10"
or programmatically:
EditText editText = new EditText(this);
int maxLength = 3;
editText.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(maxLength)
});
In Kotlin, this extension function is handy:
fun EditText.setMaxLength(maxLength: Int) {
filters = arrayOf(InputFilter.LengthFilter(maxLength))
}
edtTime.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(4) });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With