I have a RelativeLayout I want to perform a touch event with out touching the screen want to give a Toast message if its actually touched or not.Please throw I have tried with the below it seems not working
MotionEvent event = MotionEvent.obtain(downTime, eventTime, action,
x, y, pressure, size,
metaState, xPrecision, yPrecision,
deviceId, edgeFlags);
onTouchEvent(event);
You should not do that. If you indeed want to "touch" your view programatically, you should wrap whatever happens in your onTouch method in another function(without the MotionEvent parameter) and call that function, when you want to touch your view. Calling the onTouch without a real touch would get you negative stylepoints
@Override
boolean onTouch(View v, MotionEvent me) {
return action(me.getX(),me.getY());
}
boolean action(int x, int y) {
//do some stuff
}
void somewhereelse() {
//Perform touch action
action(0,0);
}
If you really want to 'dispatch' a touch event. You do it like this:
View v;
v.dispatchTouchEvent(MotionEvent.obtain(0,0,MotionEvent.ACTION_DOWN, 100,100,0.5f,5,0,1,1,0,0));
The values are pretty random. Only the deviceId=0 indicates that it is programatically dispatched Touch.
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