Skip to content Skip to sidebar Skip to footer

How To Get Edittext Maxlength Setting In Code

I would like to see the maxLength of an EditText at run time to be able to make a text display decision. Is that possible? Here is a description of what I wan't to do. I have a Lis

Solution 1:

Only limited parameters have their getters, so I don't think you can read it .

So write length (Say 12) in values folder and use it in xml layout and arrayAdapter . Now its not hard-coded .

1)Create integer.xml in values *

<?xml version="1.0" encoding="utf-8"?><resources><itemtype="integer"name="max_length">12</item></resources>

2)In layout

<TextView  android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:maxLength="@integer/max_length"
    />

3) in ArrayAdapter :

int maxLength = getResources().getInteger(R.integer.max_length);

Solution 2:

This should work:

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

Solution 3:

From api 21 you can do it like that:

for (InputFilter filter : mEditText.getFilters()) {
        if (filter instanceof InputFilter.LengthFilter) {
            ((InputFilter.LengthFilter) filter).getMax());
        }
}

I hope this helps someone.

Solution 4:

extend the edit text and retrieve the value from the attributeset in the constructor.

public class MyEditText extends EditText {

publicstaticfinalStringXML_NAMESPACE_ANDROID="http://schemas.android.com/apk/res/android";
privateint mMaxLength;

publicMyEditText(Context context) {
    super(context, null);
}

publicMyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", -1);
}

Solution 5:

You can get the Field value using the Reflection API.

Why You Shouldn't Do It


Just about everyone would advocate against it (including me) because:

  • It's slow
  • It's implementation-dependant
  • It's not intended to be accessed (obviously)

As of now, looking at the source code (Android API 19), the implementation depends on an InputFilter.LengthFilter which is set in the constructor as:

if (maxlength >= 0) {
    setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) });
} else {
    setFilters(NO_FILTERS);
}

where maxLength is the Integer you're interested in finding, parsed from the xml attribute (android:maxLength="@integer/max_length"). This InputFilter.LengthFilter has only one field (private int mMax) and no accessor method.

How It Can Be Done


  • Declare a static method in a relevant utility class accepting a TextView and returning an int.
  • Iterate over each InputFilter set on the TextView and find one belonging to the InputFilter.LengthFilter implementation.
  • Access, get and return the mMax field's value using Reflection.

This would give you something like this:

import java.lang.reflect.Field;

// [...]publicstaticintgetMaxLengthForTextView(TextView textView)
{
    intmaxLength= -1;

    for (InputFilter filter : textView.getFilters()) {
        if (filter instanceof InputFilter.LengthFilter) {
            try {
                FieldmaxLengthField= filter.getClass().getDeclaredField("mMax");
                maxLengthField.setAccessible(true);

                if (maxLengthField.isAccessible()) {
                    maxLength = maxLengthField.getInt(filter);
                }
            } catch (IllegalAccessException e) {
                Log.w(filter.getClass().getName(), e);
            } catch (IllegalArgumentException e) {
                Log.w(filter.getClass().getName(), e);
            } catch (NoSuchFieldException e) {
                Log.w(filter.getClass().getName(), e);
            } // if an Exception is thrown, Log it and return -1
        }
    }

return maxLength;
}

As mentioned earlier, this will break if the implementation that sets the maximum length of the TextView changes. You will be notified of this change when the method starts throwing. Even then, the method still returns -1, which you should be handling as unlimited length.

Post a Comment for "How To Get Edittext Maxlength Setting In Code"