Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How to assign function to volume up button?

Hi!

I'm trying to develop an app where everytime I press the screen, an action is taken.

Can I also assign this fuction to the volume up button? In other words, can "touch the screen" and pressing volume up button do the same?

I explain why I have this question. I would like to remote control my app using the bluetooth remote control of the new selfie sticks. If I assign that option to the volume button (which is the one that the bluetooth remote deals with) can I create an action using the already built selfy bluetooth remote?

Is this possible for Android and iOS?

Before I start exploring code, I would like to know if it is possible.

I thank you in advance for your time.

Regards,

Duarte

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

You can override the onKeyDown and onKeyUp methods in your activity and then check for where the key code equals KeyEvent.KEYCODE_VOLUME_UP.

Here's an example:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
            // Do an action
            return true;
        }
        return false;
    }