Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how automatically perform touch in android programmatically

Tags:

android

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);
like image 711
Abhijit Chakra Avatar asked May 28 '26 16:05

Abhijit Chakra


1 Answers

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.

like image 64
xeed Avatar answered May 31 '26 04:05

xeed