Custom ViewPager without Fragments

Happen to create a custom viewpager for my project . Thought of sharing it in my blog.

  • Approach is without using fragments.
  • Use Pageadapter to achieve the result.



        android:layout_height="wrap_content" />

public class MiniSlideViewer extends RelativeLayout {

    ViewPager viewPager;
    CirclePageIndicator pageIndicator;
    Animation slideUpAnimation;
    SplashScreenPrefs splashScreenPrefs;

    private static final float thresholdOffset = 0.5f;
    private boolean scrollStarted, checkDirection;

    public MiniSlideViewer(Context context) { // passing listview instance just to achieve the animation effect.
        this(context, null);

    public MiniSlideViewer(Context context, AttributeSet attrs) {
        super(context, attrs);

    void init(Context context) {

        inflate(context, R.layout.slide_view_pager, this);
        viewPager = (ViewPager) findViewById(;
        pageIndicator = (CirclePageIndicator) findViewById(;
        splashScreenPrefs = SplashScreenPrefs.getInstance();

     * setPageIndicatorFillColor - set page indicator fill color
     * @param fillColor
    public void setPageIndicatorFillColor(int fillColor) {

     * setPageIndicatorPageColor - set Page indicator page color
     * @param pageColor
    public void setPageIndicatorPageColor(int pageColor) {

    public void setViewPagerAdapter(MiniSlideAdapter viewPagerAdapter) {
        TrackingHelper.omnitureDeliveryAddressSlidePageLoad(getContext(), viewPager.getCurrentItem());

public class MiniSlideAdapter extends PagerAdapter {

    private final Context context;
    private static int[] miniViewerBgResources;
    private static int[] miniViewerImageResources;
    private static String[] miniViewerStringResources;

    HideMiniSlidesListener miniSlidesListener;

     * Callback for Skip click in dae slides .
    public interface HideMiniSlidesListener {
        void onMiniSliderSkipClick();

    public MiniSlideAdapter(Context context, int[] bgResources, int[] imgResources, String[] stringResources) {
        this.context = context;
        miniViewerBgResources = bgResources;
        miniViewerImageResources = imgResources;
        miniViewerStringResources = stringResources;
        miniSlidesListener = (HideMiniSlidesListener) context;

    public int getCount() {
        return miniViewerBgResources.length;

    public boolean isViewFromObject(View view, Object object) {
        return view == (object);

    public Object instantiateItem(ViewGroup container, int position) {

        LayoutInflater inflater = (LayoutInflater) context

        View view = inflater.inflate(R.layout.slide_page_content, container, false);

        ImageView daeImage = (ImageView) view.findViewById(;
        TextView daeInfoText = (TextView) view.findViewById(;
        View daeSlide = view.findViewById(;
        Button daeSkip = (Button) view.findViewById(;

        //show the skip button only in the last slide.
        if (position == getCount() - 1) {

        daeSkip.setOnClickListener(view1 -> miniSlidesListener.onMiniSliderSkipClick());

        daeSlide.setBackgroundColor(ContextCompat.getColor(context, miniViewerBgResources[position]));


        return view;

    public void destroyItem(ViewGroup container, int position, Object object) {
        (container).removeView((LinearLayout) object);


All good Now, just invoke in the activity;

final MiniSlideAdapter miniSlideAdapter = new MiniSlideAdapter(this, daeBgResources, daeImageResources, daeStringResources);

I have passed the array of Images and Strings. Just modify the code as per your needs.

Note: For Page Indicator, I have used PageIndicator library. Refer here

Happy coding 🙂

One thought on “Custom ViewPager without Fragments

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s