Rectangle 27 0

android Detect delete button in soft keyboard?


private InputFilter filter = (charSequence, start, end, dest, dStart, dEnd) -> {

    if (end == 0 || dStart < dEnd) {
        // backspace was pressed! handle accordingly
    }

    return charSequence;
};

...

myEditText.setFilters(new InputFilter[] { filter });

@John Thanks for mentioning this. I updated the answer to correctly report. R.e. pressing delete on an empty edit text, is this happening on a real phone or emulator? If you're using your computer's 'backspace' to trigger deletes, you'll probably get unexpected results. Best to manually click the emulator's software buttons.

A simpler solution to this that I stumbled upon is using an InputFilter. InputFilter's filter() method appears to report all soft keyboard events - even those where the EditText's value isn't changing.

Backspace events can be evaluated using end, dStart, and dEnd. dStart will always be less than dEnd if a character was deleted. If the EditText is empty, you can still evaluate backspace presses by checking if end == 0.

Doesn't work. Tried this only gets called when u delete the last cha from edit text. But don't get it when user press on delete on empty edit text.

Note that bulk deletes will also be caught in this if statement, so you may want to do some extra checking withing filter(). Also note that if you're using your computer keyboard to type into EditTexts in emulators, you can get unexpected results. Best to click software buttons for testing.

Note
Rectangle 27 0

android Detect delete button in soft keyboard?


final EditText edit1 = (EditText) findViewById(R.id.editText1);

    edit1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // You can identify which key pressed buy checking keyCode value
            // with KeyEvent.KEYCODE_
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                // this is for backspace
                Log.e("IME_TEST", "DEL KEY");
            }
            return false;
        }
    });

Neither on 4.2.2 But it doesn't work with Android stock virtual keyboard. I also have Swiftkey keyboard and I'm able to receive KEYCODE_DEL on OnKeyListener.

This method works only for HW keyboard.

just checked the code from that question (which actually come from the provided question and answered by Labeeb P) with the test project with just two edits on layout and it seems to work just fine - I'm able to receive delete even if edit is empty.

strange, because then I've checked it (more than 6 months ago) it was working fine on nexus one device.

Note
Rectangle 27 0

android Detect delete button in soft keyboard?


I achieved it by overriding EditText in order to get access to InputConnection object which contains deleteSurroundingText method. It helps to detect deletion (backspace) event. Please, take a look at a solution I provided there: Android - cannot capture backspace/delete press in soft. keyboard

This solution works properly for both hardKeyboard and softKeyboard.

Note
Rectangle 27 0

android Detect delete button in soft keyboard?


final CustomEditText _EditText = new CustomEditText(_Context);

     _EditText.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_DEL) {
                            String _text=_EditText.getText().toString();
                            if(StringUtils.isBlank(_text))
                                 //EditText is now empty
                            }
                        }
                        return false;
                    }
                });
import java.util.Random;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.EditText;
/**
 * Created by mkallingal on 4/25/2016.
 */
public class CustomEditText extends EditText {

    private Random r = new Random();

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context) {
        super(context);
    }

    public void setRandomBackgroundColor() {

    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
                true);
    }

    private class ZanyInputConnection extends InputConnectionWrapper {

        public ZanyInputConnection(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                CustomEditText.this.setRandomBackgroundColor();
                // Un-comment if you wish to cancel the backspace:
                // return false;
            }
            return super.sendKeyEvent(event);
        }


        @Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
            if (beforeLength == 1 && afterLength == 0) {
                // backspace
                return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                        && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
            }

            return super.deleteSurroundingText(beforeLength, afterLength);
        }

    }
}

Now you can use it in your Activity like so:

Note
Rectangle 27 0

android Detect delete button in soft keyboard?


final EditText edit1 = (EditText) findViewById(R.id.editText1);

    edit1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // You can identify which key pressed buy checking keyCode value
            // with KeyEvent.KEYCODE_
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                // this is for backspace
                Log.e("IME_TEST", "DEL KEY");
            }
            return false;
        }
    });

Neither on 4.2.2 But it doesn't work with Android stock virtual keyboard. I also have Swiftkey keyboard and I'm able to receive KEYCODE_DEL on OnKeyListener.

This method works only for HW keyboard.

just checked the code from that question (which actually come from the provided question and answered by Labeeb P) with the test project with just two edits on layout and it seems to work just fine - I'm able to receive delete even if edit is empty.

strange, because then I've checked it (more than 6 months ago) it was working fine on nexus one device.

Note