4

Is it possible to simulate a "click" (touch screen) by coordinates or on a view element?

TyrionLannister
  • 73
  • 1
  • 3
  • 7
  • what would this "simulation" produce? – Yevgeny Simkin May 20 '13 at 10:25
  • For example: If my app is a calculator I want "programming", you press "2" "+" "3" and "=" That is, simulate pressing on those 4 buttons. That is, the user will see without pressing anything like the "2" button is pressed alone, then the "+" ... – TyrionLannister May 20 '13 at 15:37

4 Answers4

5

It is possible to simulate touch events on android screen. If you have the coordinates of the view then you can generate touch events by using adb shell commands. For e.g-

adb shell input tap x y

where x and y are your coordinates. You can run this command from terminal. If you want to run the command from android code then use "/system/bin/ input tap x y" and run this by using Runtime.getRuntime() method. For details please reply, happy to help! :)

Neeraj Kumar
  • 755
  • 2
  • 16
  • 32
suv
  • 151
  • 2
  • 15
2

As azdev suggests, try 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);
Community
  • 1
  • 1
Basim Sherif
  • 5,291
  • 7
  • 46
  • 89
  • Worked, very straightforward method to simulate onTouch over view/screen. – Pelanes Apr 25 '20 at 09:41
  • what is that float x and y? is it possible for targeting webview? – gumuruh May 10 '20 at 07:53
  • @gumuruh : Its x and y coordinates of the device screen to which you need to deploy touch. – Basim Sherif May 11 '20 at 05:08
  • if let say i obtain a location of x,y from a webview element.... and then i want to touch on x+20 and y+20 which means more to the right position and more to the bottom position... is that still touchable? – gumuruh May 11 '20 at 05:32
  • @gumuruh: It can be touchable but you have to add a check to confirm if it's not outside the screen. And btw, y+20 means more to the top position, not the bottom. – Basim Sherif May 11 '20 at 10:58
  • y+20 is to the top? I thought it was bottom? Because x,y are started from corner left top screen right @BasimSherif? – gumuruh May 12 '20 at 06:13
  • @gumuruh Sorry, my bad. Yes you are correct – Basim Sherif May 12 '20 at 07:09
1

presumably you have something that you wish to invoke via a click. So... if it's an actual button you can call performClick() on it. If it's not a button, then just call whatever the method you wish to execute is, when the conditions that you expect are met. It might help if you offered a little more details as to what you're actually trying to do.

Yevgeny Simkin
  • 27,096
  • 37
  • 131
  • 232
0

On a View, yes. By coordinates, a la Java Robot, not that I'm aware.

For example:

Button buttonFoo = (Button)findViewById(R.id.button_foo);
buttonFoo.performClick();
MarsAtomic
  • 10,150
  • 5
  • 33
  • 56