Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple InputFilters on EditText?

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?

like image 240
veeraprasad Avatar asked Dec 08 '16 13:12

veeraprasad


1 Answers

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.

like image 160
Akshay Bhat 'AB' Avatar answered Sep 25 '22 15:09

Akshay Bhat 'AB'