Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android use text watcher to prevent typing of special characters [duplicate]

I want to make an EditText box and use TextWatcher to prevent typing of special characters.

like image 305
Sourav301 Avatar asked Jun 24 '12 16:06

Sourav301


1 Answers

This is for only character and space validation...

InputFilter filter = new InputFilter()     
{  
    @Override  
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)   
    {   
        for (int i = start; i < end; i++)
            if (!Character.isLetter(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i)))
                return "";

        return null;  
    }
};

myTextView.setFilters(new InputFilter[] { filter });
like image 174
Dipak Sonnar Avatar answered Oct 19 '22 12:10

Dipak Sonnar