Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a tab in Android tab layout?

Tags:

android

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.

like image 439
indira Avatar asked Feb 17 '11 08:02

indira


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

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)

like image 103
Tanmay Mandal Avatar answered Oct 01 '22 16:10

Tanmay Mandal


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.

like image 23
Carlos Espinoza Avatar answered Oct 02 '22 16:10

Carlos Espinoza