I would like to increase the value of a variable constantly when a button is held down.
I’m using onClickListener but it only increase once when the button is click and released.
How do I determine if the button is being pressed and hold?
What I want is something like “while(ButtonIsBeingHold){count ++;}”
If there are any other methods please advice me on it
Instead of using View.onClickListener, you could try using View.onTouchListener:
This will get fired for different types of touch events such as:
MotionEvent.TOUCH_UP and MotionEvent.TOUCH_DOWN.
You could set a flag for when the butotn is pressed (TOUCH_DOWN) and start increasing your counter and stop on TOUCH_UP. I would, however, advise not to use a while(true) loop and try to throttle the incrementing events being trigger. In this case, I would use a handler and pass messages to it at a constant rate. You can see how a handler like this is used for rendering and updating a game in the Snake sample project that comes with the Android SDK:
http://developer.android.com/resources/samples/Snake/index.html