Jim Blackler
Jim Blackler

Reputation: 23179

How to override the behavior of the volume buttons in an Android application?

I'd like to use the volume buttons for something else in my Android application. The Dolphin browser does this I am told. Anyone know how?

Upvotes: 28

Views: 20385

Answers (2)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

in your activity,

 @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

Upvotes: 38

Segfault
Segfault

Reputation: 8290

I imagine it looks something like this:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
   if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) { 
       // Do your thing 
       return true;
   } else {
       return super.onKeyDown(keyCode, event); 
   }
}

Upvotes: 38

Related Questions