Example usage for android.view MotionEvent ACTION_DOWN

List of usage examples for android.view MotionEvent ACTION_DOWN

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_DOWN.

Prototype

int ACTION_DOWN

To view the source code for android.view MotionEvent ACTION_DOWN.

Click Source Link

Document

Constant for #getActionMasked : A pressed gesture has started, the motion contains the initial starting location.

Usage

From source file:Main.java

public static void dispatchTouch(final View view, final long duration) {
    final long downTime = SystemClock.uptimeMillis();
    final long eventTime = SystemClock.uptimeMillis();
    final float x = view.getWidth() / 3;//0.0f;
    final float y = view.getHeight() / 3;//0.0f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    final int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState);

    // Dispatch touch event to view
    view.dispatchTouchEvent(motionEvent);

    new Handler().postDelayed(new Runnable() {
        @Override/*from   w  w  w  .  j av  a 2  s  .  c om*/
        public void run() {
            MotionEvent motionEvent = MotionEvent.obtain(downTime + duration, eventTime + duration,
                    MotionEvent.ACTION_UP, x, y, metaState);
            view.dispatchTouchEvent(motionEvent);
        }
    }, duration);
}

From source file:Main.java

/**
 * Performs a single touch on the center of the supplied view.
 * This is safe to call from the instrumentation thread and will invoke the touch
 * asynchronously.//w  w  w. jav a  2s  .  c  o m
 *
 * @param view The view the coordinates are relative to.
 */
public static void simulateTouchCenterOfView(final View view) throws Throwable {
    view.post(new Runnable() {
        @Override
        public void run() {
            long eventTime = SystemClock.uptimeMillis();
            float x = (float) (view.getRight() - view.getLeft()) / 2;
            float y = (float) (view.getBottom() - view.getTop()) / 2;
            view.onTouchEvent(MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0));
            view.onTouchEvent(MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_UP, x, y, 0));
        }
    });
}

From source file:Main.java

public static String getMotionEventString(int action) {
    switch (action) {
    case (MotionEvent.ACTION_DOWN):
        return "action_down";
    case (MotionEvent.ACTION_UP):
        return "action_down";
    case (MotionEvent.ACTION_CANCEL):
        return "action_down";
    case (MotionEvent.ACTION_MOVE):
        return "action_move";
    case (MotionEvent.ACTION_OUTSIDE):
        return "action_outside";
    case (MotionEvent.ACTION_HOVER_ENTER):
        return "action_hover_enter";
    case (MotionEvent.ACTION_HOVER_EXIT):
        return "action_hover_exit";
    case (MotionEvent.ACTION_HOVER_MOVE):
        return "action_hover_move";
    case (MotionEvent.ACTION_MASK):
        return "action_mask";
    }//from   w  ww.  j a v  a  2 s.co  m
    return "unknown action event";
}

From source file:Main.java

public static void makeListViewWorkableInScollView(ListView lv) {
    lv.setOnTouchListener(new ListView.OnTouchListener() {

        @Override/*ww w. j  a v  a 2s.  co m*/
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
}

From source file:Main.java

public static void handleAutoCloseKeyboard(boolean isAutoCloseKeyboard, View currentFocusView,
        MotionEvent motionEvent, Object dialogOrActivity) {
    if (isAutoCloseKeyboard && motionEvent.getAction() == MotionEvent.ACTION_DOWN && currentFocusView != null
            && (currentFocusView instanceof EditText) && dialogOrActivity != null) {
        int[] leftTop = { 0, 0 };
        currentFocusView.getLocationInWindow(leftTop);
        int left = leftTop[0];
        int top = leftTop[1];
        int bottom = top + currentFocusView.getHeight();
        int right = left + currentFocusView.getWidth();
        if (!(motionEvent.getX() > left && motionEvent.getX() < right && motionEvent.getY() > top
                && motionEvent.getY() < bottom)) {
            if (dialogOrActivity instanceof Dialog) {
                closeKeyboard((Dialog) dialogOrActivity);
            } else if (dialogOrActivity instanceof Activity) {
                closeKeyboard((Activity) dialogOrActivity);
            }//  w ww.  jav  a 2s.co  m
        }
    }
}

From source file:Main.java

public static List<MotionEvent> createMotionEvents(final AbsListView absListView, final float fromY,
        final float toY) {
    int x = (int) (absListView.getX() + absListView.getWidth() / 2);

    List<MotionEvent> results = new ArrayList<>();
    results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
            MotionEvent.ACTION_DOWN, x, fromY, 0));

    float diff = (toY - fromY) / 25;
    float y = fromY;
    for (int i = 0; i < 25; i++) {
        y += diff;/* w ww  .j a v a  2 s .  com*/
        results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                MotionEvent.ACTION_MOVE, x, y, 0));
    }
    results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
            MotionEvent.ACTION_UP, x, toY, 0));

    return results;
}

From source file:Main.java

public static void setTextWithLinks(TextView textView, String htmlText) {
    setHtmlText(textView, htmlText);// ww w. j ava2s .  c o m
    textView.setOnTouchListener((v, event) -> {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            TextView widget = (TextView) v;
            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = Spannable.Factory.getInstance().newSpannable(widget.getText()).getSpans(off,
                    off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                }
                return true;
            }
        }
        return false;
    });
}

From source file:Main.java

/**
 * Gets the name of an action.//from  www  .  j  a  v a 2  s .c  o  m
 * 
 * @param action        The action being performed.
 * @param isMotionEvent Whether or not the action is a motion event.
 * 
 * @return The name of the action being performed.
 */
public static String getActionName(int action, boolean isMotionEvent) {
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        return "DOWN";
    case MotionEvent.ACTION_UP:
        return "UP";
    case MotionEvent.ACTION_MOVE:
        return isMotionEvent ? "MOVE" : "MULTIPLE";
    case MotionEvent.ACTION_CANCEL:
        return "CANCEL";
    case MotionEvent.ACTION_OUTSIDE:
        return "OUTSIDE";
    case MotionEvent.ACTION_POINTER_DOWN:
        return "POINTER_DOWN";
    case MotionEvent.ACTION_POINTER_UP:
        return "POINTER_UP";
    case MotionEvent.ACTION_HOVER_MOVE:
        return "HOVER_MOVE";
    case MotionEvent.ACTION_SCROLL:
        return "SCROLL";
    case MotionEvent.ACTION_HOVER_ENTER:
        return "HOVER_ENTER";
    case MotionEvent.ACTION_HOVER_EXIT:
        return "HOVER_EXIT";
    default:
        return "ACTION_" + Integer.toString(action);
    }
}

From source file:Main.java

public static void setOnTouchBackgroundEffect(View view) {
    view.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (!(v.getBackground() instanceof TransitionDrawable))
                return false;

            TransitionDrawable transition = (TransitionDrawable) v.getBackground();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                transition.startTransition(500);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                transition.reverseTransition(500);
                break;
            }/*from   w ww.  j  av a  2 s . com*/

            return false;
        }
    });
}

From source file:Main.java

/**
 * push View ScaleAnimation/*from   ww  w  .  j  a  va2s .  co m*/
 *
 * @param targetView TargetView
 * @param action     MotionEventAction
 * @param scaleX
 * @param scaleY
 */
private static void pushScale(View targetView, int action, float scaleX, float scaleY) {
    if (action == MotionEvent.ACTION_DOWN) {
        // Touch Down
        ScaleAnimation anim = new ScaleAnimation(1.0f, scaleX, 1.0f, scaleY, targetView.getWidth() / 2,
                targetView.getHeight() / 2);
        anim.setDuration(DURATION);
        anim.setFillEnabled(true);
        anim.setFillAfter(true);
        targetView.startAnimation(anim);
    } else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Touch Up
        ScaleAnimation anim = new ScaleAnimation(scaleX, 1.0f, scaleY, 1.0f, targetView.getWidth() / 2,
                targetView.getHeight() / 2);
        anim.setDuration(DURATION);
        anim.setFillEnabled(true);
        anim.setFillAfter(true);
        targetView.startAnimation(anim);
    }
}