Created
August 29, 2019 16:05
-
-
Save trfiladelfo/f1f0031cf0e48286620f2d73d1e4497d to your computer and use it in GitHub Desktop.
simulate click android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Button buttonFoo = (Button)findViewById(R.id.button_foo); | |
buttonFoo.performClick(); |
Error Syntax buttonFoo.performClick();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi