Android: How to create a MotionEvent?
You should use one of the static obtain
methods of the MotionEvent
class to create a new event.
The simplest way (besides wrapping a new event from an existing one) is:
static public MotionEvent obtain(long downTime, long eventTime, int action,
float x, float y, int metaState) {
API Docs:
Create a new MotionEvent, filling in a
subset of the basic motion values.
Those not specified here are: device
id (always 0), pressure and size
(always 1), x and y precision (always
1), and edgeFlags (always 0).
Parameters:
downTime
The time (in ms) when the
user originally pressed down to start
a stream of position events. This
must be obtained from
SystemClock.uptimeMillis().eventTime
The the time (in ms) when
this specific event was generated.
This must be obtained fromSystemClock.uptimeMillis()
.action
The kind of action being
performed -- one of eitherACTION_DOWN
,ACTION_MOVE
,ACTION_UP
, orACTION_CANCEL
.x
The X coordinate of this event.y
The Y coordinate of this event.metaState
The state of any meta /
modifier keys that were in effect
when the event was generated.
Link to API Docs
How to create a MotionEvent for unit testing in Android Studio?
here is documentation how to achieve it dispatchTouchEvent()
kotlin example:
val motionEvent = MotionEvent.obtain(200, 300, MotionEvent.ACTION_MOVE, 1.0f, 0.0f, 0)
motionEvent.source = InputDevice.SOURCE_JOYSTICK
yourView.dispatchGenericMotionEvent(motionEvent)
How to send synthesized MotionEvent through the system?
No, it's prevented by design. The concern is that such a feature can be used to subvert the entire security model - e.g. by "injecting" touches to contact the marketplace, arrange an install, accept the security warnings .. all on its own.
This has been discussed at some length here and following.
If this answers your question, kindly click the checkmark to the left - thanks!
How to perform onTouch event from code?
This should work, found here: How to simulate a touch event in Android?:
// 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?
EDIT: and to get the location of your view, for the x
and y
coordinates, use:
int[] coords = new int[2];
myView.getLocationOnScreen(coords);
int x = coords[0];
int y = coords[1];
Implement Motion Event in android button to show password on Edittext with alternate button clicks
Use below code:
Define one flag,
boolean isPassWordShowing = false;
The click of ShowPassword Button:
showPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isPassWordShowing)
{
passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
isPassWordShowing = false;
}
else
{
passwordET.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
isPassWordShowing = true;
}
}
});
How do I make a MotionEvent to zoom?
take a look here: https://stackoverflow.com/questions/522312/best-practices-for-unit-testing-android-apps
I think multitouch unit testing is not possible with the standard Android framework.
How to programmatically trigger the touch event in android?
// 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);
Related Topics
Installation Failed with Message Invalid File
How to Change the Toolbar Text Size
App Always Starts Fresh from Root Activity Instead of Resuming Background State (Known Bug)
How to Read and Edit Android Calendar Events Using the New Android 4.0 Ice Cream Sandwich API
Displaying Yuv Image in Android
Emulator Appearing Offline on M1 MAC After the Last Update of Arm64-V8A
Android: Get Thumbnail of Image on Sd Card, Given Uri of Original Image
Load the Image Saved in Sdcard in Webview
Android Httpclient Hangs on Second Request to the Server (Connection Timed Out)
How to Change the Android Startactivity() Transition Animation
How to Check If an App Is a Non-System App in Android
Difference Between a Clickable Imageview and Imagebutton
How to Write Text Over a Picture in Android and Save It
Android: How to Get Gsm Signal Strength for All Available Network Operators
How to Implement Placeautocompletefragment and Placeautocompleteactivity to Get Place Details