Skip to content

Instantly share code, notes, and snippets.

@trfiladelfo
Created August 29, 2019 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trfiladelfo/f1f0031cf0e48286620f2d73d1e4497d to your computer and use it in GitHub Desktop.
Save trfiladelfo/f1f0031cf0e48286620f2d73d1e4497d to your computer and use it in GitHub Desktop.
simulate click android
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);
Button buttonFoo = (Button)findViewById(R.id.button_foo);
buttonFoo.performClick();
@NirealCheater350
Copy link

Hi

@NirealCheater350
Copy link

Error Syntax buttonFoo.performClick();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment