Title: Swipe Between Activities
Tags: android;android-activity;fragment;swipe
Question: I have 2 activities where i want to switch between them with swipe, i've done a lot of research on google but couldn't find a solution, as i am working with bitmaps (images) i have most of code written inside the onCreate() method of Activity, is there any solution for this, or how can i convert the activity like it is into a fragment

Comment: Why dont you use Fragments with ViewPager?

Comment: It's not hard to convert, just a little changes in activities will do the job! Good luck

Comment: i have created an activity to display sdcard images and to select from them so i can display in the other activity (edit activity) where i can edit them, i don't know if i can convert activity into fragment exactly like it is now

Here is the accepted answer: You can do it by using GestureDetector. Below is the sample snippet.

```// You can change values of below constants as per need.
private static final int MIN_DISTANCE = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOCITY = 100;
private GestureDetector mGestureDetector;

// write below code in onCreate method
mGestureDetector = new GestureDetector(context, new SwipeDetector());

// Set touch listener to parent view of activity layout
// Make sure that setContentView is called before setting touch listener.
findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Let gesture detector handle the event
                return mGestureDetector.onTouchEvent(event);
            }
        });


// Define a class to detect Gesture
private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e1 != null &amp;&amp; e2 != null) {
                float dy = e1.getY() - e2.getY();
                float dx = e1.getX() - e2.getX();

                // Right to Left swipe
                if (dx &gt; MIN_DISTANCE &amp;&amp; Math.abs(dy) < MAX_OFF_PATH &amp;&amp;
                        Math.abs(velocityX) &gt; THRESHOLD_VELOCITY) {
            // Add code to change activity  
                    return true;
                }

                // Left to right swipe
                else if (-dx &gt; MIN_DISTANCE &amp;&amp; Math.abs(dy) < MAX_OFF_PATH &amp;&amp;
                        Math.abs(velocityX) &gt; THRESHOLD_VELOCITY) {
            // Below is sample code to show left to right swipe while launching next activity
           currentActivity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
           startActivity(new Intent(currentActivity,NextActivity.class));
                    return true;
                }
            }
            return false;
        }
}

//Below are sample animation xml files.

anim/right_in.xml
<?xml version="1.0" encoding="utf-8"?&gt;
<set xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0" /&gt;
</set&gt;

anim/right_out.xml
<?xml version="1.0" encoding="utf-8"?&gt;
<set xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%p" /&gt;
</set&gt;
```

Comment for this answer: It is working fine , thank you, but what i want is to see the slide effect and the activity to be turned on already, after all these tries i am considering to convert every activity into fragments, thank you again !
Comment for this answer: i have already achieved the result i wanted using fragments, thanks again for answer !
Comment for this answer: For slide effect you can use animation and activity's overridePendingTransition method..Check my updated answer. but yes I agree using fragment is correct way.

Here is another answer: There are some libraries for you:


https://github.com/ikew0ng/SwipeBackLayout
https://github.com/liuguangqiang/SwipeBack
https://github.com/sockeqwe/SwipeBack



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:08/17/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-activity><fragment><swipe>', 'Title': 'Swipe Between Activities', 'ViewCount': 580, 'id': 32041551, 'user_AboutMe': '', 'user_AccountId': 6405625.0, 'user_DisplayName': 'Blerim Blerii', 'user_Reputation': 193.0, 'user_Views': 45.0, 'user_id': 4966954.0}}