109

How to simulate a touch event with Android while giving the X and Y coordinates manually?

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
indira
  • 6,569
  • 18
  • 65
  • 80
  • 1
    You are getting some workable answers below, just bear in mind they will only work on applications to which you can make small modifications. For other apps you cannot modify, you would need a rooted platform to inject events. – Chris Stratton Dec 09 '10 at 08:20
  • Is it possible to make a application to touch in x,y on each 10 second and minimize it, but touching in x,y be continued? – Dr.jacky Nov 22 '14 at 16:40

7 Answers7

113

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.LENGTH_LONG
        );
        toast.show();

        return true;
    }
});


// 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);

For more on obtaining a MotionEvent object, here is an excellent answer: Android: How to create a MotionEvent?

Community
  • 1
  • 1
dvs
  • 12,324
  • 6
  • 38
  • 45
  • 2
    Good answer, but as an Android beginner the OnTouchListener confused me. I now realize that the listener is not necessary to the simulation. The only thing that is necessary is the MotionEvent in the second half of your code. – user1532390 Jan 28 '13 at 16:06
  • Why are you adding 100 millis to the eventTime? Does it not work if downTime and eventTime are the same? – tudor -Reinstate Monica- Oct 07 '14 at 01:04
  • 3
    @tudor - Only to simulate a real touch. `downTime` would be the time when the user touches down on the screen, while `eventTime` in this case would be when the user lifts their finger up (`ACTION_UP`). I am not sure if it will still work if both are the same. You could test it and post your results. – dvs Oct 07 '14 at 19:44
  • 1
    How should I declare the view object? – TheLogicGuy Dec 22 '16 at 09:49
  • ok.. what about swipe gesture left, right, top and bottom.. using ACTION_MOVE how to do this ? – Ramesh_D Apr 02 '17 at 12:23
  • @azdev that's not true about "simulating real touch". From docs: `event time` is the the time (in ms) when this specific event was generated. **This must be obtained from uptimeMillis()**. `downTime` is The time (in ms) when the user originally pressed down to start a stream of position events. **This must be obtained from uptimeMillis()** – StayCool Dec 11 '19 at 09:24
24

Here is a monkeyrunner script that sends touch and drags to an application. I have been using this to test that my application can handle rapid repetitive swipe gestures.

# This is a monkeyrunner jython script that opens a connection to an Android
# device and continually sends a stream of swipe and touch gestures.
#
# See http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html
#
# usage: monkeyrunner swipe_monkey.py
#

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device
device = MonkeyRunner.waitForConnection()

# A swipe left from (x1, y) to (x2, y) in 2 steps
y = 400
x1 = 100
x2 = 300
start = (x1, y)
end = (x2, y)
duration = 0.2
steps = 2
pause = 0.2

for i in range(1, 250):
    # Every so often inject a touch to spice things up!
    if i % 9 == 0:
        device.touch(x2, y, 'DOWN_AND_UP')
        MonkeyRunner.sleep(pause)
    # Swipe right
    device.drag(start, end, duration, steps)
    MonkeyRunner.sleep(pause)
    # Swipe left
    device.drag(end, start, duration, steps)
    MonkeyRunner.sleep(pause)
Warwick
  • 249
  • 1
  • 3
22

use adb Shell Commands to simulate the touch event

adb shell input tap x y 

and also 

adb shell sendevent /dev/input/event0 3 0 5 
adb shell sendevent /dev/input/event0 3 1 29 
Arjun Prakash
  • 669
  • 9
  • 23
  • 1
    hey what about if want to simulate pinch zoom using adb command and I have coordinates of pinch zoom – nikhil84 Aug 20 '14 at 10:30
1

You should give the new monkeyrunner a go. Maybe this can solve your problems. You put keycodes in it for testing, maybe touch events are also possible.

white
  • 1,823
  • 2
  • 12
  • 20
Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
  • 1
    Please let me know how to install monkeyrunner. adb is not recognizing monkeyrunner – indira Dec 10 '10 at 04:24
  • It's not related to adb's ui excersiser monkey. You'll find monkeyrunner in the tools directory of revision 9 of the adt. – Ben Weiss Dec 10 '10 at 11:38
  • Also check out the link I have provided within my answer. This leads to Google's provided information on the monkeyrunner. – Ben Weiss Dec 10 '10 at 12:26
  • I tried with "./adb shell monkey". But couldn't invoke touch event – indira Dec 13 '10 at 05:23
  • It is not `adb shell monkey` it is the `monkeyrunner`, which is a different tool. – Ben Weiss Dec 13 '10 at 11:46
  • There is a description of the general useage over here: http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html I didn't find the time to try it myself, but it sounds promising. – Ben Weiss Dec 14 '10 at 14:06
  • sorry.. monkeyrunner didn't work for me.. got "command not found". – indira Dec 15 '10 at 10:20
  • Are you sure, that you do have android sdk tools revision 8 installed? They provide the monkeyrunner. You will find it in the `tools` directory. – Ben Weiss Dec 15 '10 at 18:15
  • "grep -rn monkey * " only shows images/NOTICE.txt:80: /system/framework/monkey.jar images/NOTICE.txt:2527: /system/framework/monkey.jar – indira Dec 21 '10 at 06:57
  • You are sure that you did get the latest android utilities? Run `android update sdk` and install the latest sdk and tools. They also contain the monkeyrunner – Ben Weiss Dec 21 '10 at 09:49
  • Thank u very much.. :) Now I'm getting monkeyrunner – indira Dec 24 '10 at 06:03
1

If I understand clearly, you want to do this programatically. Then, you could use the onTouchEvent method of View, and create a MotionEvent with the coordinates you need.

Valentin Rocher
  • 11,667
  • 45
  • 59
0

When using Monkey Script I noticed that DispatchPress(KEYCODE_BACK) is doing nothing which really suck. In many cases this is due to the fact that the Activity doesn't consume the Key event. The solution to this problem is to use a mix of monkey script and adb shell input command in a sequence.

1 Using monkey script gave some great timing control. Wait a certain amount of second for the activity and is a blocking adb call.
2 Finally sending adb shell input keyevent 4 will end the running APK.

EG

adb shell monkey -p com.my.application -v -v -v -f /sdcard/monkey_script.txt 1
adb shell input keyevent 4

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. I have removed the link. Do not add it back in without adding a disclosure as well. – Andrew Barber Jan 25 '13 at 05:33
-7

MotionEvent is generated only by touching the screen.

indira
  • 6,569
  • 18
  • 65
  • 80
  • 11
    You can create one manually by using one of MotionEvent's static obtain methods. – dvs Aug 09 '11 at 18:58