get EditText Max Length - Android User Interface

Android examples for User Interface:EditText

Description

get EditText Max Length

Demo Code


//package com.java2s;
import android.text.InputFilter;
import android.widget.EditText;
import java.lang.reflect.Field;

public class Main {

    public static int getMaxLength(EditText view) {
        int length = 0;
        try {/*from w w w. ja  va2s .c  o  m*/
            InputFilter[] inputFilters = view.getFilters();
            for (InputFilter filter : inputFilters) {
                Class<?> c = filter.getClass();
                if (c.getName().equals(
                        "android.text.InputFilter$LengthFilter")) {
                    Field[] f = c.getDeclaredFields();
                    for (Field field : f) {
                        if (field.getName().equals("mMax")) {
                            field.setAccessible(true);
                            length = (Integer) field.get(filter);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }
}

Related Tutorials