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?
Asked
Active
Viewed 1.5k times
3 Answers
15
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
.

Wroclai
- 26,835
- 7
- 76
- 67
-
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 Mar 11 '11 at 22:17
-
Most examples are in terms of Canvas etc. My onTouchEvent is not getting called when the user touches Activity screen – Androider Mar 11 '11 at 22:20
-
@Androider: Maybe you're looking for http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)? – Wroclai Mar 11 '11 at 22:28
-
i'm looking for something like getContentView()? – Androider Mar 11 '11 at 22:34
-
I see how to set to layout .. but there should be a get view – Androider Mar 11 '11 at 22:34
-
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 Mar 11 '11 at 22:37
-
I want to get the current content view and set its onTouchListener – Androider Mar 11 '11 at 22:37
-
http://stackoverflow.com/questions/5279182/android-how-to-register-ontouchevent-for-entire-activity-main-content-view/5279252#5279252 I have answered that here – Androider Mar 11 '11 at 22:59
-
Timestamps might work well, I think that is simpler. Not sure how accurate it is however. – Androider Mar 11 '11 at 23:04
-
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 – StackJP Jan 14 '14 at 17:19
8
You can't use unix timestamps in this case. Android offers it's own time measurement.
long eventDuration =
android.os.SystemClock.elapsedRealtime()
- event.getDownTime();

Sebastian Ullrich
- 1,007
- 11
- 21