How to programmatically trigger the touch event in android?
Asked Answered
A

4

63

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.

Angiosperm answered 28/5, 2014 at 4:33 Comment(3)
you can override developer.android.com/reference/android/view/…Bellman
Right now I have the function of the ontouchEvent already, and i would like to trigger it programmtically instead of the user manually touch on the screenAngiosperm
I have think of calling the ontouch event in a function directly but it is quite complex so I think the best way is to emulate the touch eventAngiosperm
E
116
// 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);
Escapism answered 28/5, 2014 at 4:43 Comment(6)
How should I declare and initialize the view object?Themselves
what about swipe gesture left, right, top and bottom.. using ACTION_MOVE is it possible ?Anhydrite
@Anhydrite You would have to generate a lot of ACTION_MOVE events to simulate a swipe.Linder
A swipe can be generated with an ACTION_DOWN, an ACTION_MOVE, and an ACTION_UP.Ryon
I use this, worked perfectly, onTouch event is called, but I found one issue when i use above code and hold my finger on the view and move, ontouchevent not calledVariolite
what's the view definition?Squier
H
6

And here is the clean version:

public void TouchView(View view)
{
    view.DispatchTouchEvent(MotionEvent.Obtain(SystemClock.UptimeMillis(), SystemClock.UptimeMillis(), (int)MotionEventActions.Down, 0, 0, 0));
    view.DispatchTouchEvent(MotionEvent.Obtain(SystemClock.UptimeMillis(), SystemClock.UptimeMillis(), (int)MotionEventActions.Up, 0, 0, 0));
}

PS: This is a xamarin android solution but you can easily modify it for java

Higginbotham answered 3/7, 2018 at 7:24 Comment(0)
I
1

To add to the excellent answer by @bstar55 above - if what you want is to create a 'tap' event, e.g, a user tapping on a view, then you need to simulate the user touching the screen and then removing their finger in a short time frame.

To do this you 'dispatch' an ACTION_DOWN MotionEvent followed after a short time by an ACTION_UP MotionEvent.

The following example, in Kotlin, is tested and worked reliably:

   //Create amd send a tap event at the current target loctaion to the PhotoView
   //From testing (on an original Google Pixel) a tap event needs an ACTION_DOWN followed shortly afterwards by
   //an ACTION_UP, so send both events
   //First, create amd send the ACTION_DOWN MotionEvent
   var originalDownTime: Long = SystemClock.uptimeMillis()
   var eventTime: Long = SystemClock.uptimeMillis() + 100
   var x = your_X_Value
   var y = your_Y_Value 
   var metaState = 0
   var motionEvent = MotionEvent.obtain(
           originalDownTime,
           eventTime,
           MotionEvent.ACTION_DOWN,
           x,
           y,
           metaState
    )
    var returnVal = yourView.dispatchTouchEvent(motionEvent)
    Log.d(TAG,"rteurnVal: " + returnVal)

    //Create amd send the ACTION_UP MotionEvent
    eventTime= SystemClock.uptimeMillis() + 100
    motionEvent = MotionEvent.obtain(
           originalDownTime,
           eventTime,
           MotionEvent.ACTION_UP,
           x,
           y,
           metaState
     )
     returnVal = yourView.dispatchTouchEvent(motionEvent)
     Log.d(TAG,"rteurnVal: " + returnVal)
Infundibuliform answered 9/11, 2020 at 12:32 Comment(0)
T
1
fun View.performTouchDown() = dispatchTouchEvent(MotionEvent
    .obtain(uptimeMillis(), uptimeMillis() + 700, ACTION_DOWN, 0f, 0f, 0))
Turnage answered 3/8, 2022 at 4:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.