Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can restrict my EditText input to some special character like backslash(/),tild(~) etc by soft keyboard in android programmatically

I am developing an application for keyboard, but i am geting an issue. I want to restrict/block some special character from soft keyboard in EditText in android programmatically.

So, Is there any way i can restrict any special character input in edit text in android.

If anyone have idea,Please reply.

Thanks in advance.

like image 859
Jagdish Avatar asked Feb 17 '14 11:02

Jagdish


3 Answers

Try this may work for you

public class MainActivity extends Activity {

    private EditText editText;
    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;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        editText.setFilters(new InputFilter[] { filter });
    }

}
like image 93
Biraj Zalavadia Avatar answered Nov 14 '22 06:11

Biraj Zalavadia


If you want to add spaces you can give space after the last digit.

  android:digits="0123456789qwertzuiopasdfghjklyxcvbnm "
like image 27
Thirupathi Yadav Avatar answered Nov 14 '22 07:11

Thirupathi Yadav


This should work:

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter});

Or if you prefer the easy way:

<EditText android:inputType="text" android:digits="0123456789*,qwertzuiopasdfghjklyxcvbnm" />
like image 30
Solenya Avatar answered Nov 14 '22 06:11

Solenya