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();
Author by
Androider
Updated on July 26, 2022Comments
-
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 about 13 yearsOk. 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 about 13 yearsMost examples are in terms of Canvas etc. My onTouchEvent is not getting called when the user touches Activity screen
-
Wroclai about 13 years@Androider: Maybe you're looking for developer.android.com/reference/android/app/…?
-
Androider about 13 yearsi'm looking for something like getContentView()?
-
Androider about 13 yearsI see how to set to layout .. but there should be a get view
-
Androider about 13 yearsI 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 about 13 yearsI want to get the current content view and set its onTouchListener
-
Androider about 13 yearsstackoverflow.com/questions/5279182/… I have answered that here
-
Androider about 13 yearsTimestamps might work well, I think that is simpler. Not sure how accurate it is however.
-
StackJP over 10 yearsThis 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