Search code examples
androidandroid-layoutkotlinandroid-tablayout

How to disable tabLayout scroll animation while scrolling it programetically?


I am changing the position of tab layout programmatically when an activity starts but while changing tab layout position parametrically it gives scroll animation from 0th tab to the tab which I am switching to

Code which I am using is

new Handler().postDelayed(() -> Objects.requireNonNull
(binding.tabLayout.getTabAt(5)).select(), 100);

I have also tried by

binding.tabLayout.setSmoothScrollingEnabled(false);

Still every time it gives scrolling animation which I don't want. Any possibility to achieve this?


Solution

  • Are you referring to the underline scrolling animation of the TabLayout? you can set

    app:tabIndicatorHeight="0dp"
    

    As an example:

    <android.support.design.widget.TabLayout
        android:id="@+id/comtabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:tabIndicatorHeight="0dp"
        app:tabPaddingStart="0dp"
        app:tabPaddingEnd="0dp"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:background="@android:color/white" />
    

    But if you referring to the content of the tab that happen because of the viewpager, so when clicking on the tab you need to use this method of the viewpager :

    mViewPager.setCurrentItem(position, false)
    

    2nd parameter is smoothScroll if false, you're disabling the scroll animation