Can we execute touch event programmatically in android by some method, such as passing screen co-ordinates(x,y)? Is there any such method? Please guide me.
Asked
Active
Viewed 2.2k times
4 Answers
19
I am not sure if it works, but try this:
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action,
x, y, pressure, size,
metaState, xPrecision, yPrecision,
deviceId, edgeFlags);
onTouchEvent(event);

onexf
- 3,674
- 3
- 22
- 36

Dmytro Danylyk
- 19,684
- 11
- 62
- 68
-
can u little bit explain it....how can i get these value i.e downtime,eventtime etc. – Srb May 19 '14 at 06:42
-
i want to click button programmatically using this code but it's not working for me. – umerk44 May 28 '15 at 10:21
2
You could call
View.onTouchEvent (MotionEvent)
when you know the destination
When you want to address some child or grand-child,
View.dispatchTouchEvent (MotionEvent)
is better
However, you have to supply an self crafted motion event. The MotionEvent holds the coordinates, too.

stefan bachert
- 9,413
- 4
- 33
- 40
2
You can try using dispatchTouchEvent and give it MotionEvent instance like below
dispatchTouchEvent(MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags))
you can find more help in this link

Hakem Zaied
- 14,061
- 1
- 23
- 25