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

Pankaj picture Pankaj · Aug 8, 2015 · Viewed 29.1k times · Source

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

Answer

Mihir Palkhiwala picture Mihir Palkhiwala · Aug 10, 2015

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);