Search code examples
javaandroidviewswipegesture

Simulating a gesture


I'm trying to simulate the swipe-to-right gesture in Android with this method:

private void addGestures () {
    final int[] x1 = {0};
    final int[] x2 = {0};
    final int[] y1 = {0};
    final int[] y2 = {0};
    final int[] t1 = {0};
    final int[] t2 = {0};

    View myView = findViewById(R.id.detail_activity);
    myView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x1[0] = (int)event.getX();
                    y1[0] = (int) event.getY();
                    t1[0] = (int) System.currentTimeMillis();
                    return true;
                case MotionEvent.ACTION_UP:
                    x2[0] = (int) event.getX();
                    y2[0] = (int) event.getY();
                    t2[0] = (int) System.currentTimeMillis();

                    if (x2[0] > x1[0]) {
                        onBackPressed();
                    }
                    return true;
                case MotionEvent.ACTION_MOVE:
                    return true;
            }
            return false;
        }
    });
}

The problem is that now, as soon as I touch the screen, it gets recognized as a Gesture and the onBackPressed method gets invoked. I added a third variable to check the time the user takes to touch the screen. How can I implement them to recognize the swipe?


Solution

  • Forget what you've done.

    Add this listener to your project:

    package com.example.testproject.listeners;
    import android.content.Context;
    import android.view.GestureDetector;
    import android.view.GestureDetector.SimpleOnGestureListener;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    
    public class OnSwipeTouchListener implements OnTouchListener {
    
        private final GestureDetector gestureDetector;
    
        public OnSwipeTouchListener (Context ctx){
            gestureDetector = new GestureDetector(ctx, new GestureListener());
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    
        private final class GestureListener extends SimpleOnGestureListener {
    
            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
    
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                boolean result = false;
                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                            result = true;
                        }
                    }
                    else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                        result = true;
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }
    
        public void onSwipeRight() {
            //Called when swiping right to all views that implement the listener
        }
    
        public void onSwipeLeft() {
            //Called when swiping left to all views that implement the listener
        }
    
        public void onSwipeTop() {
            //Called when swiping top to all views that implement the listener
        }
    
        public void onSwipeBottom() {
            //Called when swiping bottom to all views that implement the listener
        }
    }
    

    Then to implement it:

    yourView.setOnTouchListener(new OnSwipeTouchListener(this) {
                public void onSwipeTop() {
                    //Called when swiping top on this view
                }
                public void onSwipeRight() {
                    //Called when swiping right on this view
                }
                public void onSwipeLeft() {
                    //Called when swiping left on this view
                }
                public void onSwipeBottom() {
                    //Called when swiping bottom on this view
                }
    
            });
    

    This is a great and modular solution! If you need to tune the swiping gestures to your liking, change the value of

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    

    in the listener.