I know this is a bit repeating question. I have reviewed various questions on SO and few other sites but could not find a single answer that satisfies my need.
Requirement
1) I have X,Y coordinates, I want to create MotionEvent and dispatch it to topmost activity/view.
2) I have to inject it in my own App. So Inject_Events permission should not be required.
3) While I have the coordinates, I do not know, Activity or View which are at that position. So Activity.dispatchTouchEvent does not work for me.. (view.dispatchTouchEvent will work but I do not know the view).
4) Instrumentation does not help either because they do not have single method to inject touch event at x,y location.
5) InputManager has hidden method injectInputEvent but I don't know how to access injectInputEvent and compile it?
Any help appreciated!
Valentin Rocher's method works if you've extended your view, but if you're using an event listener, use this: view. setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { Toast toast = Toast. makeText( getApplicationContext(), "View touched", Toast.
Touch events are delivered first to Activity. dispatchTouchEvent. It's where you may catch them first. Here they get dispatched to Window, where they traverse View hierarchy, in such order that Widgets that are drawn last (on top of other widgets) have chance to process touch in View.
Try code below to detect touch events. mView. setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //show dialog here return false; } }); To show dialog use Activity method showDialog(int).
Search for Enable resampling input events. Click on Enable touch events > Disabled.
dispatchTouchEvent(MotionEvent.obtain(
SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN, Xinput, Yinput, 0));
You do not need to specify a view, the event will register to the foreground activity. Replace Xinput and Yinput with your values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With