Disable SPACE key in EditText android

Saunik Singh picture Saunik Singh · Jul 28, 2016 · Viewed 9.5k times · Source

I'd created EditText with following.

<EditText
        android:id="@+id/et_regis_num"
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:digits="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:hint="@string/txt_reg_num"
        android:inputType="textCapCharacters"
        android:maxLength="10" />

in this edittext I don't want to press SPACE key but when I'm pressing SPACE key it's working as BACKSPACE key. means it's deleting one character in each twice press.

Answer

Anjali-Systematix picture Anjali-Systematix · Apr 11, 2017

Set InputFilter on EditText. Please check below answer it's worked for me.

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.isWhitespace(source.charAt(i))) {
                return "";
            }
        }
        return null;
    }
};

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