6

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.

Linh
  • 57,942
  • 23
  • 262
  • 279
Saunik Singh
  • 996
  • 1
  • 9
  • 19
  • 2
    Didn't get your question. Is this (space working as backspace) the current behavior or the expected behavior? – Ishita Sinha Jul 28 '16 at 10:37
  • You can set input filters as like mentioned in this post http://stackoverflow.com/questions/33993041/android-disable-space-only-for-edittext – Mujammil Ahamed Jul 28 '16 at 10:37
  • @IshitaSinha agree – eLemEnt Jul 28 '16 at 10:39
  • I have test on simulator (Nexus API 19) and it don't happend – Linh Jul 28 '16 at 10:40
  • @MujammilAhamed I tried that one also but still facing same problem. – Saunik Singh Jul 28 '16 at 10:40
  • actually when I'm pressing space button from keyboard after filling the edit =text till 10 digits. after pressing space button it's deleting one bye one character from last. working as BACKSPACE key. @IshitaSinha – Saunik Singh Jul 28 '16 at 10:42
  • @SaunikSingh your layout seems to be fine for me no issues with that – eLemEnt Jul 28 '16 at 10:44
  • @eLemEnt try this at your end you will get the actual problem. – Saunik Singh Jul 28 '16 at 10:47
  • @SaunikSingh You have specified `android:maxLength="10"` and `android:digits="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"`. What is the behavior if you try pressing any key other than space after the 10-character limit? Which devices have you tried this on? – Ishita Sinha Jul 28 '16 at 10:47
  • @SaunikSingh Is there any manipulation of Edittext from your activity code?? – Sujay Jul 28 '16 at 10:47
  • @IshitaSinha Lennovo A7000a & Xiomi MI4 – Saunik Singh Jul 28 '16 at 10:48
  • and what happens if you press any key other than the space key after reaching the 10-char limit? – Ishita Sinha Jul 28 '16 at 10:50
  • @sJy nothing manipulation in fragment v4 & Activity end regarding it. just as usual using setText if any value is available in variable. – Saunik Singh Jul 28 '16 at 10:50
  • @IshitaSinha nothing happen after reaching 10 alphanumeric digits.except SPACE key press – Saunik Singh Jul 28 '16 at 10:52
  • @PhanVănLinh try to test in actual device not in emulator. – Saunik Singh Jul 28 '16 at 10:52
  • @SaunikSingh in that case, try adding a space character to your `android:digits` attribute. – Ishita Sinha Jul 28 '16 at 10:54
  • @IshitaSinha my requirement doesn't match this criteria. I can't allow space in this edit text. – Saunik Singh Jul 28 '16 at 10:58
  • This is the first time I encountered this and it drove me crazy. It seems like a bug in `EditText`. [Other people](http://stackoverflow.com/questions/36445783/android-space-bar-is-not-working) seem to have encountered this as well, with no solution. Your best bet seems to remove `android:digits` from the xml and handle the input programmatically with a regex or something. Also, this only happens with the soft keyboard. With the hard keyboard, the `EditText` doesn't register the space key. – Ishita Sinha Jul 28 '16 at 11:39

6 Answers6

10

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 });
TofferJ
  • 4,678
  • 1
  • 37
  • 49
Anjali-Systematix
  • 1,531
  • 14
  • 17
  • This approach has very strange side effect (if suggestions are enabled for this EditText) - tapping space triggers like backspace. 1st entered space in the end of EditText really filters out, but next space tappings works like backspace erasing entered text which is totally weird. To avoid this side effect you could use inputType="textVisiblePassword" - with this workaround it will work. – Stan Nov 10 '17 at 08:10
4

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
    }
}
Mujammil Ahamed
  • 1,454
  • 4
  • 24
  • 50
2
   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) {
                }
            });
ABD ALAZEZ
  • 21
  • 2
  • Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/) – Calos Mar 19 '20 at 01:05
1
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 });
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
0
    passwordEditText.filters = arrayOf(
        InputFilter { source, start, end, _, _, _ ->
            for (i in start until end) {
                if (Character.isWhitespace(source[i])) {
                    return@InputFilter ""
                }
            }
            null
        })
0
 edittxt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                String str = s.toString();
                if (str.equalsIgnoreCase(" ")||str.equalsIgnoreCase("  ")) {
                    // orderdata.setError("Space is not allowed");
                    edittxt.setText("");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
iffu
  • 331
  • 1
  • 4
  • 16