Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • The general text (in TextViews) needs to be one color (white, in this case - dark background)
  • The text on a Spinner needs to be a different color (black, since white's too hard to read)

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?

like image 828
Tom Kidd Avatar asked Jan 17 '12 23:01

Tom Kidd


1 Answers

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.

like image 182
TofferJ Avatar answered Oct 31 '22 11:10

TofferJ