1

Since setting EditText.setMaxlength or through the filters doesn't do anything on restricting the user from entering text to a specific length, I thought I will extend the EditText and handle myself but I am running into a Indexoutofbound exception.

Here is my code and I appreciate if anyone could point me to the right direction:

 
import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.widget.EditText;

public class EditTextExtended extends EditText
{
 protected int maxTextLength = Integer.MAX_VALUE;
 protected int minTextLength = Integer.MIN_VALUE;
 private Boolean isTextChanged;

 public EditTextExtended(Context context, AttributeSet attributeSet)
 {
  super(context, attributeSet);
 }

 public void setMaximumTextLength(int value)
 {
  maxTextLength = value;
 }

 public void setMinimumTextLength(int value)
 {
  minTextLength = value;
 }

 @Override
 protected void onTextChanged(CharSequence charSequence, int start, int before, int after)
 {
  Editable userEnteredText = this.getText();

  if (this.getText().length() > maxTextLength)
  {
   String maximumAllowedCharacters = userEnteredText.toString().substring(0, maxTextLength);

   if (!isTextChanged)
   {
    isTextChanged = true;
    this.setText(maximumAllowedCharacters);
   }
  }
  super.onTextChanged(charSequence, start, before, after);
 }

}

I have set the setMaximumTextLength to 10. So, when I type in 11th character it throws InvocationTrargetException with the IndexOutOfBoundsException as you see below: enter image description here

So, please give me some kind of clue on what the heck am I doing wrong. By the way, am on Android 5.0.1 build target API 21.

Vincy
  • 1,078
  • 1
  • 9
  • 16
  • Instead of overriding the `onTextChanged` you should override the `afterTextChanged` in your case – Panther Dec 18 '14 at 18:29
  • Yes, after reading a lot, just realized that in this method we cannot edit the text without crashing. So, we have to use TextWatcher to override afterTextChanged method. Thank you Panther! – Vincy Dec 23 '14 at 17:38

2 Answers2

1

Assuming your edittext is defined in a layout you can just do it there

android:maxLength="10" // your required length here

http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLength

mjstam
  • 1,049
  • 1
  • 6
  • 5
  • I know, but the android:maxLength="10" is no longer working as I mentioned in my post. I have also tried using the code setting the filters but user can still enter as many characters they want which would eventual break the back end during sync. It is the Android bug I believe introduced in 4.1 ... – Vincy Dec 18 '14 at 19:30
  • You can also do this with a TextWatcher and an addTextChangeListener – mjstam Dec 18 '14 at 21:40
  • Is it possible your change has not been applied to the field yet. If you used charSequence instead of userEnteredText.toString() – mjstam Dec 18 '14 at 21:54
1

First delete the android:maxLength="10" from .xml then do these:

  1. Set your minlength to some point, assume that is 10.

  2. Change maxLength whenever the input is longer than minlength.

*note the maxlenght will be defined programmatically or take a look at this topic click here

Community
  • 1
  • 1
Jedi Fighter
  • 377
  • 1
  • 4
  • 20