2

I am showing an AlertDialog in my app and even though I have set it to be non-cancelable, it gets cancelled when the hardware search button is pressed.

I tried overriding the keyDown method and detecting the key press and also the onSearchRequested method. But both of them do not work for the first time the button is pressed though they work after that.

Any ideas?

PS: I know this is a repeated question.... but nobody replied to it, that's why re-posting :)

Community
  • 1
  • 1
mvrck
  • 512
  • 1
  • 4
  • 18

1 Answers1

4

You should be able to do it with an OnKeyListener. In this case, only DPAD keys are allowed.

return new AlertDialog.Builder(this)
    .setTitle("Title")
    .setMessage("Dialog message")
    .setCancelable(false)
    .setOnKeyListener(new DialogInterface.OnKeyListener() {

        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            //whitelist allowed keys - allow navigation keys only
            if (keyCode < KeyEvent.KEYCODE_DPAD_UP || keyCode > KeyEvent.KEYCODE_DPAD_CENTER) {
                return true;
            }
            return false;
        }
    })
.create();
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132