detecting movements using onTouchEvent

Asked

Viewed 121 times

1

I found this method on the internet to detect movement, but I would like when it moves right it executes a command (example changes button color), but I’m having trouble implementing this code if anyone knows to give me a strength, I thank you from now on.

private String TAG = GestureActivity.class.getSimpleName();
float initialX, initialY;

@Override
public boolean onTouchEvent(MotionEvent event) {
    //mGestureDetector.onTouchEvent(event);

    int action = event.getActionMasked();

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            initialX = event.getX();
            initialY = event.getY();

            Log.d(TAG, "Action was DOWN");
            break;

        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "Action was MOVE");
            break;

        case MotionEvent.ACTION_UP:
            float finalX = event.getX();
            float finalY = event.getY();

            Log.d(TAG, "Action was UP");

            if (initialX < finalX) {
                Log.d(TAG, "Left to Right swipe performed");
            }

            if (initialX > finalX) {
                Log.d(TAG, "Right to Left swipe performed");
            }

            if (initialY < finalY) {
                Log.d(TAG, "Up to Down swipe performed");
            }

            if (initialY > finalY) {
                Log.d(TAG, "Down to Up swipe performed");
            }

            break;

        case MotionEvent.ACTION_CANCEL:
            Log.d(TAG,"Action was CANCEL");
            break;

        case MotionEvent.ACTION_OUTSIDE:
            Log.d(TAG, "Movement occurred outside bounds of current screen element");
            break;
    }

    return super.onTouchEvent(event);
}
  • 1

    Rodolfo, could you describe your difficulty? In your code, "move to the right" is the first if (you can replace Log.d(TAG, "Left to Right swipe performed"); by the code to change the button color or whatever you want.

  • 1

    I don’t quite understand your question. As I understand it, I think this link will answer your questions: http://stackoverflow.com/questions/6645537/how-to-detect-the-swipe-left-right-in-android Good luck!

  • The first line uses reflection to catch the class name GestureActivity. The parameter Motionevent is an event containing the current position and historical data of the movement. You must override this method onTouchEventin a Activity or create and link a OnTouchListener to your view. It in turn will create the event and shoot to your listener.

  • For more concrete help (to put your code to work), I suggest you create a mvce pointing exactly where your doubts lie.

  • @Anthonyaccioly worked code, but it does not work at first and need to make several attempts.

  • if you know another method for this purpose, and can help me

  • Rodolfo, my suggestion is to use the GestureDetector along with the GestureDetector.OnGestureListener, can detect the gesture by onFling and/or the onScroll, verifying the displacement at coordinate y.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.