1

I need to detect swipe direction in my code. I can detect the direction but it comes like if I swipe right top right or left-top like that coming. same for the left, my requirement is without lifting finger if i swipe left it should come only left ,likewise all the direction. Can anyone help me out. Thanks in advance!

@Override public boolean onTouchEvent(MotionEvent touchevent) {

    switch (touchevent.getAction()) {
        // when the user first touches the screen we get x and y coordinate
        case MotionEvent.ACTION_DOWN: {
            x1 = touchevent.getX();
            y1 = touchevent.getY();
            break;
        }

        case MotionEvent.ACTION_MOVE: {


            x2 = touchevent.getX();
            y2 = touchevent.getY();


            float deltaX = x2 - x1;


            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // Left to Right swipe action
                if (x2 > x1) {;
                    Log.e("RTL", "Right to Left Swap Performed");
                }

                else {
                    Log.e("LTR", "Left to Right Swap Performed");
                }

            } else {

                if (y2 > y1) {

                    Log.e("UTD", "UP to Down Swap Performed");
                }

                // Right to left swipe action
                else {
                    Log.e("DTU", "Down to UP Swap Performed");
                }
            }
        }
    }
    return false;
}
Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56
user2806221
  • 101
  • 1
  • 11
  • http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures https://developer.android.com/training/gestures/detector.html – cokceken Nov 29 '16 at 14:03
  • @cokceken move is working..but i cant get exact left, right, up and down – user2806221 Nov 29 '16 at 14:10
  • I don't think there is a function that returns direction. You should do the math. Calculate the move vector, calculate angle. If your vector is bigger than X and your angle is between Z-Y then user swiped direction1. – cokceken Nov 29 '16 at 14:13
  • @cokceken the above edited code i have used ... i set MIN_DISTANCE = 150 – user2806221 Nov 29 '16 at 14:16
  • 1
    Would recommend using Sensey library (https://github.com/nisrulz/sensey) for something like this. – John O'Reilly Nov 29 '16 at 14:38

2 Answers2

3

Create a GestureDetectorCompat object

GestureDetectorCompat gestureDetectorCompat;

and override onTouchEvent in the activity

 @Override
        public boolean onTouchEvent(MotionEvent event) {
            gestureDetectorCompat.onTouchEvent(event);
            return true;
        }

or if you want to detect on some view then you can Override onTouch

someView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                gestureDetectorCompat.onTouchEvent(motionEvent);
                return false;
            }
        });

and initialize gestureDetectorCompat as follows somewhere preferably in onCreate() and you are done.

gestureDetectorCompat = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            float angle = (float) Math.toDegrees(Math.atan2(e1.getY() - e2.getY(), e2.getX() - e1.getX()));

            if (angle > -45 && angle <= 45) {
                Log.d(DEBUG_TAG, "Right to Left swipe performed");
                return true;
            }

            if (angle >= 135 && angle < 180 || angle < -135 && angle > -180) {
                Log.d(DEBUG_TAG, "Left to Right swipe performed");
                return true;
            }

            if (angle < -45 && angle >= -135) {
                Log.d(DEBUG_TAG, "Up to Down swipe performed");
                return true;
            }

            if (angle > 45 && angle <= 135) {
                Log.d(DEBUG_TAG, "Down to Up swipe performed");
                return true;
            }

            return false;
        }


    });
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
1

You should calculate angle then decide with that. If the vector is bigger than your min distance value, then you can check for angle.

float angle = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
if(angle > 45 && angle <= 135) return UP;
else if(angle > 135 && angle <= 225) return LEFT;
else if(angle > 225 && angle <= 315) return DOWN;
else return RIGHT;
cokceken
  • 2,068
  • 11
  • 22