Mohammad Rababah
Mohammad Rababah

Reputation: 1726

Disable key volume up/down while running dialog

Is there a way to disable" key volume up/down" while running dialog box??!!

AlertDialog.Builder builder1 = new AlertDialog.Builder(thisActivity);
builder1.setMessage("Flight mode is ON");
builder1.setCancelable(true);
builder1.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
}
});
AlertDialog alert11 = builder1.create();
alert11.show();

When dialog appears ---> user can't use key volume up/down

Upvotes: 0

Views: 911

Answers (2)

SMR
SMR

Reputation: 6736

Capture the key events but dont do anything:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                //do nothing
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                //do nothing
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93678

Override the onKeyDown function in your activity. If the dialog is up and you receive a volume key event, ignore it (do not call super.onKeyDown in those cases).

Upvotes: 1

Related Questions