Disable SPACE key in EditText android
Asked Answered
G

6

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.

Golda answered 28/7, 2016 at 10:32 Comment(18)
Didn't get your question. Is this (space working as backspace) the current behavior or the expected behavior?Hegyera
You can set input filters as like mentioned in this post #33993541Kaspar
@IshitaSinha agreePhlox
I have test on simulator (Nexus API 19) and it don't happendRespectively
@MujammilAhamed I tried that one also but still facing same problem.Golda
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. @IshitaSinhaGolda
@SaunikSingh your layout seems to be fine for me no issues with thatPhlox
@Phlox try this at your end you will get the actual problem.Golda
@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?Hegyera
@SaunikSingh Is there any manipulation of Edittext from your activity code??Kono
@IshitaSinha Lennovo A7000a & Xiomi MI4Golda
and what happens if you press any key other than the space key after reaching the 10-char limit?Hegyera
@sJy nothing manipulation in fragment v4 & Activity end regarding it. just as usual using setText if any value is available in variable.Golda
@IshitaSinha nothing happen after reaching 10 alphanumeric digits.except SPACE key pressGolda
@PhanVănLinh try to test in actual device not in emulator.Golda
@SaunikSingh in that case, try adding a space character to your android:digits attribute.Hegyera
@IshitaSinha my requirement doesn't match this criteria. I can't allow space in this edit text.Golda
This is the first time I encountered this and it drove me crazy. It seems like a bug in EditText. Other people 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.Hegyera
A
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 });
Aeneid answered 11/4, 2017 at 13:33 Comment(1)
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.Diplodocus
K
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
    }
}
Kaspar answered 28/7, 2016 at 11:21 Comment(4)
have you tried if I already mentioned android:digits. so Space will not be consider here.Golda
have you tried to add like this android:digits=" 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" in xmlKaspar
I tried like this <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" /> and afterTextChangedKaspar
i tried this when i press space key twice it deletes only last characterKaspar
S
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) {
                }
            });
Swatch answered 19/3, 2020 at 0:24 Comment(1)
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 answersCavan
T
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 });
Thaine answered 6/6, 2018 at 11:53 Comment(0)
H
0
    passwordEditText.filters = arrayOf(
        InputFilter { source, start, end, _, _, _ ->
            for (i in start until end) {
                if (Character.isWhitespace(source[i])) {
                    return@InputFilter ""
                }
            }
            null
        })
Hover answered 6/7, 2020 at 13:57 Comment(0)
P
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

            }
        });
Poundal answered 30/9, 2020 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.