Freddy
Freddy

Reputation: 174

Using an Android Long-Button press to increment/decrement counter

I want to be able to press a button on my program and hold it down (without releasing) to increment a variable. Problem I am having right now is when I conduct the long button press it only runs once, until I release and press again.

First I want to find out if there is a way to do this without having to use the OnTouchListener, and just using the OnLongClick. Is there a way to check the value of the button? For example.. buttondown=true; Conduct a whileloop to increment until the button is released.

Second, I don't want the updates to be delayed, because the incremented value is being drawn as the user holds down the button.

Basically I am doing something like this:

btn_resume.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {


..code..
return true;

        }
    });

Upvotes: 2

Views: 7088

Answers (4)

rochdev
rochdev

Reputation: 3855

If I understood your question correct this can be achieved using a OnLongClickListener.

Upvotes: 4

Jinsi K
Jinsi K

Reputation: 267

I think you can use OnLongClickListener for increment/decrement. But once the long press is done for the button, the longpress has to be canceled or reset for the next long press of the same button.

Upvotes: 0

OneWorld
OneWorld

Reputation: 17671

Check http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

OnTouchListener provides a more granular handling of touch events, e.g. KeyDown, KeyUp

Upvotes: 0

Cheryl Simon
Cheryl Simon

Reputation: 46844

OnLongClick will only be called once per press. It isn't going to work for your purpose.

Upvotes: 4

Related Questions