I wanted to change the background of Android tab widget. So I used this code
public static void setTabColor(TabHost tabhost) {
for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#000000")); // selected
}
In order to change the color of each tab, I had to implement onTabChangedListener on the object of the class TabHost.
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
}
});
Now, my question is: how can I call the method setTabColor() from onTabChanged when I have no TabHost object to pass as the parameter?
You can set the
tabHostvariablefinal, then you can reference it inside of OnTabChangeListener. Another solution like this would be to maketabHosta member variable of your Activity, so you can reference it anytime.Alternatively, if you are in a TabActivity, you can call
getTabHost()at any time.