Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically trigger the touch event in android?

I would like to trigger a touch event like this:

First the finger is touch down at the (0,50%) of the screen and slide to the (50%,50%) of the screen, and exit (move the finger off the screen)

I have found some thing like this:

MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags);  onTouchEvent(event); 

However, how to emulate the above case? Do I need to create 2 event ? onTouchDown , onMove etc.... ? Thanks for helping.

like image 414
user782104 Avatar asked May 28 '14 04:05

user782104


1 Answers

// Obtain MotionEvent object long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + 100; float x = 0.0f; float y = 0.0f; // List of meta states found here:     developer.android.com/reference/android/view/KeyEvent.html#getMetaState() int metaState = 0; MotionEvent motionEvent = MotionEvent.obtain(     downTime,      eventTime,      MotionEvent.ACTION_UP,      x,      y,      metaState );  // Dispatch touch event to view view.dispatchTouchEvent(motionEvent); 
like image 96
bstar55 Avatar answered Oct 27 '22 23:10

bstar55