3

I want to catch the backspace/delete button event in soft keyboard on EditText custom.

How can I do it ?

I tried these solutions but they do not work for me :

Android custom EditText and back button override

EditText OnKeyDown

Get back key event on EditText

Android EditText delete(backspace) key event

Thanks for your help !


EDIT : I found a fix for that isssue with the function DispatchKeyEvent :

@Override public boolean dispatchKeyEvent(@NonNull KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_DOWN
        && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
     // ... 
    }
    return super.dispatchKeyEvent(event);
}
Community
  • 1
  • 1
Stéphane G.
  • 127
  • 1
  • 15
  • What do you mean by "do not work"? Where are you testing? In an emulator or on a device? – airowe Oct 06 '15 at 17:32
  • When I pressed any button in the soft keyboard, the code in the function is not executed (outside of all conditions). I tested with my device (Samsung galaxy S5). – Stéphane G. Oct 06 '15 at 17:38
  • Checking the docs for the [KeyEvent class](http://developer.android.com/reference/android/view/KeyEvent.html), it states catching soft input is unreliable. You may have better luck overriding `onBackPressed()` in your `Activity` class and doing what you need to do in there. **Edit:** Unless of course you mean "move cursor one space back" kind of back press rather than the standard back navigation key. – PPartisan Oct 06 '15 at 17:38
  • @PPartisan onBackPressed will not be helpful when soft input is open – Rahul Tiwari Oct 06 '15 at 17:42
  • @PPartisan I want the standard back button for deleting a character in my edittext, but with my rules – Stéphane G. Oct 06 '15 at 17:49
  • @nolimitee, please take a look at a solution I provided below (http://stackoverflow.com/a/34858233/1808829). – Ayaz Alifov Jan 18 '16 at 15:24

4 Answers4

3

I found a fix for that isssue with the function DispatchKeyEvent :

@Override public boolean dispatchKeyEvent(@NonNull KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_DOWN
        && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
        // ... 
    }
    return super.dispatchKeyEvent(event);
}
Stéphane G.
  • 127
  • 1
  • 15
0

Catching Key Events from soft input methods in Android is unreliable. Here is an excerpt from the JavaDoc for the KeyEvent class:

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier.

One workaround could involve using TextWatcher. Whenever a delete occurs in an EditText, the character count drops.

myEditText.addTextChangedListener(this);
//...
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (count == 0) {
        //text was deleted. 
    }
}

Edit: This is untested, so may not be completely reliable. In addition, it's worth pointing out that count may also read 0 if a long section of text is selected and then replaced. I created a library once that was designed to add Undo/Redo functionality to EditText, but can also identify the difference between text being replaced and deleted, so this may be of use to you if you require more accuracy.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
0

Please, take a look at a solution I provided in another similar topic: Android - cannot capture backspace/delete press in soft. keyboard. You need to build your custom EditText class and override onCreateInputConnection method in it. It will give you access to deleteSurroundingText event which is firing when you click backspace. I've tested it in some devices and hope it will work in others also. Try it and give a feedback.

Community
  • 1
  • 1
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
0

Here you go..

editText.addTextChangedListener(new TextWatcher() {

            private int before;
            private int after;
            private int prevLength;
            private boolean isBackPressed;
            ArrayList<Integer> arrayList;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                prevLength = charSequence.length();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int end, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                isBackPressed = prevLength > editable.length();

                if(isBackPressed){
                    Toast.makeText(getApplicationContext(),"Back",Toast.LENGTH_LONG).show();
                }
            }
        });
Flutterian
  • 1,761
  • 1
  • 21
  • 47