Changing the speed of transition of ViewPager and setCurrentItem

viewpager smooth scroll speed android
android-viewpager change page from fragment
viewpager transition android
viewpager android
android-viewpager slow swipe
android-viewpager smooth scroll programmatically
viewpager2 scroll duration
horizontal viewpager android

I have a ViewPager that I want to rotate automatically every 5 seconds, whilst also allowing the user to swipe too. However, when I set the automatic change, the transition animation between pages happens really quickly, and I want this to be slower.

I've seen the answers for this question here:

Slowing speed of Viewpager controller in android

...but they all use reflection. Does anyone know of any way of slowing down the automatic speed of a ViewPager without using reflection?

I thought of using a PageTransformer, but not sure if that would work, and also it would probably affect the speed if the user swiped as well as the automatic one? Unless I can somehow detect the difference, and then do one or another PageTransformation?

Use this Custom View

public class ViewPagerCustomDuration extends ViewPager {
    private FixedSpeedScroller mScroller = null;

    public ViewPagerCustomDuration(Context context) {
        super(context);
        init();
    }

    public ViewPagerCustomDuration(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /*
     * Override the Scroller instance with our own class so we can change the
     * duration
     */
    private void init() {
        try {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            mScroller = new FixedSpeedScroller(getContext(),
                    new DecelerateInterpolator());
            scroller.set(this, mScroller);
        } catch (Exception ignored) {
        }
    }

    /*
     * Set the factor by which the duration will change
     */
    public void setScrollDuration(int duration) {
        mScroller.setScrollDuration(duration);
    }

    private class FixedSpeedScroller extends Scroller {

        private int mDuration = 500;

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

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

        public FixedSpeedScroller(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);
        }

        public void setScrollDuration(int duration) {
            mDuration = duration;
        }
    }
}

How to change ViewPager scroll animation duration and velocity, I have a ViewPager that I want to rotate automatically every 5 seconds, whilst also allowing the user to swipe too. However, when I set the automatic change, the  Questions: I have a ViewPager that I want to rotate automatically every 5 seconds, whilst also allowing the user to swipe too. However, when I set the automatic change, the transition animation between pages happens really quickly, and I want this to be slower. I’ve seen the answers for this question here: Slowing speed of

You create custom view pager

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import java.lang.reflect.Field;

public class CustomViewPager extends ViewPager {

    private FixedSpeedScroller mScroller = null;
    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }
        init();
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }
        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }


    /*
     * Override the Scroller instance with our own class so we can change the
     * duration
     */
    private void init() {
        try {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            mScroller = new FixedSpeedScroller(getContext(),
                    new DecelerateInterpolator());
            scroller.set(this, mScroller);
        } catch (Exception ignored) {
        }
    }

    /*
     * Set the factor by which the duration will change
     */
    public void setScrollDuration(int duration) {
        mScroller.setScrollDuration(duration);
    }

    private class FixedSpeedScroller extends Scroller {

        private int mDuration = 500;

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

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

        public FixedSpeedScroller(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);
        }

        public void setScrollDuration(int duration) {
            mDuration = duration;
        }
    }

}

you can set duration of view pager according to you

SLOW ANIMATION: VIEW PAGER TRANSFORMATION ANIMATION , setCurrentItem(position); There will show transaction animation for change position , but this animation is too fast. So I want change this animation  Changing the speed of transition of ViewPager and setCurrentItem (2) I have a ViewPager that I want to rotate automatically every 5 seconds, whilst also allowing the user to swipe too. However, when I set the automatic change, the transition animation between pages happens really quickly, and I want this to be slower.

use schedule timer option for auto rotate the pages.

int i = 0;
static Timer timer = new Timer();

//Scroller scroll = new Scroller(this);

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.simple_circles);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);

    mPager.setAdapter(mAdapter);

     mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);

    mIndicator.setViewPager(mPager);

    mPager.setCurrentItem(i);

    SwitchPage(3);


  }

public void SwitchPage(int seconds) 

{

if(timer != null)
{
    timer.cancel();
}

timer = new Timer(); // At this line a new Thread will be created

timer.schedule(new SwitchPageTask(), 
2000, seconds * 2000);               
 // delay in milliseconds
 }


class SwitchPageTask extends TimerTask 
{

@Override
public void run() {

    runOnUiThread(new Runnable() {

        public void run() {

            if(i < mAdapter.getCount())

            {

                i++;

                mPager.setCurrentItem(i, true);

            }
            else
            {

                i=0;

                mPager.setCurrentItem(i, true);
            }
        }
    });
}
}

just cancel the timer like.. timer.cancel().. or
cancel timer from another activity or fragment activity..

    textView1_page3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (SampleCirclesDefault.timer != null) {
                    SampleCirclesDefault.timer.cancel();
                    Toast.makeText(getActivity(), "timer
                  cancled", 1).show();
                }

            }
        });

Spicy up your ViewPager (part 1): tune the scroll duration. – Antonio , Is there a way to set manually the animation speed? ViewPager { // The speed of the scroll used by setCurrentItem() private void animatePagerTransition(final boolean forward) { ValueAnimator animator = ValueAnimator. Questions: I’m changing slide with viewPager.setCurrentItem(index++, true); But it changes to fast. Is there a way to set manually the animation speed? Searched and only found somebody asking the same everywhere (besides StackOverflow, maybe I have luck here :)), without answers.

Slide between fragments using ViewPager, // // The 0.5, 1.0, 2.0, 2.5 values you see here are what makes the view move in a different speed. // // The bigger the number, the faster the view  ViewPager. It works perfectly, the only thing that I want to do is change the speed of the slide animation when the setCurrentItem is called with the parameter smoothScroll true. It's too fast and in some devices it looks a little jerky. Any suggestion? Thanks!-- -- You received this message because you are subscribed to the Google

Viewpager animation speed, In the series of posts entitled “Spice up your ViewPager” I am going to walk thought some simple of the app by controlling for example the transition from one page to the other. However, controlling the speed is not all. that on the first row of widgets you can change the duration of the horizontal scroll. Change ViewPager animation duration when sliding programmatically (4) Better solution is to simply access the private fields by creating the class in the support package. EDIT This is bound to the MAX_SETTLE_DURATION of 600ms, set by the ViewPager class.

Look Deep Into ViewPager2, ViewPager objects can animate screen slides automatically. Here's what a screen slide looks like that transitions from one screen of content to the next: Screen  In this android kotlin source code example, we are going to change the default speed of ViewPager auto slide in Kotlin. You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel. Below is a step by step source code to change the default speed of ViewPager auto slide in Kotlin.

Comments
  • I don't actually remember why I asked this or how I solved it, but this answer has enough up ticks, that I'm willing to accept it as a correct answer. Also, @Ezio seems to think it works :D
  • Excellent solution.
  • Why wouldn't it be adjustable at least for component in Compat library? It's 2018, I think we deserve to have ability to customize animation speed by now...
  • @DenRimus So true.. unbelievable.
  • @RussWheeler: In your question text you explicitly ask for a solution without reflection, and this solution uses reflection to access a private field. Worse yet, the poster doesn't even bother to point that out. I don't think you should accept this answer. The question is still valid and unanswered.
  • harikrishnan looking through your code it looks like your changing the delay in between the change. That's not what I have a problem with. I think the actual animation of the auto transition is too quick, it's almost instant, I want it to scroll from one image to the next slower, not at a variable time difference. I'll update my original question to make that clearer.