Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:20:34+00:00 2026-06-13T07:20:34+00:00

I want my application to react differently to a normal and a long pressed

  • 0

I want my application to react differently to a normal and a long pressed Key Event of the Volume Buttons.

I’ve already seen this, but if I keep the volume button pressed, I get a lot of KeyDown Events before I get the KeyLongPressed Event.

I’d like to have either one event or the other, not both, so that I can adjust the Volume at a short press and skip a track at a long press.

Can you help me out here?

This is my code:

    @Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) 
    {
        Log.d("Test", "Long press!");
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        event.startTracking();
        Log.d("Test", "Short");
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Any help appreciated!
– Iris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:20:36+00:00Added an answer on June 13, 2026 at 7:20 am

    Here is the code that I wrote. It works like a charm. May be you can optimize it for better logic. But you will get the point with it. The key is to use flags. Short press is a press where we press volume button for short time and release. So onKeyUp is the one which will help us detect short presses.

    package com.example.demo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.Menu;
    
    public class TestVolumeActivity extends Activity {
        boolean flag = false;
    
        boolean flag2 = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash_screen);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_splash_screen, menu);
            return true;
        }
    
        @Override
        public boolean onKeyLongPress(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                Log.d("Test", "Long press!");
                flag = false;
                flag2 = true;
                return true;
            }
            return super.onKeyLongPress(keyCode, event);
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                event.startTracking();
                if (flag2 == true) {
                    flag = false;
                } else {
                    flag = true;
                    flag2 = false;
                }
    
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    
        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
    
                event.startTracking();
                if (flag) {
                    Log.d("Test", "Short");
                }
                flag = true;
                flag2 = false;
                return true;
            }
    
            return super.onKeyUp(keyCode, event);
        }
    }
    

    Logcat for all long presses(No short press detected):

    10-18 02:06:15.369: D/Test(16834): Long press!
    10-18 02:06:18.683: D/Test(16834): Long press!
    10-18 02:06:21.566: D/Test(16834): Long press!
    10-18 02:06:23.738: D/Test(16834): Long press!
    

    Logcat for all short presses:

    10-18 02:07:42.422: D/Test(16834): Short
    10-18 02:07:43.203: D/Test(16834): Short
    10-18 02:07:43.663: D/Test(16834): Short
    10-18 02:07:44.144: D/Test(16834): Short
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add to my application a new view with buttons for sharing
I want my application to pick a video from the gallery. It uses following
I want my application to reset shared preferences when the application reinstalling let's say
I want my application to have a zoomable element which will allow the XAML
I want my application to encrypt a user password, and at one time password
I want my Application to show Custom Dialog When an Uncatch Exception occur. For
I want my application to be inside installation kit and after the installation create
I want my application to show a presentation page for 2 seconds for instance
I want my application to align two field, one to left and other to
When the user running my application, I want my application forward the user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.