Android EditText delete(backspace) key event Android EditText delete(backspace) key event android android

Android EditText delete(backspace) key event


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 very confusing! )

editText.setOnKeyListener(new 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        }        return false;           }});


It's been a while since you asked but I just had the same issue. As already mentioned by Estel the problem with key listeners is that they only work with hardware keyboards. To do this with an IME (soft keyboard), the solution is a bit more elaborate.

The single method we actually want to override is sendKeyEvent in the EditText's InputConnection class. This method is called when key events occur in an IME. But in order to override this, we need to implement a custom EditText which overrides the onCreateInputConnection method, wrapping the default InputConnection object in a proxy class! :|

Sounds complicated, but here's the simplest example I could contrive:

public class ZanyEditText extends EditText {    private Random r = new Random();    public ZanyEditText(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public ZanyEditText(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ZanyEditText(Context context) {        super(context);    }    public void setRandomBackgroundColor() {        setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r                .nextInt(256)));    }    @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) {                ZanyEditText.this.setRandomBackgroundColor();                // Un-comment if you wish to cancel the backspace:                // return false;            }            return super.sendKeyEvent(event);        }    }}

The line with the call to setRandomBackgroundColor is where my special backspace action occurs. In this case, changing the EditText's background colour.

If you're inflating this from XML remember to use the full package name as the tag:

<cc.buttfu.test.ZanyEditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/somefield"></cc.buttfu.test.ZanyEditText>


This is just an addition to Idris's answer, adding in the override to deleteSurroundingText as well. I found more info on that here: Android: Backspace in WebView/BaseInputConnection

package com.elavon.virtualmerchantmobile.utils;import java.util.Random;import android.content.Context;import android.graphics.Color;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;public class ZanyEditText extends EditText {    private Random r = new Random();    public ZanyEditText(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public ZanyEditText(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ZanyEditText(Context context) {        super(context);    }    public void setRandomBackgroundColor() {        setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r                .nextInt(256)));    }    @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) {                ZanyEditText.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);        }    }}