Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 742
Pankaj Avatar asked Aug 08 '15 08:08

Pankaj


People also ask

How do I select a tab in Android?

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.


2 Answers

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); 
like image 140
Mihir Palkhiwala Avatar answered Oct 02 '22 23:10

Mihir Palkhiwala


This is how I solved it:

tabLayout.getTabAt(CurrentItem).getCustomView().setSelected(true); 
like image 24
Alireza Ghanbarinia Avatar answered Oct 02 '22 22:10

Alireza Ghanbarinia