Lily
Lily

Reputation: 846

Closing tabs on click in Android

I'm writing an application that creates multiple tabs I want the user to have the option to close it when pressing on a button the same way closing tabs in brewer by pressing the little x in the corner but I want only one tab to close and the rest stays

here is my code

    TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
    tabHost.setup();

    TabSpec spec1=tabHost.newTabSpec("Tab 1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Tab 1");

    TabSpec spec2=tabHost.newTabSpec("Tab 2");
    spec2.setIndicator("Tab 2");
    spec2.setContent(R.id.tab2);


    tabHost.addTab(spec1);
    tabHost.addTab(spec2);

I want Tab2 to close when the user press on the button, my problem is how to remove the tab from TabHost not adding a listener to the button

is there a way to do that? I found a function which removes everything and I do not want that

Upvotes: 0

Views: 147

Answers (2)

The Ray of Hope
The Ray of Hope

Reputation: 738

use following on button event

TabHost  tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);

Upvotes: 1

Kirk
Kirk

Reputation: 5077

This one will help you, very easy one for far i found.

tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(2));

In addition, these are the available functionality that you can use to remove individual tabs

  • removeView(View view)
  • removeViewAt(int index)
  • removeViewInLayout(View view)

Hope it helps!

Upvotes: 0

Related Questions