Reputation: 8384
I'm trying to use setontouchlistener. What I would like to do if I hold an imagebutton for an example 5 seconds, I get alerted... this is how I would do it:
final ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// do something
return false;
}
});
How can I implement something like this with setontouchlistener after x seconds?
Working solution:
imageButton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mTouchDownTime = event.getEventTime();
Log.v("CustomDebug", "Message: " + mTouchDownTime);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
long elapsedTime = event.getEventTime() - mTouchDownTime;
Log.v("CustomDebug", "Message: " + elapsedTime);
}
return false;
}
});
Upvotes: 0
Views: 523
Reputation: 6131
onTouch gets called twice, the first time with a MotionEvent ACTION_DOWN and a second time with the MotionEvent ACTION_UP
so pseudocode:
if Event == ACTION_DOWN
saveTime = time();
elseif Event == ACTION_UP and time()-savetime > 5
doStuff because the guy hold it for 5sec.
Upvotes: 2