I want to finish an activity when the soft keyboard is open, I want to override the back event of the soft keyboard and finish the activity.
I'm using this but it's not working , any Idea ?
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
this.finish();
return false;
}
return super.dispatchKeyEvent(event);
}
onKeyDown()
andonBackPressed()
doesn't work for this case. You have to useonKeyPreIme
.Initially, you have to create custom edit text that extends
EditText
. And then you have to implementonKeyPreIme
method which controlsKeyEvent.KEYCODE_BACK
. After this, one back press enough for solve your problem. This solution works for me perfectly.CustomEditText.java
In your XML
In your Activity