6
int maxLength = 20;
private String blockCharacterSet = "~#^|$%'&*!;";

private InputFilter filter = new InputFilter()
{

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {

        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};

Here only one filter is working either blockCharacterSet or max length:

EditText etname;
etname.setFilters(new InputFilter[] { filter });
etname.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});

Can anyone please help me to set above two filters at a time?

weston
  • 54,145
  • 21
  • 145
  • 203
veeraprasad
  • 61
  • 1
  • 6

1 Answers1

14

If you have two inputFilters, add it in array like below:

etname.setFilters(new InputFilter[] {
    new InputFilter.LengthFilter(maxLength), filter});

Finally the setFilter() takes array of input filters, so in the array you create in setFilters() should contain all the input filters.

Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32