Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabLayout tab selection

How should I select a tab in TabLayout programmatically?

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
 tabLayout.setupWithViewPager(viewPager);
like image 487
Kid24 Avatar asked Sep 26 '22 14:09

Kid24


1 Answers

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.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();

This technique works even if you're using the TabLayout by itself without a ViewPager (which is atypical, and probably bad practice, but I've seen it done).

like image 494
Firefly Avatar answered Oct 08 '22 10:10

Firefly