Courtesy: GitHub

Android HorizontalPaging Sample

This sample shows how to implement tabs, using Fragments and a ViewPager.


This sample implements tabs using the deprecated ActionBar.TabListener. It uses ViewPager and FragmentPagerAdapter to handle swiping between tabs and displaying the selected tab content.

  1. Create an Activity that extends FragmentActivity, with a ViewPager for its layout.
  2. Implement ActionBar.TabListener interface.
  3. Create a class that extends FragmentPagerAdapter and override its getItem(int position), getCount() and getPageTitle(int position) methods.
  4. In the onCreate(Bundle savedInstanceState) method of your activity, set navigation mode to tabs for the ActionBar using setNavigationMode(ActionBar.NAVIGATION_MODE_TABS). Note: This is DEPRECATED as of Android Lollipop.
  5. Set your custom FragmentPagerAdapter on your ViewPager.
  6. Implement setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()) on your ViewPager to know the selected tab, so you can update your ActionBar with setSelectedNavigationItem(position).


  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository


Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.


If you've found an error in this sample, please file an issue:

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see for more details.


Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile ""

compile ""

compile ""

compile ""

compileSdkVersion 26

minSdkVersion 11

targetSdkVersion 26


versionCode 1

versionName 1.0


versionCode 1

versionName 1.0



The {@link} that will provide fragments for each of the sections. We use a {@link} derivative, which will keep every loaded fragment in memory. If this becomes too memory intensive, it may be best to switch to a {@link}.

The {@link ViewPager} that will host the section contents.

Create the activity. Sets up an {@link} with tabs, and then configures the {@link ViewPager} contained inside R.layout.activity_main. *

A {@link SectionsPagerAdapter} will be instantiated to hold the different pages of fragments that are to be displayed. A {@link} will also be configured to receive callbacks when the user swipes between pages in the ViewPager. * @param savedInstanceState

Update {@link ViewPager} after a tab has been selected in the ActionBar. * @param tab Tab that was selected. @param fragmentTransaction A {@link} for queuing fragment operations to execute once this method returns. This FragmentTransaction does not support being added to the back stack.

Unused. Required for {@link}.

Unused. Required for {@link}.

A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the sections/tabs/pages. This provides the data for the {@link ViewPager}.

Get fragment corresponding to a specific position. This will be used to populate the contents of the {@link ViewPager}. * @param position Position to fetch fragment for. @return Fragment for specified position.

Get number of pages the {@link ViewPager} should render. * @return Number of fragments to be rendered as pages.

Get title for each of the pages. This will be displayed on each of the tabs. * @param position Page to fetch title for. @return Title for specified page.

A dummy fragment representing a section of the app, but that simply displays dummy text. This would be replaced with your application's content.

The fragment argument representing the section number for this fragment.