2

I'm showing a non-cancelable dialog in my application, but it gets cancelled if the user presses SEARCH button. I've tried to override onSearchRequested and onKeyDown, but it doesn't help. Any suggestion?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Flávio Faria
  • 6,575
  • 3
  • 39
  • 59

2 Answers2

3

I also came across this problem and Jamasan's solution did not work for me. I instead added the following code to my custom dialog class (extending Dialog):

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        return true;
    } else {
        return false;
    }
}

keyCode and KeyEvent.KEYCODE_SEARCH are both int. The docs for onKeyDown says

If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

Works for me.

Stephen Blair
  • 369
  • 2
  • 14
0

Override the Activity's onKeyDown event, and check for KEYCODE_SEARCH to return false

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    char c = (char) event.getUnicodeChar();

    if (c == KeyEvent.KEYCODE_SEARCH) {
        return false;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}

Returning false just blocks that key press (as if it didn't happen). Otherwise running super.onKeyDown(..) just processes it regularly.

Good luck.

pjama
  • 3,014
  • 3
  • 26
  • 27