0

I am doing a Intro Activity that consists in a TabLayout with three fragments.

I have two buttons below my TabLayout - one for "Skip" the intro and the other for "Next" screen/tab. They are working fine but I would like to make some changes on the "Next" function:

When I click the "Next" button he does this:

tabLayout.getTabAt(tabLayout.getSelectedTabPosition() + 1).select()

And he is indeed selecting the next tab, but I was wondering if I could slow down the slide/swap animation of the tab. Can I somewhat Override de animation so I can slow it down?

EDIT

I created a custom class that extends TabLayout:

public class CustomTabLayout extends TabLayout {

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

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

    public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
        super(context, attrs, defStyleAttr); 
    }
}

And I noticed that the TabLayout class has this method:

 private void animateToTab(int newPosition) {
    if (newPosition == Tab.INVALID_POSITION) { return; }

    if (getWindowToken() == null || !ViewCompat.isLaidOut(this) || mTabStrip.childrenNeedLayout()) {
        setScrollPosition(newPosition, 0f, true);
        return;
    }

    final int startScrollX = getScrollX();
    final int targetScrollX = calculateScrollXForTab(newPosition, 0);

    if (startScrollX != targetScrollX) {
        ensureScrollAnimator();

        mScrollAnimator.setIntValues(startScrollX, targetScrollX);
        mScrollAnimator.start();
    }

    mTabStrip.animateIndicatorToPosition(newPosition, ANIMATION_DURATION);
}

The problem is that ANIMATION_DURATION is a private static final constant set to 300 in the TabLayout class. Can I somewhat change this value to 1000 for example to slow down the animation?

EDIT 2

As suggested by azizbekian, I am trying to change this value using reflection:

public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setAnimationDuration();
}

static void setAnimationDuration() {
    try {
        Field field = TabLayout.class.getDeclaredField("ANIMATION_DURATION");
        field.setAccessible(true);
        field.set(field.get(TabLayout.class), 2000);

        int v = (int) field.get(TabLayout.class);
        //  v == 2000; so I guess that I changed the value successfully
    } catch (Exception e) { e.printStackTrace(); }
}

But when my tabs are created, it still has the same transaction duration (to fast). Am I doing something wrong with reflection? Or this "ANIMATION_DURATION" is not the correct value to change?

Ravers
  • 988
  • 2
  • 14
  • 45

1 Answers1

0

As you can see in sources, animation duration is not customizable:

private static final int ANIMATION_DURATION = 300;
...
mScrollAnimator.setDuration(ANIMATION_DURATION);

So, it cannot be configured neither from styles (xml), nor through Java code.

Only thing you can try to do is to change the value of private static final field through reflection.

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249