Buda Gavril Buda Gavril - 1 year ago 615
Android Question

Android EditText delete(backspace) key event

How can I detect delete (backspace) key event for a editText? I've tried using TextWatcher, but when the editText is empty, when I press delete key, nothing happens. I want to detect delete key press foe an editText even if it has no text.

Answer Source

NOTE:onKeyListener doesn't work for soft keyboards

You can set OnKeyListener for you editText so you can detect any key press
EDIT: A common mistake we are checking KeyEvent.KEYCODE_BACK for backspace, but really it is KeyEvent.KEYCODE_DEL (Really that name is confusioning! )

    editText.setOnKeyListener(new OnKeyListener() {                 
        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
        return false;       
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download