Mun0n Mun0n - 3 months ago 29
Android Question

Change text color and selector in TabWidget

I've a

TabWidget
, independently of the ÀctionBar
, in a
FragmentTabHost`.

I want to customize the look and feel of the
TabWidget
but I don't get it. My intention is to change the text color and the selector color, as you can see in the image I can change the background of the
TabWidget
. I don't want to use a custom
TextView
for the tabs because the tabs must be with the Holo look and feel.

TabWidget

I've tried to put a style to the
TabWidget
but it doesn't work. In this way:

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@style/MyTabs"
/>


and with the style

<style name="MyTabs">
<item name="android:textColor">@color/white</item>
<item name="android:background">@drawable/tabs</item>
<item name="android:backgroundStacked">@color/red_action_bar</item>
</style>


I've tried to add the style from a theme.xml using the
parent="android:Widget.Holo.TabWidget"
, but nothing happens.

Answer

I finally find a way to do that. Using this code in the onCreateViewmethod of the Fragment

    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                View v = tabHost.getTabWidget().getChildAt(i);
                v.setBackgroundResource(R.drawable.tabs);

                TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
                tv.setTextColor(getResources().getColor(R.color.white));
}

And setting the bakcground color of the TabWidgetto red

Comments