How to Enable Swiping Between Multiple Screens in Android (Using Fragments)

FragmentActivity is a helpful class for creating an application with multiple screens in which the user can swipe between. Rather than creating one activity per screen and defining the transition animations, the FragmentActivity class will handle all of the work for you.

All you have to do is define each screen as a Fragment.

Your main activity should extend the FragmentActivity class, and should define all of the Fragments like so:

MainActivity.java

/**
 *
 */
public class MainActivity extends FragmentActivity {

	/**
	 * Identifier for the first fragment.
	 */
	public static final int FRAGMENT_ONE = 0;

	/**
	 * Identifier for the second fragment.
	 */
	public static final int FRAGMENT_TWO = 1;

	/**
	 * Number of total fragments.
	 */
	public static final int FRAGMENTS = 2;

	/**
	 * The adapter definition of the fragments.
	 */
	private FragmentPagerAdapter _fragmentPagerAdapter;

	/**
	 * The ViewPager that hosts the section contents.
	 */
	private ViewPager _viewPager;

	/**
	 * List of fragments.
	 */
	private List<Fragment> _fragments = new ArrayList<Fragment>();

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		// Create fragments.
		_fragments.add(FRAGMENT_ONE, new FirstFragment());
		_fragments.add(FRAGMENT_TWO, new SecondFragment());

		// Setup the fragments, defining the number of fragments, the screens and titles.
		_fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){
			@Override
			public int getCount() {
				return FRAGMENTS;
			}
			@Override
			public Fragment getItem(final int position) {
				return _fragments.get(position);
			}
			@Override
			public CharSequence getPageTitle(final int position) {
				switch (position) {
					case FRAGMENT_ONE:
						return "Title One";
					case FRAGMENT_TWO:
						return "Title Two";
					default:
						return null;
				}
			}
		};
		_viewPager = (ViewPager) findViewById(R.id.pager);
		_viewPager.setAdapter(_fragmentPagerAdapter);
	}

}

activity_main.xml

<ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- This title strip will display the currently visible page title, as well as the page titles for adjacent pages. -->
    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</ViewPager>

For each Fragment, define its layout as you would normally define the layout of an Activity.
An example of a Fragment looks like so:

FirstFragment.java

public class FirstFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_one, null);

		// Create UI components here.

		return view;
	}

}

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

9 thoughts on “How to Enable Swiping Between Multiple Screens in Android (Using Fragments)

  1. nick Reply

    Hi i copied your code exactly and when i try running on an emulator it crashes. Could you specify how you setup your project and emulator.

  2. santosh Reply

    I tried with this code dude I couldnt make it work please give some more examples

  3. B Reply

    Really cool. But a downloadable version to see ALL the files working together would be great.
    Thank you.

  4. vipin yadav Reply

    you save my lot of time but activity_main xml show an error so i use =

    thanks

  5. Vinayak Patil Reply

    Can you please tell me which packages to be import.. i mean whether
    import android.support.v4.app.Fragment;
    OR
    import android.app.Fragment;

    i am getting some error :-(

Leave a Reply

Your email address will not be published. Required fields are marked *