Samjack
Samjack

Reputation: 188

Android: How to determinate how long i touch the screen?

for my app i want to determinate how long i touch the screen. I want use the function onTouchEvent, i don't want use some images or some buttons to do it. Some idea? Otherwise, if there isn't a way, with a buttons but not with a image.

Upvotes: 2

Views: 267

Answers (2)

Sipka
Sipka

Reputation: 2311

ZouZou's answer is good, but here's another solution, without struggling with timers. If you have a MotionEvent event then you can use getDownTime() and getEventTime() methods. The difference between them will be the duration of the touch.

@Override
public boolean onTouchEvent(MotionEvent event) {
    final long time = (event.getEventTime() - event.getDownTime();
    return true;
}

Upvotes: 1

Alexis C.
Alexis C.

Reputation: 93842

You can catch the movement using MotionEvent.ACTION_DOWN to start the timer and stop it when you catch MotionEvent.ACTION_UP.

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
              //start timer
        } else if(event.getAction()==MotionEvent.ACTION_UP){
             //end timer
        }
        return true;
    }

Upvotes: 3

Related Questions