How to auto slide android View Pager

First Create Slider class extend with TimerTask

public class SliderTimer extends TimerTask {
    private ViewPager viewPager;
    private int size;
    private Activity activity;

    public SliderTimer(ViewPager viewPager, int size, Activity activity) {
        this.viewPager = viewPager;
        this.size = size;
        this.activity = activity;
    }

    @Override
    public void run() {
        activity.runOnUiThread(() -> {
            if (viewPager.getCurrentItem() < size - 1) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
            } else {
                viewPager.setCurrentItem(0, true);
            }
        });
    }
}

Next Create SpeedSlowScroller for slow scrolling

public class SpeedSlowScroller extends Scroller {

    private int mDuration = 2500;

    public SpeedSlowScroller(Context context) {
        super(context);
    }

    public SpeedSlowScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    public SpeedSlowScroller(Context context, Interpolator interpolator, boolean flywheel) {
        super(context, interpolator, flywheel);
    }


    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }
}

Finally add SpeedSlowScroller into ViewPager, and SliderTimer for auto scroll

try {
 Field mScroller = ViewPager.class.getDeclaredField("mScroller");
 mScroller.setAccessible(true);
 SpeedSlowScroller scroller = new SpeedSlowScroller(_context);
 mScroller.set(your_viewpager, scroller);
 Timer timer = new Timer();
 timer.scheduleAtFixedRate(new SliderTimer(your_viewpager, models.size(), activity), 4000, 6000);
 } catch (Exception ignored) {
 }

Setting smoothScroll = true in setCurrentItem(int item, boolean smoothScroll) does not always have the smooth scroll effect. Suppose if you have less than 5 pages in you viewpager, you will hardly notice the smooth scroll.

In this scenario, the hard way to do it is to put it in a for loop

//This will scroll page-by-page so that you can view scroll happening
for (int i = 0; i < mAdapter.getCount()-1; i++)
    mPager.setCurrentItem(i, true);

If some one needs more slower scroll, they can use postDelayed() like this...

static int i=0;
private final Handler handler = new Handler();
somefunction()
{
    handle.post(ViewPagerVisibleScroll);
}

    Runnable ViewPagerVisibleScroll= new Runnable() {
                @Override
                public void run() {
                    if(i <= mAdapter.getCount()-1)
                    {
                        mPager.setCurrentItem(i, true);
                        handle.postDelayed(TopChartAnimation, 100);
                        i++;
                    }
                }
            };

Sleep is always NOT recommended : If some one needs more slower scroll, they may use a sleep in this for loop...

@Override
public void onClick(View v) {
Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < mAdapter.getCount()-1; i++) {
                        final int value = i;
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mPager.setCurrentItem(value, true);
                            }
                        });
                    }
                }
            };
            new Thread(runnable).start();

        }

short answer 2020

After you set your viewPager adapter

   private Runnable runnable = null;
  
   public void function DisplaySlider(){

    sliderItemAdapter = new SliderItemAdapter(getActivity(), mSliderList);//push the data to the adapter
    mViewPager.setAdapter(sliderItemAdapter); //set the adapter to the view pager
    startAutoSlider(sliderItem.getCount());

   }


     

private void startAutoSlider(final int count) {

    runnable = new Runnable() {
        @Override
        public void run() {
            int pos = mViewPager.getCurrentItem();
            pos = pos + 1;
            if (pos >= count) pos = 0;
            mViewPager.setCurrentItem(pos);
            handler.postDelayed(runnable, 3000);
        }
    };
    handler.postDelayed(runnable, 3000);
}

   

And finally don`t forget to free the memory from the callbacks

@Override
public void onDestroy() {
    if (runnable != null) handler.removeCallbacks(runnable);
    super.onDestroy();
}

Probably, You should take a look at the following APIs ViewPager.beginFakeDrag(), ViewPager. fakeDragBy(float offset) and ViewPager.endFakeDrag() if You want drag simulation. Also, setCurrentItem() APIs provide ability to set current page and do it smoothly. Simplest way I could suggest to make slide automatically is to setup Handler with simple Runnable which would call pager methods for setting item in the activity and just do postDelayed() for it. And don't forget to call removeCallbacks() for it when user interaction or e.g. activity pause.