How to hide a tab in Android tab layout?
I have a tab layout containin开发者_StackOverflow中文版g 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.
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.
精彩评论