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
If you know the index of the tab you want to select, you can do it like so: TabLayout tabLayout = (TabLayout) findViewById(R. id. tabs); TabLayout.
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);
This is how I solved it:
tabLayout.getTabAt(CurrentItem).getCustomView().setSelected(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With