I am using a TabHost which has 3 tabs. Each tab has a image + text.
spec = tabHost.newTabSpec("MyTasks")
.setIndicator(Html.fromHtml("<b><H2>My Tasks</H2></b>"), getResources().getDrawable(R.drawable.task ))
.setContent(intent);
tabHost.addTab(spec);
I want to change image when I select a tab. I used following code to change it …
TabWidget tw = getTabWidget();
View leftTabView = tw.getChildAt(0);
leftTabView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab1_drawable));
tab1_drawable is a xml (selector and items for each state). THIS IS SETTING AND CHANGING BACKGROUND NOT THE IMAGE I SET. How can I change it?
Instead of changing the drawable through code, why not use a state-list drawable? Then you’d just specify a different drawable for the tab when it is selected.
Example, ic_tab_tasks.xml:
Then, when you’re setting the tab indicator’s drawable initially, just use the state-list drawable.