5

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?

sean
  • 717
  • 2
  • 9
  • 23
  • What do you mean by Volume UI? Do you mean the Volume level dialog in the above of the screen? – Sami Eltamawy Aug 20 '13 at 12:28
  • This is a good question what volume UI are you talking about? The answer isn't that important as weather you can access the method that calls it and closes it. – Mihai Bratulescu Aug 20 '13 at 12:31
  • Similar to this? http://stackoverflow.com/questions/14563208/android-hide-volume-change-bar-from-device – Max Aug 20 '13 at 12:33
  • @Max Yes stackoverflow.com/questions/14563208/ is what i am referring to . The post from says that it is not possible. Is it still impossible ? – sean Aug 20 '13 at 12:36
  • @Abd El-Rahman El-Tamawy I am referring to the volume ui that is shown when the hardware volume is pressed. – sean Aug 20 '13 at 12:37
  • @sean There is an onKeyDown method in the class Dialog so you can try to implement it there. – Max Aug 20 '13 at 12:39
  • @ Max i tried overriding the volume_up and volume_down key but the ui slider is still shown. – sean Aug 20 '13 at 13:05

2 Answers2

7

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?

Community
  • 1
  • 1
Max
  • 292
  • 2
  • 7
  • Hi max , i tried this but the ui slider is still shown . The only different is man = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE); – sean Aug 20 '13 at 13:07
  • For me - Android 2.3.3 - it's working. What version do you use? Can you try to set a Log.d when one button is pressed so that you can see whether it's called? Can you show us your full code? – Max Aug 20 '13 at 13:24
  • you solution is correct . The problem is caused by a problem in my code . i have updated my code to show the changes – sean Aug 20 '13 at 14:43
5

Try this code, it should disable the toast message of the Volume

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        //Do something to hide the view
        AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        manager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
    }
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)){
        //Do something to hide the view
        AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        manager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
    }
    return true;
}
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66