Handle Swipe Gestures in Java Android: Right to Left (Forward)

Hi guys,

Instead of using a ViewPager you can handle touch gestures like this (Useful when you have only two views and you’d like to swipe only in a single direction – For example: Only Right to Left to go forward):

 //This method is called everytime the user touches the screen
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchevent.getX();
                y1 = touchevent.getY();
            case MotionEvent.ACTION_UP:
                x2 = touchevent.getX();
                y2 = touchevent.getY();

                if (x1>x2) {
                    Log.i(“log_tag”, “Right to left Swipe Detected!”);
        return false;