Android: How to Create a Motionevent

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 from
    SystemClock.uptimeMillis().
  • action The kind of action being
    performed -- one of either
    ACTION_DOWN, ACTION_MOVE,
    ACTION_UP, or ACTION_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



Leave a reply



Submit