Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable SPACE key in EditText android

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.

like image 616
Saunik Singh Avatar asked Jul 28 '16 10:07

Saunik Singh


4 Answers

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 });
like image 67
Anjali-Systematix Avatar answered Oct 02 '22 19:10

Anjali-Systematix


Just allow space in your edittext and replace space with empty,

    @Override
    public void afterTextChanged(Editable s) {
    String result = s.toString().replaceAll(" ", "");
    if (!s.toString().equals(result)) {
         ed.setText(result);
         ed.setSelection(result.length());
         // alert the user
    }
}
like image 21
Mujammil Ahamed Avatar answered Oct 02 '22 19:10

Mujammil Ahamed


   EditText editPassword = findViewById(R.id.et_Rpassword);
            editPassword.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if(charSequence.length() > 0){
                    if((charSequence.charAt(i)+"").equalsIgnoreCase(" ")){
                        Toast.makeText(RegisterActivity.this, "you'r can't enter Space", Toast.LENGTH_LONG).show();
                        String oldPass = editPassword.getText().toString();
                        editPassword.setText(oldPass.replace(" ",""));
                    }
                }}
                @Override
                public void afterTextChanged(Editable editable) {
                }
            });
like image 33
ABD ALAZEZ Avatar answered Oct 02 '22 20:10

ABD ALAZEZ


private InputFilter filter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {


        if(source.equals(" ")){
            int startSelection=editTextView.getSelectionStart();
            int endSelection=editTextView.getSelectionEnd();
            editTextView.setText(editTextView.getText().toString().trim());
            editTextView.setSelection(startSelection,endSelection);

        }

        return null;
    }
};

editTextView.setFilters(new InputFilter[] { filter });
like image 33
Jobin_vibes Avatar answered Oct 02 '22 20:10

Jobin_vibes