0

I am working with a project where I want to go from one activity to another by swiping finger on the screen.Swiping from left to right means going to next activity.Swiping from right to left means going back to previous activity.And as i want to use these feature repeatedly,i don't want to repeat the whole code everytime.

I have googled it and found that move activity to another activity using Finger

But it didn't seem too helpful because of lack of perfect ans.

Community
  • 1
  • 1

2 Answers2

1

Instead of creating so many activities why don't you consider using View Pager with Fragment Pager Adapter.

You will have multiple fragments which will be available on right swipe and left swipe. It will be very efficient as well. You will be able to see the animation.

But in case of activity you will have to detect the swipe and then launch another activity.

Also, Have a look at below link : How to implement a ViewPager with different Fragments / Layouts

Community
  • 1
  • 1
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
0

You should implement OnTouchListener and set it in onCreate method.

view.setOnTouchListener(new MyOnTouchListener(this));

and here are the listeners:

public class MyOnTouchListener extends View.OnTouchListener { 
   final GestureDetector gesture;

   public MyOnTouchListener(Activity activity) {
       gesture = new GestureDetector(activity, new MyGestureListener());
   }

   @Override
   public boolean onTouch(View v, MotionEvent event) {
       return gesture.onTouchEvent(event);
   }  
}

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        final int SWIPE_MIN_DISTANCE = 120;
        final int SWIPE_MAX_OFF_PATH = 250;
        final int SWIPE_THRESHOLD_VELOCITY = 200;

        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                return false;
            }

            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                // TODO action for right to left swipe
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                // TODO action for left to right swipe
            }
        } catch (Exception e) {
            // nothing
        }

        return super.onFling(e1, e2, velocityX, velocityY);
    }
}
Igor Bljahhin
  • 939
  • 13
  • 23