Android Touch Event determining duration

14,515

Solution 1

You could try mixing MotionEvent and Runnable/Handler to achieve this.

Sample code:

private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
    public void run() {
         checkGlobalVariable();
    }
};

// Other init stuff etc...

@Override
public void onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        // Execute your Runnable after 5000 milliseconds = 5 seconds.
        handler.postDelayed(runnable, 5000);
        mBooleanIsPressed = true;
    }

    if(event.getAction() == MotionEvent.ACTION_UP) {
        if(mBooleanIsPressed) {
            mBooleanIsPressed = false;
            handler.removeCallbacks(runnable);
        }
    }
}

Now you only need to check if mBooleanIsPressed is true in the checkGlobalVariable() function.

One idea I came up with when I was writing this was to use simple timestamps (e.g. System.currentTimeMillis()) to determine the duration between MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP.

Solution 2

long eventDuration = event.getEventTime() - event.getDownTime();

Solution 3

You can't use unix timestamps in this case. Android offers it's own time measurement.

long eventDuration = 
            android.os.SystemClock.elapsedRealtime() 
            - event.getDownTime();
Share:
14,515
Androider
Author by

Androider

Updated on July 26, 2022

Comments

  • Androider
    Androider almost 2 years

    How does one detect the duration of an Android 2.1 touch event? I would like to respond only if the region has been pressed for say 5 seconds?

  • Androider
    Androider about 13 years
    Ok. This is something I need on most of my Activity Screens. How would I enable listening on the Activity screen. I have implemented the onTouchEvent listener in the activity, but how do you enable this for the activity, so that the onTouchEvent gets called when user touches activity screen. Thanks
  • Androider
    Androider about 13 years
    Most examples are in terms of Canvas etc. My onTouchEvent is not getting called when the user touches Activity screen
  • Wroclai
    Wroclai about 13 years
    @Androider: Maybe you're looking for developer.android.com/reference/android/app/…?
  • Androider
    Androider about 13 years
    i'm looking for something like getContentView()?
  • Androider
    Androider about 13 years
    I see how to set to layout .. but there should be a get view
  • Androider
    Androider about 13 years
    I suppose I could use fineViewById(layoutId) like for components and set the activity as its onTouchEvent listener. Seems like this might not be best place however
  • Androider
    Androider about 13 years
    I want to get the current content view and set its onTouchListener
  • Androider
    Androider about 13 years
    stackoverflow.com/questions/5279182/… I have answered that here
  • Androider
    Androider about 13 years
    Timestamps might work well, I think that is simpler. Not sure how accurate it is however.
  • StackJP
    StackJP over 10 years
    This worked great for me, thanks a lot! One thing to note for my use case was since my view was housed in a pager (i.e. could be scrolled away), I needed to also cancel the handler in the MotionEvent.ACTION_CANCEL switch case. This way my handler wouldn't go off when the touch event was stolen by the pager