I have a text view, on which I am implementing the touch listener.
Code:
textview_temp2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.i("TAG1","touched Down");
return true;
case MotionEvent.ACTION_UP:
Log.i("TAG3","touched UP");
return true;
}
return false;
}
});
Now, I want to call event action up if the user holds the text view for 5 or more seconds. Also, only one among these two events should be called.
Easy to accomplish. You need to use a handler to handle your delayed tasks.
final int STEP3SECS = 3;
final int STEP5SECS = 5;
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == STEP3SECS) {
//do 3 second job
} else if (msg.what == STEP5SECS) {
//do 5 second job
}
return false;
}
});
And then:
textview_temp2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.i("TAG1","touched Down");
handler.sendEmptyMessageDelayed(STEP3SECS,3000);
handler.sendEmptyMessageDelayed(STEP5SECS,5000);
return true;
case MotionEvent.ACTION_UP:
Log.i("TAG3","touched UP");
handler.removeCallbacksAndMessages(null);
return true;
}
return false;
}
});
ACTION_UP
can take place in 3 different time intervals: before 3 seconds, well no job gets done. After 3 seconds and before 5 seconds, only 3 second job gets done. And after 5 seconds which both jobs are handled.