Reputation: 6591
I need to identify the event of back press which hide the softkeyboard
I have tested by override following methods
But the controller is not reaching there
Upvotes: 1
Views: 3321
Reputation: 311
An update to @Ludevik's answer
Firstly suggest overriding onKeyDown()
Secondly if the key press has been handled then return true not super.onKeyDown()
Updated code (in Kotlin):
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
// if recognise the keyCode then process
return if (keyCode == desiredKeyCode) {
// do what you need to do
true // key press handled
} else {
super.onKeyDown(keyCode, event)
}
}
As for closing the soft key board - My experience is that can prove problematic (leading to unexpected consequences)
Upvotes: 0
Reputation: 7254
Use dispatchKeyEventPreIme in subclassed EditText view:
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
//do what you need here
}
return super.dispatchKeyEventPreIme(event);
}
Upvotes: 2