TabWidget white foreground color?
Asked Answered
H

4

6

I don't know what I did but for a period of time my TabWidget had white colored tabs which looked really nice. I never set a theme or background/foreground color in my project at all. The next time I compiled it it reverted back to the gray tabs. My application is using the default dark theme. Even if I set the application theme to light, the tabs are still gray. So obviously it was something else that changed the tabs' color. Anyone know how to do this?

Husein answered 30/3, 2010 at 6:56 Comment(3)
Are you perhaps testing on two different versions of the platform? The tab style changed in 2.0. Also, if you could post a screenshot, taken with DDMS, it would very much help.Formulaic
Ah, yes. It was from compiling for 1.6. Is there any way to manually set the same color for 2.0+ ?Husein
I had this issue and determined that it was the targetSdkVersion attribute in the AndroidManifest.xml was causing it to change for me.Slipper
S
14

I was having a problem due to a bug in Android 1.6's light theme (tab indicator text is white). I was able to override the default theme as follows:

  1. I created a custom theme that inherited from the default theme:

styles.xml:

<style name="MyTheme" parent="@android:style/Theme.Light">
    <item name="android:tabWidgetStyle">@style/LightTabWidget</item>
</style>

<style name="LightTabWidget" parent="@android:style/Widget.TabWidget">
    <!-- set textColor to red, so you can verify that it applied. -->
    <item name="android:textColor">#f00</item>
</style>

Then I just apply that theme to my application by adding android:theme="@style/MyTheme" to the <application /> element of my AndroidManifest.xml.

Slipper answered 2/7, 2010 at 14:33 Comment(0)
G
6

Check this answer of mine: Background in tab widget ignore scaling

You can also refer to the android.graphics.drawable package

In your code, you can set the background for your tabs like this:

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(
            android.R.color.white);
Greenstein answered 30/3, 2010 at 9:0 Comment(1)
This makes the tab background black no matter what.Husein
P
1

in the public void onCreate(Bundle savedInstanceState)

           `tabHost = getTabHost();
            tabHost.setOnTabChangedListener(this);
    tabHost.setCurrentTab(0);
    setTabColor();`

than in the listener:

public void onTabChanged(String tabId) { setTabColor();

finally the function, which set the foreground and the background too:

public void setTabColor() {
    // set foreground color:
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
        RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i);
        ImageView imageView = (ImageView) rl.getChildAt(0);// change it if you want it
        TextView textView = (TextView) rl.getChildAt(1);//          
        textView.setTextColor(Color.parseColor("#FFFFFF"));
    }

    // set background color:
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#010101")); // unselected
    }
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#121288")); // selected
}
Pall answered 20/7, 2011 at 11:34 Comment(0)
O
0

In onCreated() :

    tabHost.setCurrentTab(0);

// Set tabs text color to white:
TabWidget tabWidget = tabHost.getTabWidget();
int whiteColor = getResources().getColor(R.color.white);
int someOtherColor = getResources().getColor(R.color.someOtherColor);
for(int i = 0; i < tabWidget.getChildCount(); i++){
    View tabWidgetChild = tabWidget.getChildAt(i);
    if(tabWidgetChild instanceof TextView){
        ((TextView) tabWidgetChild).setTextColor(whiteColor);
    } else if(tabWidgetChild instanceof Button){
        ((Button) tabWidgetChild).setTextColor(whiteColor);
    } else if(tabWidgetChild instanceof ViewGroup){
        ViewGroup vg = (ViewGroup)tabWidgetChild;
        for(int y = 0; y < vg.getChildCount(); y++){
            View vgChild = vg.getChildAt(y);
            if(vgChild instanceof TextView){
                ((TextView) vgChild).setTextColor(whiteColor);
            }
        }
        vg.setBackgroundColor(someOtherColor);
    }
}
Outside answered 1/11, 2013 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.