Android ViewPager Prev/Next Button
Button:
Button yourButton = (Button)findViewById(R.id.button1);
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mViewPager.setCurrentItem(getItem(+1), true); //getItem(-1) for previous
}
});
Function:
private int getItem(int i) {
return mViewPager.getCurrentItem() + i;
}
Hope this helps :)
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
yourViewPager.setCurrentItem(page, smoothScroll);
}
});
1) Make layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/white"
android:weightSum="1">
<android.support.v4.view.ViewPager
android:id="@+id/images_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v4.view.ViewPager>
<ImageView
android:id="@+id/img_next"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/forward_white"
android:background="@color/colorPrimary"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
/>
<ImageView
android:id="@+id/img_previous"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/back_white"
android:background="@color/colorPrimary"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
</RelativeLayout>
2) Set custom Adapter
CustomViewPagerAdapter custompageradpter;
ViewPager mViewPager;
mViewPager = (ViewPager)shareImagesDialouge.findViewById(R.id.images_pager);
custompageradpter = new CustomViewPagerAdapter(this);
mViewPager.setAdapter(custompageradpter);
public class CustomViewPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
int[] mResources = {
R.drawable.emoji_1,
R.drawable.emoji_2,
R.drawable.emoji_3,
R.drawable.emoji_4,
R.drawable.emoji_5,
R.drawable.emoji_6
};
public CustomViewPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
3) For next and previous button
case R.id.img_previous:
mViewPager.setCurrentItem(getItemofviewpager(-1), true);
break;
case R.id.img_next:
mViewPager.setCurrentItem(getItemofviewpager(+1), true);
break;
4) Make this function
private int getItemofviewpager(int i) {
return mViewPager.getCurrentItem() + i;
}
A failproof upgrade to sweggersen's and Alex Orlov's answer,
// For scrolling to next item
nextPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(getNextPossibleItemIndex(1), true);
}
});
// For scrolling to previous item
previousPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(getNextPossibleItemIndex(-1), true);
}
});
Add getNextPossibleItemIndex() method in your Activity/Fragment,
private int getNextPossibleItemIndex (int change) {
int currentIndex = viewPager.getCurrentItem();
int total = viewPager.getAdapter().getCount();
if (currIndex + change < 0) {
return 0;
}
return Math.abs((currentIndex + change) % total) ;
}
This way, you will be able to change currentItem without worrying about IndexOutOfBoundsException as suggested by Heinrich's comment. Note that using this method, items will be displayed like a circular list.
1 -> 2 -> 3 -> 1 -> 2 -> 3 -> 1
But going back, it will stop at first item
1 <- 1 <- 2 <- 3