tags:

views:

11099

answers:

5

I have an activity which has a TabHost containing a set of TabSpecs each with a listview containing the items to be displayed by the tab. When each TabSpec is created, I set an icon to be displayed in the tab header.

The TabSpecs are created in this way within a setupTabs() method which loops to create the appropriate number of tabs:

TabSpec ts = mTabs.newTabSpec("tab");
ts.setIndicator("TabTitle", iconResource);

ts.setContent(new TabHost.TabContentFactory(
{
    public View createTabContent(String tag)
    {
        ... 
    }      
});
mTabs.addTab(ts);

There are a couple instances where I want to be able to change the icon which is displayed in each tab during the execution of my program. Currently I am deleting all the tabs, and calling the above code again to re-create them.

mTabs.getTabWidget().removeAllViews();
mTabs.clearAllTabs(true);
setupTabs();

Is there a way to replace the icon that is being displayed without deleting and re-creating all of the tabs?

+2  A: 

The short answer is, you're not missing anything. The Android SDK doesn't provide a direct method to change the indicator of a TabHost after it's been created. The TabSpec is only used to build the tab, so changing the TabSpec after the fact will have no effect.

I think there's a workaround, though. Call mTabs.getTabWidget() to get a TabWidget object. This is just a subclass of ViewGroup, so you can call getChildCount() and getChildAt() to access individual tabs within the TabWidget. Each of these tabs is also a View, and in the case of a tab with a graphical indicator and a text label, it's almost certainly some other ViewGroup (maybe a LinearLayout, but it doesn't matter) that contains an ImageView and a TextView. So with a little fiddling with the debugger or Log.i, you should be able to figure out a recipe to get the ImageView and change it directly.

The downside is that if you're not careful, the exact layout of the controls within a tab could change and your app could break. Your initial solution is perhaps more robust, but then again it might lead to other unwanted side effects like flicker or focus problems.

dmazzoni
+7  A: 

Just to confirm dominics answer, here's his solution in code (that actually works):

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
      public void onTabChanged(String tabId) {
       if (TAB_MAP.equals(tabId)) {
        ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
        iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
        iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
        iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
       } else if (TAB_LIST.equals(tabId)) {
        ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
        iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
        iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
        iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
       }
      }
        });

Ofcourse it's not polished at all and using those direct indices in getChildAt() is not nice at all...

Minor improvement - use: TabWidget.getChildTabViewAt(..) instead of getChildAt(...)
tonys
+1  A: 

hi

See my post with code example regarding Customized Android Tabs.

Thanks Spct

Great! Exactly what I was looking for. Thanks!
Fedor
I don't this that this is a correct approach.
y ramesh rao
+2  A: 

It might be a little late but you might wanna take a look at this thread :)

http://groups.google.com/group/android-developers/browse_thread/thread/ef3bdebcb715b385

Cheers, Geoffrey Chavepeyer

Sweet! It is a little late, but nice to know none the less.
lnediger
A: 

Hi,

I am using a tab widget to display different activities.And i called the activities through intents.

I am trying to remove a particular tab from widget. But it is not possible to delete tab. is there any process to delete?

Thanks Jetti

Jetti