androidandroid-tabsandroid-tablayoutandroid-scrollable-tabs

How to scroll tablayout programmatically - Android


I have created 30 scrollable tabs using tablayout.

So first three tabs are visible on screen and rest of them are invisible which can be scroll using swipe gesture.

The problem is when I am selecting last tab programmatically but it is not get visible (tab layout not get scrolled to last tab).

How can I make tablayout to scroll to last tab?


Solution

  • I found the solution.

    First I had found the width of tablayout and scroll it's x position to width and than called the select() method of last tab.

    And it works fine.

    Below is the code.

    mTabLayout.setScrollX(mTabLayout.getWidth());
    mTabLayout.getTabAt(lastTabIndex).select();
    

    Updated:

    If above is not working you can use the below code as well, it is also working fine.

    new Handler().postDelayed(
            new Runnable() {
                @Override public void run() {
                    mTabLayout.getTabAt(TAB_NUMBER).select();
                }
            }, 100);