Search code examples
androidandroid-tablayout

android.support.design.widget.TabLayout select Tab Programmatically


I am using android.support.design.widget.TabLayout. It has two tabs, If user selects second tab On particular condition I want user to redirect to first tab and disallow him to go to sencond tab until condition matches. To achieve this I tried,

tabLayout.getTabAt(0).select(); 

but it does not reselect first tab


Solution

  • This is because that view is still not initialized properly, and you are trying to perform some action.

    As a solution you just need to put one hadler before selecting perticular tab.

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