veeraprasad
veeraprasad

Reputation: 61

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?

Upvotes: 6

Views: 3605

Answers (1)

Akshay Bhat 'AB'
Akshay Bhat 'AB'

Reputation: 2700

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.

Upvotes: 14

Related Questions