vnshetty
vnshetty

Reputation: 20132

how to find the time gap between action up and action down event in android

i have following code...

public boolean onTouch(View view, MotionEvent me) {

    //here i want to know the  time of touch

    switch (me.getAction()) {
       case MotionEvent.ACTION_DOWN: { }  
       case MotionEvent.ACTION_UP: { }
       -
       -
       -
   }
  }

Any Idea?

Upvotes: 0

Views: 4271

Answers (3)

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

It is pretty simple

public yourClass{
    long down;    
    public boolean onTouch(View view, MotionEvent me) {
        //you don't need here the difference because it might be a down action!
        //you need the difference when the up action occurs
        switch (me.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                //this is the time in milliseconds
                long diff = System.currentTimeMillis() - down; 
                break;
        }
    }
}     

Upvotes: 7

clonejo
clonejo

Reputation: 1249

I suppose you want to measure the time between the two events. This is very simple. All you need is to find an API method which returns the current time. In this case android.os.SystemClock.elapsedRealtime() or .uptimeMillis() should serve you (android documentation).
When the first event occurs, you save the current time in a variable. On the second event you just calculate the difference between the current time and the value you stored before.

Upvotes: 2

NickT
NickT

Reputation: 23873

You can use

System.currentTimeMillis();

to capture the times on each event

Upvotes: 0

Related Questions