Reputation: 8531
I have been trying to apply a style to my TabWidget.
I've tried several methods such as
for(int i=0;i < tabHost.getTabWidget().getChildCount();i++) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
from How do I change the background of an Android tab widget?
However, this code does not work for me. I've also tried messing with some XML styles. The closest I have gotten is to set the Divider, however that makes the entire tab widget turn one solid color and the Tabs are no longer drawn on top.
Please help. Mark's books only touch on setting the Icons for the tabs, not changing the color. I feel this should be simple, but TabWidgets and Hosts make everything harder.
I've tried this code targeting both the 1.6 and 2.2 platforms, but neither API works.
Thanks
Upvotes: 0
Views: 1744
Reputation: 134664
The background of a tab is actually a NinePatch image, set into a StateListDrawable. When you call setBackgroundColor()
, you're replacing the set StateListDrawable with a simple color, so the entire tab turns into that color. What you'll need to do is actually modify (or draw your own) NinePatch tab images that are the color and style that you want for each state (e.g. focused, pressed, etc.).
Alternately, in code you could set a ColorFilter as described here (getBackground()
will work for a TabWidget as well as a button) but I'd recommend going the NinePatch route personally.
Upvotes: 1