Ad

Keep On Doing An Action While Button Is Pressed After Long Click

Sorry if the title makes no sense. What I'm trying to accomplish is to be able to long click a button, and once the long click is detected, to keep on doing a certain task.

To put it into context, I have a view with a background that changes to a random color on click. I would like to have the background keep on changing color as long as I long click the button. So, basically, upon long click and keeping the button pressed, the background will keep on changing.

Thank you very much folks :)

Ad

Answer

This isn't possible with view.setOnLongClickListener() because an OnLongClickListener has a very specific way of functioning. As soon as enough time has passed, the listener will activate and the code is run.

What you want is to use view.setOnTouchListener(). This way you can set a Timer when it detects a MotionEvent.ACTION_DOWN. Once the timer is long enough to equal a long click, you can have it trigger your color changing code repeatedly until the OnTouchListener detects a MotionEvent.ACTION_UP. That's when you can stop the color changing code.

Ad
source: stackoverflow.com
Ad