Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove tab from TabHost

In a TabHost widget, I can create a new tab with its content (Intent) using TabHost.addTab(TabHost.TabSpec tabSpec).

We can remove all tabs we created by calling clearAllTabs(), but I can't figure out how to remove the tab or just replace the content (Intent) inside the tab with new Intent.

so what I need something like removeTab(int index)

like image 589
Anwar Chandra Avatar asked Jul 21 '10 13:07

Anwar Chandra


2 Answers

Much Easier:

 tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(3));
like image 84
2red13 Avatar answered Nov 13 '22 17:11

2red13


Actually, clearAllTabs does that :

public void clearAllTabs() {
  mTabWidget.removeAllViews();
  initTabHost();
  mTabContent.removeAllViews();
  mTabSpecs.clear();
  requestLayout();
  invalidate();
}

And the method removeAllViews comes from the class ViewGroup. Luckily, ViewGroup does have methods to remove only one view :

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

Knowing that, I would recommend to subclass TabWidget and TabHost to add the behaviour you need. Maybe there is an easier way but that's the only one I can think of. Good luck

like image 13
Sephy Avatar answered Nov 13 '22 17:11

Sephy