Android ViewPager Example

In this tutorial you will learn android horizontal view swiping with ViewPager. This feature has been showcased in several high profile applications like the Android Market application and the Google+ Android client. There are a number of classes in the Android compatibility package that can be used to implement horizontal page swiping behavior in your Android packages. The ViewPager control (android.support.v4.view.ViewPager) provides the horizontal swiping behavior. It can be used within your layouts much like a Gallery or other adapter-populated user interface control would be. The PagerAdapter (android.support.v4.view.PagerAdapter) class is used to define the data displayed by the ViewPager control.

Horizontal view paging is based upon APIs only available with the Android Compatibility package v4, Revision 3 ; these APIs are not available in the standard Android SDK at this time. Therefore, you will need to add the Android compatibility package to your Android project to access the appropriate APIs.

 

Step 1 :  Define ViewPager in layout (main.xml)

 

Step 2 : Create ViewPager Tab indicator layout (indicator.xml)

 

Step 3 :  Create ViewPager Activity (ViewPagerStyle1Activity.java)

I have used the FragmentActivity to define the view pager.

Step 4 :  Create ViewPager Adapter java file (ViewPagerAdapter.java)

 

Step 5 :  Create Xml Layout one (layout_one.xml)

 

Step 7 :  Create Java file (layout_two.xml)

Step 8 :Create Java file (LayoutOne.java)

 

Step 9 : Create Java file (LayoutTwo.java)

 

Step 10 : Define styles (values/styles.xml)

 

Download source code