Search code examples
androideventsgesture

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?


Solution

  • 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;
            }
        }
    }