1

I'm a beginner to android. I need to determine the touched time duration of android activity. As an example, When user touch and hold the screen, after 5 seconds, I need to show a toast(this scenario should be done in user's touch time period. In other words not in action up event). Is this possible? I tried with this.

public boolean onTouchEvent(MotionEvent event) {
    int eventaction = event.getAction();
    switch (eventaction) {
    case MotionEvent.ACTION_DOWN:
        pressTime = event.getDownTime();            
        break;
    case MotionEvent.ACTION_UP:
        eventTime = event.getEventTime();
        //Toast.makeText(this, (eventTime - pressTime)+"", 100).show();
        if (pressTime <= 5000) {
            Toast t = Toast.makeText(this, "5 sec", 5);
            t.show();
        }
        if (pressTime > 7000) {
            Toast t = Toast.makeText(this, "5 sec to 20 sec", 5);
            t.show();
        }
}

This is working when the action up event. I need to do it in the touching time period.

Thanks in Advance.

hump
  • 53
  • 10

1 Answers1

2

Check difference between your press time and release time:

long holdTime = eventTime - pressTime;
if (holdTime > 7000 && holdTime < 20000) {
    Toast t = Toast.makeText(this, "5 sec to 20 sec", 5);
    t.show();
} else if (holdTime > 5000 && holdTime <= 7000) {
    Toast t = Toast.makeText(this, "5 sec", 5);
    t.show();
}
wladimiiir
  • 351
  • 1
  • 4
  • But it's working on `ACTION_UP`. I need to do it touching time period. – hump Jun 24 '14 at 06:51
  • But this is working on `ACTION_UP` event. I need it when the user touches the screen. Is it possible? – hump Jun 24 '14 at 06:54
  • Then you have to use the solution provided in http://stackoverflow.com/questions/5278579/android-touch-event-determining-duration. – wladimiiir Jun 24 '14 at 08:28
  • It can be using only for 1 time period(5 sec only).. How can I use it for other time periods? – hump Jun 24 '14 at 12:04
  • You will add as many of these runnables as you need. In your case you will have one with 5000 delay and the second one with delay 7000. I hope you understand what I mean. – wladimiiir Jun 24 '14 at 12:13
  • You mean, Run a thread for each time periods? – hump Jun 24 '14 at 12:17