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 1016259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:31:23+00:00 2026-05-16T10:31:23+00:00

I have two buttons that increment and decrement a value by one with each

  • 0

I have two buttons that increment and decrement a value by one with each press and they’re working just fine with the onClickListener. I see that an onLongClickListener exists, which I assume is for touch and hold events. How would I have the number rapidly increment/decrement if the button is held?

Am I correct in assuming that onLongClickListener only fires once per long click? Is there a more appropriate listener or a property somewhere that I’m not aware of?

  • 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-05-16T10:31:24+00:00Added an answer on May 16, 2026 at 10:31 am

    You may implement it as in the following code.

    package org.me.rapidchange;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnKeyListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.TextView;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    public class MainActivity extends Activity implements OnKeyListener, 
            OnTouchListener, OnClickListener {
        private class UpdateCounterTask implements Runnable {
            private boolean mInc;
    
            public UpdateCounterTask(boolean inc) {
                mInc = inc;
            }
    
            public void run() {
                if (mInc) {
                    mHandler.sendEmptyMessage(MSG_INC);
                } else {
                    mHandler.sendEmptyMessage(MSG_DEC);
                }
            }
        }
    
        private static final int MSG_INC = 0;
        private static final int MSG_DEC = 1;
    
        private Button mIncButton;
        private Button mDecButton;
        private TextView mText;
        private int mCounter;
    
        private Handler mHandler;
        private ScheduledExecutorService mUpdater;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case MSG_INC:
                            inc();
                            return;
                        case MSG_DEC:
                            dec();
                            return;
                    }
                    super.handleMessage(msg);
                }
            };
            mIncButton = (Button) findViewById(R.id.inc_button);
            mDecButton = (Button) findViewById(R.id.dec_button);
            mText = (TextView) findViewById(R.id.text);
            mIncButton.setOnTouchListener(this);
            mIncButton.setOnKeyListener(this);
            mIncButton.setOnClickListener(this);
            mDecButton.setOnTouchListener(this);
            mDecButton.setOnKeyListener(this);
            mDecButton.setOnClickListener(this);
        }
    
        private void inc() {
            mCounter++;
            mText.setText(Integer.toString(mCounter));
        }
    
        private void dec() {
            mCounter--;
            mText.setText(Integer.toString(mCounter));
        }
    
        private void startUpdating(boolean inc) {
            if (mUpdater != null) {
                Log.e(getClass().getSimpleName(), "Another executor is still active");
                return;
            }
            mUpdater = Executors.newSingleThreadScheduledExecutor();
            mUpdater.scheduleAtFixedRate(new UpdateCounterTask(inc), 200, 200,
                    TimeUnit.MILLISECONDS);
        }
    
        private void stopUpdating() {
            mUpdater.shutdownNow();
            mUpdater = null;
        }
    
        public void onClick(View v) {
            if (mUpdater == null) {
                if (v == mIncButton) {
                    inc();
                } else {
                    dec();
                }
            }
        }
    
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
            boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
            boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getAction() != KeyEvent.ACTION_MULTIPLE;
    
            if (isKeyOfInterest && isReleased) {
                stopUpdating();
            } else if (isKeyOfInterest && isPressed) {
                startUpdating(v == mIncButton);
            }
            return false;
        }
    
        public boolean onTouch(View v, MotionEvent event) {
            boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
            boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
    
            if (isReleased) {
                stopUpdating();
            } else if (isPressed) {
                startUpdating(v == mIncButton);
            }
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has two buttons on it, one yes, one no,
I have two buttons that are not performing the IBAction that they are programmed
Suppose I have two buttons that are ivar outlets. One is called Blue and
I have two buttons that have assigned two functions, one by a broad selection:
I have two select lists that each display users and two buttons (add and
I have two buttons in my program and i want that when i press
I have a main ViewController that has two buttons that each segue to their
I have two buttons that moves a movieclip up/down the Y-axis. How do I
I have two radio buttons on my MVC form that I use to hide
I have a menu bar that is rotated slightly. Here are two buttons as

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.