Introduction

ViewPager is a simple layout manager to mover through views using the simple left and right swipes.
It was not available till android SDK 11 so we will need to import the android support library v4 or v13
It is used in managing fragments but in this article, we are going to implement a simple ImageView swipe.
This demo uses 4 images android1.jpg .... android4.jpg so first get these images or download the source code
It also uses a background resource from an earlier article android buttons background
Now let's start, create a new project, import the android v4 support library
Now open activity_main.xml file
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7. <android.support.v4.view.ViewPager
  8. android:id="@+id/viewPager"
  9. android:layout_height="match_parent"
  10. android:layout_width="match_parent"
  11. android:layout_weight="1"
  12. />
  13. <LinearLayout
  14. android:layout_height="50dp"
  15. android:layout_width="match_parent"
  16. android:background="#C799AA"
  17. >
  18. <Button
  19. android:id="@+id/previousButton"
  20. android:layout_height="wrap_content"
  21. android:layout_width="80dp"
  22. android:padding="10dp"
  23. android:text="Previous"
  24. android:textSize="14sp"
  25. android:background="@drawable/button_background"
  26. android:layout_margin="5dp"
  27. android:textColor="#ffffff"
  28. android:enabled="false"
  29. />
  30. <TextView
  31. android:id="@+id/positionTextView"
  32. android:layout_height="wrap_content"
  33. android:layout_width="fill_parent"
  34. android:gravity="center"
  35. android:layout_weight="1"
  36. android:padding="5dp"
  37. android:layout_gravity="center_vertical"
  38. android:textColor="#000000"
  39. android:textSize="15sp"
  40. />
  41. <Button
  42. android:id="@+id/nextButton"
  43. android:layout_height="wrap_content"
  44. android:layout_width="80dp"
  45. android:padding="10dp"
  46. android:text="Next"
  47. android:textSize="14sp"
  48. android:background="@drawable/button_background"
  49. android:layout_margin="5dp"
  50. android:textColor="#ffffff"
  51. />
  52. </LinearLayout>
  53. </LinearLayout>
Here android.support.v4.view.ViewPager is the tag for the ViewPager as it is imported from the android v4 support library
Rest is self-explanatory i.e. two Buttons for next and previous, a TextView for indicating the position.
Now open MainActivity.java
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.support.v4.view.PagerAdapter;
  4. import android.support.v4.view.ViewPager;
  5. import android.support.v4.view.ViewPager.OnPageChangeListener;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.view.ViewGroup;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout.LayoutParams;
  13. import android.widget.TextView;
  14. public class MainActivity extends Activity {
  15. private int[] images = {R.drawable.android1, R.drawable.android2, R.drawable.android3, R.drawable.android4, R.drawable.android5};
  16. private ViewPager mViewPager;
  17. private MyPagerAdapter myPagerAdapter;
  18. private Button nextButton, previousButton;
  19. private TextView positinTextView;
  20. private int imageCount = images.length;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. /*
  26. * Initialize every Object
  27. */
  28. myPagerAdapter = new MyPagerAdapter();
  29. previousButton = (Button)findViewById(R.id.previousButton);
  30. nextButton = (Button)findViewById(R.id.nextButton);
  31. positinTextView = (TextView)findViewById(R.id.positionTextView);
  32. mViewPager = (ViewPager)findViewById(R.id.viewPager);
  33. mViewPager.setAdapter(myPagerAdapter);
  34. /*
  35. * ViewPager's setCurrentItem(int) method is used here to set the first image although not required
  36. */
  37. mViewPager.setCurrentItem(0);
  38. positinTextView.setText("1 of "+imageCount);
  39. /*
  40. * Set OnPageChangeListener
  41. * In onPageSelected enable/disable next/previous buttons and set the value in poistionTextView
  42. */
  43. mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
  44. @Override
  45. public void onPageSelected(int position) {
  46. // TODO Auto-generated method stub
  47. positinTextView.setText((position+1)+" of "+imageCount);
  48. if (position==imageCount-1) {
  49. nextButton.setEnabled(false);
  50. }
  51. else {
  52. nextButton.setEnabled(true);
  53. }
  54. if (position==0) {
  55. previousButton.setEnabled(false);
  56. }
  57. else {
  58. previousButton.setEnabled(true);
  59. }
  60. }
  61. @Override
  62. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  63. }
  64. @Override
  65. public void onPageScrollStateChanged(int state) {
  66. }
  67. });
  68. /*
  69. * Set OnClickListener on buttons to change the Image in ViewPager
  70. */
  71. nextButton.setOnClickListener(new OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. // TODO Auto-generated method stub
  75. mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
  76. }
  77. });
  78. previousButton.setOnClickListener(new OnClickListener() {
  79. @Override
  80. public void onClick(View v) {
  81. // TODO Auto-generated method stub
  82. mViewPager.setCurrentItem(mViewPager.getCurrentItem()-1);
  83. }
  84. });
  85. }
  86. @Override
  87. public boolean onCreateOptionsMenu(Menu menu) {
  88. // Inflate the menu; this adds items to the action bar if it is present.
  89. getMenuInflater().inflate(R.menu.main, menu);
  90. return true;
  91. }
  92. /*
  93. * Pager Adapter for ViewPager
  94. * We have to override 4 methods as done below
  95. */
  96. private class MyPagerAdapter extends PagerAdapter {
  97. public MyPagerAdapter(){}
  98. /*
  99. * Returns the
  100. */
  101. @Override
  102. public int getCount() {
  103. return imageCount;
  104. }
  105. @Override
  106. public View instantiateItem(ViewGroup container, int position) {
  107. /*
  108. * We dynamically create an ImageView but we could have inflated a layout if needed
  109. */
  110. ImageView mImageView = new ImageView(MainActivity.this);
  111. mImageView.setImageResource(images[position]);
  112. // Now just add ImageView to ViewPager and return it
  113. container.addView(mImageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  114. return mImageView;
  115. }
  116. @Override
  117. public void destroyItem(ViewGroup container, int position, Object object) {
  118. container.removeView((View) object);
  119. }
  120. @Override
  121. public boolean isViewFromObject(View view, Object object) {
  122. return view == object;
  123. }
  124. }
  125. }
Please read the comments in between the code lines for more explanation
Google Documentation: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
Article4Img1.png
Article4Img2.png
Article4Img3.png