So I'm trying to display on the button the time since the user started to press it, here's my code :
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
long eventDuration;
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
isDown = true;
break;
case MotionEvent.ACTION_UP:
isDown = false;
break;
default:
break;
}
eventDuration = motionEvent.getEventTime() - motionEvent.getDownTime();
btn.setText(String.valueOf(eventDuration));
return true;
}
});
The code is working except the value of the pressed time don't update if the user is holding down the button and not moving his finger because there's no Action such as "MotionEvent.ACTION_HOLD". I want that value to keep updating even if the user isn't moving his finger.
I tried to run a method each time the the Action is "MotionEvent.ACTION_DOWN"
while(isDown){
eventDuration = motionEvent.getEventTime() - motionEvent.getDownTime();
btn.setText(String.valueOf(eventDuration));
}
But didn't work the while loop doesn't stop and isDown kept its last value true
. Can anyone tell me how can I get it work? THANKS!
You can use a CountDownTimer here, when user presses the button start the countDownTimer with max limit of very high value and cancel it when you receive action_up in ouTouchListener
then start this inside Action_down is not already started otherwise reset it
and finish it inside action_down