You can do it without any third party library
Final Result:
1) Use frame layout as a container for Viewpager and then add a Linearlayout at the bottom of it.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax">
<android.support.v4.view.ViewPager
android:id="@+id/product_images_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="10dp">
<LinearLayout
android:id="@+id/indicator_root"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
2) Define size and margin for indicators
//define globaly
private LinearLayout.LayoutParams imageParam;
//init params
int margin = Utils.pxFromDp(getActivity(), 5);
int width = Utils.pxFromDp(getActivity(), 8);
imageParam = new LinearLayout.LayoutParams(width, width);
imageParam.setMargins(margin, margin, margin, margin);
3) Add Indicators in Linear Layout
for (int indicatorCount = 0; indicatorCount < productFromShoppingList.getProductImages().size();
indicatorCount++) {
ImageView imageIndicator =
new ImageView(getActivity());
imageIndicator.setAdjustViewBounds(true);
imageIndicator.setScaleType(ImageView.ScaleType.FIT_XY);
imageIndicator.setLayoutParams(imageParam);
indicatorContainer.addView(imageIndicator);
indicators.add(imageIndicator);
imageIndicator.setBackgroundResource(R.drawable.indicator_unselected);
}
4) Initialize indicator at 0 position
indicators.get(0).setBackgroundResource(R.drawable.indicator_unselected);
5) Update indicator on view pager page change
carousalViewPager.setAdapter(new slidingPagerAdapter(getActivity(),
productFromShoppingList.getProductImages()));
carousalViewPager
.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
currentPageIndex = position;
updateIndicators(currentPageIndex);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
private void updateIndicators(int selectedPostion) {
for (int indicatorPosition = 0; indicatorPosition < indicators.size(); indicatorPosition++) {
indicators.get(indicatorPosition).setBackgroundResource(indicatorPosition == selectedPostion ? R.drawable.indicator_selected
: R.drawable.indicator_unselected);
}
}
Last but not least add this 2 drawable for indicator
indicator_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="@color/white" />
</shape>
indicator_selected.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="100dp" />
<solid android:color="@android:color/white" />
</shape>