<p>I have an activity that 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.</p>
<p>The TabSpecs are created in this way within a <code>setupTabs()</code> method which loops to create the appropriate number of tabs:</p>
<pre><code>TabSpec ts = mTabs.newTabSpec(&quot;tab&quot;);
ts.setIndicator(&quot;TabTitle&quot;, iconResource);

ts.setContent(new TabHost.TabContentFactory(
{
    public View createTabContent(String tag)
    {
        ... 
    }            
});
mTabs.addTab(ts);
</code></pre>
<p>There are a couple of 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.</p>
<pre><code>mTabs.getTabWidget().removeAllViews();
mTabs.clearAllTabs(true);
setupTabs();
</code></pre>
<p>Is there a way to replace the icon that is being displayed without deleting and re-creating all of the tabs?</p>

[{'answer_id': 68078, 'author': 'dmazzoni', 'author_id': 7193, 'author_profile': 'https://Stackoverflow.com/users/7193', 'pm_score': 6, 'selected': True, 'text': "<p>The short answer is, you're not missing anything.  The Android SDK doesn't provide a direct method to change the indicator of a <code>TabHost</code> after it's been created.  The <code>TabSpec</code> is only used to build the tab, so changing the <code>TabSpec</code> after the fact will have no effect.</p>\n\n<p>I think there's a workaround, though.  Call <code>mTabs.getTabWidget()</code> to get a <code>TabWidget</code> object.  This is just a subclass of <code>ViewGroup</code>, so you can call <code>getChildCount()</code> and <code>getChildAt()</code> to access individual tabs within the <code>TabWidget</code>.  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 <code>ViewGroup</code> (maybe a <code>LinearLayout</code>, but it doesn't matter) that contains an <code>ImageView</code> and a <code>TextView</code>.  So with a little fiddling with the debugger or <code>Log.i</code>, you should be able to figure out a recipe to get the <code>ImageView</code> and change it directly.</p>\n\n<p>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.</p>\n"}, {'answer_id': 504023, 'author': 'srakyi', 'author_id': 56276, 'author_profile': 'https://Stackoverflow.com/users/56276', 'pm_score': 5, 'selected': False, 'text': "<p>Just to confirm dominics answer, here's his solution in code (that actually works):</p>\n\n<pre><code>tabHost.setOnTabChangedListener(new OnTabChangeListener() {\n    public void onTabChanged(String tabId) {\n        if (TAB_MAP.equals(tabId)) {\n            ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));\n            iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));\n        } else if (TAB_LIST.equals(tabId)) {\n            ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));\n            iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));\n        }\n    }\n});\n</code></pre>\n\n<p>Of course it's not polished at all and using those direct indices in getChildAt() is not nice at all...</p>\n"}, {'answer_id': 510196, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>See my post with code example regarding <a href="http://ezmobile.wordpress.com/2009/02/02/customized-android-tabs/" rel="noreferrer">Customized Android Tabs</a>.</p>\n\n<p>Thanks\nSpct</p>\n'}, {'answer_id': 6627972, 'author': 'Mohit', 'author_id': 620661, 'author_profile': 'https://Stackoverflow.com/users/620661', 'pm_score': 3, 'selected': False, 'text': '<p>This is what I did and it works for me. I created this function in the activity that extends from TabBarActivity</p>\n\n<pre><code>public void updateTab(int stringID) {\n    ViewGroup identifyView = (ViewGroup)getTabWidget().getChildAt(0);\n    TextView v =  (TextView)identifyView.getChildAt(identifyView.getChildCount() - 1);\n    v.setText(stringID);\n}\n</code></pre>\n\n<p>You can modify this function to change the image instead of text or you can change both, also you can modify this to get any tab child. I was particularly interested in modifying the text of the first tab at runtime.</p>\n\n<p>I called this function from the relevant activity using this call</p>\n\n<pre><code>getParent().updateTab(R.string.tab_bar_analyze);\n</code></pre>\n'}, {'answer_id': 11808808, 'author': 'Gautam Vasoya', 'author_id': 1500667, 'author_profile': 'https://Stackoverflow.com/users/1500667', 'pm_score': 2, 'selected': False, 'text': '<p>Try This:</p>\n\n<pre><code>tabHost.setOnTabChangedListener(new OnTabChangeListener() {\n    public void onTabChanged(String tabId) {\n        if (TAB_MAP.equals(tabId)) {\n            ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));\n            iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));\n        } else if (TAB_LIST.equals(tabId)) {\n            ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));\n            iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);\n            iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));\n        }\n    }\n});\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/36881', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1197/']