Edison Santos
Edison Santos

Reputation: 103

Define multiple textColor in one theme

Currently I'm trying to define in my application a theme for day mode and another for night mode. I can already change between those two, the problem is that I have textViews with white text color and others with black text color. So I would like to know if there is a way for:

// defined in the layout
<TextView style="@style/whiteStyleText" />
<TextView style="@style/blackStyleText" />

// defined in the styles
<style name="Theme.DayTheme" parent="@style/Theme.ActivityTheme">
    <item name="@style/whiteStyleText">@color/white</item>
    <item name="@style/blackStyleText">@color/black</item>
</style>

<style name="Theme.NightTheme" parent="@style/Theme.ActivityTheme">
    <item name="@style/whiteStyleText">@color/light_red</item>
    <item name="@style/blackStyleText">@color/red</item>
</style>

Many thanks in advance.

Upvotes: 0

Views: 141

Answers (2)

Edison Spencer
Edison Spencer

Reputation: 503

I haven't found anything about multiple textColors defined in the same theme.

One solution, which I've done is, set a default textColor for the dayTheme and another one for the nightTheme. For all other textViews that does not use that default color, I've defined it in the layout, and at the beginning of the activity I check what is selected, day or night mode and I call setContentView with the proper layout.

Another solution would be, declare a custom attribute, e.g. textColorNight, and create a custom textView which would use this attribute and then would check what mode is currently selected, so it could decide if the textColor (day mode) or the textColorNight (night mode) would be applied to it's text. I thing this second solution is a more refined one, but I haven't implemented it because of the lack of time. But next time I need to implement the day/night mode, I'll do it this way.

Upvotes: 0

Pieter Visagie
Pieter Visagie

Reputation: 154

this might help you a bit. Not a 100% sure what you want.

It is to define all your colors in one place. Place this is res>values>styles.xml

<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
</style>

<color name="blueDefault">#2782AF</color>
<color name="blueBGDark">#226A87</color>
<color name="blueDark">#257493</color>
<color name="blueLight">#297F99</color>
<color name="greyDefault">#3F4144</color>

</resources>

Upvotes: 1

Related Questions