Android ViewPager automatically change page

If you want to use thread in the main UI then you need to use a hander to hand it.

Handler handler = new Handler();

        Runnable update = new Runnable()  {

            public void run() {
                if ( currentPage == NUM_PAGES ) {

                    currentPage = 0;
                }
                featureViewPager.setCurrentItem(currentPage++, true);
            }
        };


        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(update);
            }
        }, 100, 500);

Easiest way how to solve it is to create an postDelayed runnable

    private Handler mHandler;
    public static final int DELAY = 5000;

    Runnable mRunnable = new Runnable()
    {

        @Override
        public void run()
        {
            //TODO: do something like mViewPager.setCurrentPage(mIterator);
            mHandler.postDelayed( mRunnable , DELAY );
        }
    };

as You can see it will loop infinitelly. When you want to stop it, just simply call

        mHandler.removeCallbacks( mRunnable );