Tom Kidd
Tom Kidd

Reputation: 12908

How can I have a global theme with different TextView and Spinner text colors?

I'm writing an App which needs the following two things:

I used a theme, applied at the Application level in the manifest, to perform the first item above.

<resources>
    <style name="GlobalTheme" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <item name="android:textColor">#FFFFFF</item>
    </style>
</resources>

Worked great. Except it also makes the text on the spinner white, which is hard to read.

OK, so I want the spinner color to still be black but everything else be white.

I found this question which showed how to set the Spinner text color, and it works, but only when I'm not also setting the global textColor as well.

So the following does not work:

<resources>
    <style name="GlobalTheme" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:spinnerItemStyle">@style/GlobalThemeSpinnerItem</item>
    </style>
    <style name="GlobalThemeSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
        <item name="android:textAppearance">@style/GlobalThemeTextAppearanceSpinnerItem</item>
    </style>
    <style name="GlobalThemeTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
        <item name="android:textColor">#000000</item>
    </style>
</resources>

I guess I was hoping that this would be like CSS and things would cascade down (i.e., "all text shall be white, except that which is in a spinner"). If I remove the android:textColor line in the main theme, the spinner color trick works fine.

It looks like the SpinnerItem derives from TextView so I tried to come up with a textViewStyle-type separation similar to the spinnerItemStyle separation, but didn't have any luck.

Unlike most people who ask about this, I want to keep it in the XML as much as possible. Does anyone know what I'm doing wrong?

Upvotes: 7

Views: 1614

Answers (1)

TofferJ
TofferJ

Reputation: 4784

Try this:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="GlobalTheme" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    <item name="android:spinnerItemStyle">@style/GlobalThemeSpinnerItem</item>
    <item name="android:textViewStyle">@style/GlobalThemeTextViewItem</item>
</style>

<style name="GlobalThemeTextViewItem" parent="android:Widget.TextView">
    <item name="android:textAppearance">@style/GlobalThemeTextAppearanceTextViewItem</item>
</style>
<style name="GlobalThemeTextAppearanceTextViewItem" parent="android:TextAppearance.Widget.TextView">
    <item name="android:textColor">#FFFFFF</item>
</style>

<style name="GlobalThemeSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
    <item name="android:textAppearance">@style/GlobalThemeTextAppearanceSpinnerItem</item>
</style>
<style name="GlobalThemeTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
    <item name="android:textColor">#000000</item>
</style>

This will make the text in your TextViews white and the text in your Spinners black.

Upvotes: 1

Related Questions