I have a tab layout containing 3 tabs. I want to add 4th tab at run time and hide this tab after some time. Please let me know how to hide 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.
Get TabHost
from resource as
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
Then runtime use this
tabHost.getTabWidget().getChildAt(3).setVisibility(View.GONE);
Supposing that you are trying to hide 4th Tab.(So 3 is used
)
Well, I'm building an app that use a FragmentPagerAdapter as SectionsPagerAdapter in an activity. This activity was generated by AS and its layout has a TabLayout with some TabItems.
I wanted to hide one of them if var hide is false. Then I used:
Toolbar toolbar = findViewById(R.id.sectionBar);
if(!hide) {
TabLayout.Tab tab = tabLayout.getTabAt(2);
if(tab!=null) {
tabLayout.removeTab(tab);
}
}
I know that remove is not hide but it was my solution.
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