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:

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>();

	protected void onCreate(final Bundle savedInstanceState) {


		// 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()){
			public int getCount() {
				return FRAGMENTS;
			public Fragment getItem(final int position) {
				return _fragments.get(position);
			public CharSequence getPageTitle(final int position) {
				switch (position) {
					case FRAGMENT_ONE:
						return "Title One";
					case FRAGMENT_TWO:
						return "Title Two";
						return null;
		_viewPager = (ViewPager) findViewById(;



<ViewPager xmlns:android=""
    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:layout_gravity="top" />


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

public class FirstFragment extends Fragment {

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

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

		// Create UI components here.

		return view;



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:layout_height="wrap_content" />


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 =


  5. Vinayak Patil Reply

    Can you please tell me which packages to be import.. i mean whether

    i am getting some error :-(

Leave a Reply

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