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"
Unfortunately the following answer does not work. However, it helped us to find out that the bug mentioned by Poly Bug is not reproducible if the input begins with a number.
You can use a TextWatcher
to listen for EditText
changes and prevent the user from typing too many characters into the field. The following code works for me:
final int maxLength = 10;
EditText editText = (EditText) findViewById(R.id.my_edittext_id);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (s.length() >= maxLength) s.delete(maxLength, s.length());
}
});
In afterTextChanged
, the code compares the length of the inserted text with the maximal length maxLength
and deletes all superfluous characters.
You might replace all standard EditText
classes with the following class to use the listener in conjunction with the maxLength
XML attribute:
public class MyMaxLengthEditText extends EditText {
public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
private final int mMaxLength;
public MyMaxLengthEditText(Context context) {
super(context, null);
mMaxLength = -1;
}
public MyMaxLengthEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", -1);
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (s.length() >= mMaxLength) s.delete(mMaxLength, s.length());
}
});
}
}
You can use InputFilter
InputFilters can be attached to Editables to constrain the changes
that can be made to them.
Xml
<EditText
android:id="@+id/edit_Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java Class
int maxLengthofYourEditText = 5; // Set Your Limit
edit_TextObj.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofYourEditText)});
I also bumped into this situation when had a series of fields containing 1 symbol (a usual pin-code).
My silution is based on @jmeinke answer.
Create a layout containing several EditText
s with android:maxLength="2"
(we will enter 1 symbol in any of them and jump to another EditText
. Strange, but on some phones the code below works even for maxLength=1).
class YourTextWatcher(
private val prevEdit: EditText?,
private val currEdit: EditText,
private val nextEdit: EditText?,
private val method: () -> Unit
) : TextWatcher {
override fun afterTextChanged(s: Editable?) {
if (s.isNullOrEmpty()) {
prevEdit?.requestFocus()
} else {
if (s.length > 1) {
if (currEdit.selectionEnd > 1) {
// If stand on second position of EditText and enter new symbol,
// will move to next EditText copying second symbol.
val secondSymbol = s.substring(1, 2)
nextEdit?.setText(secondSymbol)
}
// Remove second symbol.
s.delete(1, s.length)
}
nextEdit?.requestFocus()
nextEdit?.setSelection(nextEdit.length(), nextEdit.length())
method()
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
}
Then for all EditText
s do following:
currEdit.addTextChangedListener(
YourTextWatcher(prevEdit, currEdit, nextEdit) { yourMethodToValidateText() })
Instead of prevEdit, currEdit, nextEdit set your EditText
controls. For instance, for the first:
edit1.addTextChangedListener(
YourTextWatcher(null, edit1, edit2) { yourMethodToValidateText() })
yourMethodToValidateText() is a method that will check entered symbols. For instance, you can check if all EditText
s are filled.
This solution has a bug when you try to press BackSpace in empty EditText
. Nothing happens, you won't go to a previous EditText
. You can catch BackSpace in View.OnKeyListener, but it can intersect with TextWatcher. Also I didn't check a situation when copy & paste a text to EditText
, forgot to check, what happens, when select a text in EditText
and press Delete.