Bytecode
Bytecode

Reputation: 6591

Soft Keyboard hide on backpress

I need to identify the event of back press which hide the softkeyboard
I have tested by override following methods

  1. onKeydown
  2. onBackPressed
  3. onConfigurationChanged
  4. dispatchKeyEvent

But the controller is not reaching there

Upvotes: 1

Views: 3321

Answers (2)

RatherBeSailing
RatherBeSailing

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

Ludevik
Ludevik

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

Related Questions