Search code examples
androidandroid-edittextsoft-keyboard

Android: soft keyboard Backspace on EditText not working


This is what happens: Activity A and B have an EditText and they both have IME_SEARCH. The input is done only via soft keyboard on a SAMSUNG tablet. On Activity A I can use the EditText without problems. The thing is that on Activity B I can't erase text with backspace after I hit 'space' or whenever I use a word from the suggestions. It behaves like there wasn't text there anymore. If I type new characters, I can erase them up to the space.

Important points:

  1. The View hierarchy that contains the EditTexts are identical
  2. The code that configures the IME_SEARCH processing (via setOnEditorActionListener) is identical
  3. The TextWatcher of both are also identical
  4. In the Manifest, both activities are configures with
    android:configChanges="keyboardHidden|keyboard|orientation"
    android:windowSoftInputMode="stateAlwaysHidden|adjustUnspecified"
    

I set a breakpoint on the method beforeTextChanged of both TextWatcher. I inserted a 'space' and hit 'backspace'. On the Edittext of activity A, the method is triggered but on activity B's it is not triggered. I can't see the reason for this to happen since the properties of both Edittext are configured identically. I also tried removing the IME option but the behavior kept the same.

Does anyone know what could be happening?

EDIT 1:

searchTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    btnClear.setVisibility(View.GONE);
                } else{
                    btnClear.setVisibility(View.VISIBLE);
                }
            }
        });

searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    buildGrid();
                    return true;
                }
                return false;
            }
        });

EDIT 2: The layout hierarchy is as following.

<LinearLayout
    ... >

    <include layout="@layout/title_bar" />
    <RelativeLayout
        ...>
        <EditText
            ...>

Solution

  • The problem was that, for some reason, the Activity B was overriding dispatchKeyEvent() and always returning true. Removing it solved the problem.