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;
}
});
Detecting backspace on an empty EditText
Most reliable solution to detect any event or text change of an EditText is using TextWatcher but if EditText is already empty, you can not detect backspace/delete key event on all devices.
Please keep it in mind that it is not possible and hopefully you or any other will not waste your time.
Android - EditText delete (soft button) key event and deleting action
if(keyCode == KeyEvent.KEYCODE_DEL) {
Log.d("TAG", "OnKeyListener, premuto BACKSPACE");
backspacePressed = true;
return true; // This is the problem
}
By returning true, you are telling system that.. I'm going to handle that. Remove it and it will work.
Android - catch backspace (delete) button on custom EditText
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);
}
Detect delete button in soft keyboard
Possible duplicate of Android EditText delete(backspace) key event
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.
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;
}
});
Seems android documentation of EditText should be made more clear or at least any guide for EditText - Soft Keyboard interaction provided, because there many typical ones whose should be worked out by nearly every developer.
UPDATE:
Seems this way doesn't work on latest (at least after 4.1) Android versions. This answer seems to work on versions after 4.1.
Detect backspace in TextWatcher
A KeyListener
can fulfil both of your conditions.
mEditText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL){
//on backspace
}
return false
}
});
Similarly inside the onKey()
, you can put multiple check statements to check for the condition, when you would want to clear the textView
.
EDIT : As @RankoR was kind enough to point out, please bear in mind that onKeyListener()
works only for the hardware keyboards and not the soft keyboards.
Send backspace key event to edit text
From the Android developer docs:
public KeyEvent (int action, int code)
Create a new key event.
Parameters
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code
The first parameter should be an action code. In your case you should use ACTION_DOWN, because you want to simulate a keypress:
public static final int ACTION_DOWN
getAction() value: the key has been pressed down.
So this should work:
@Override
public void onClick(View v)
{
text.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}
Related Topics
How to Install Google Frameworks (Play, Accounts, etc.) on a Genymotion Virtual Device
Taking Picture from Camera Without Preview
How to Disable Mobile Data on Android
How to Change the Text on the Action Bar
How to Disable Bottomnavigationview Shift Mode
How to Get the Width and Height of an Android.Widget.Imageview
CSS Media Query - Soft-Keyboard Breaks CSS Orientation Rules - Alternative Solution
How to Have Onscrolllistener for a Scrollview
Android: How to Bind Spinner to Custom Object List
Change Value of R.String Programmatically
Launch Sms Application with an Intent
How to Keep Onitemselected from Firing Off on a Newly Instantiated Spinner
How to Lock/Unlock Screen Programmatically
Accessing Localhost:Port from Android Emulator
Convert View to Bitmap on Android