Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide UI when volume up down pressed in a dialog

Tags:

android

volume

Is there a way to hide the volume ui when the volume_up/volume_down key is pressed. I understand that it can be done with an activity when it doesnt seem to work when tiggered when a dialog is shown.

Is there an solution?

like image 675
sean Avatar asked Dec 25 '22 22:12

sean


1 Answers

This should work with the Dialog class:

AudioManager man = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
dialog.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                man.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                man.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
           default:
                return super.onKeyDown(keyCode, event);
        }
    }
});

Like at Android: Hide Volume change bar from device?

like image 99
Max Avatar answered Jan 08 '23 02:01

Max