jjdev80
jjdev80

Reputation: 461

Style selector?

I have two holo based ABS styles, Purple and Lime, users are able to set the theme from settings.

In my layout I have a TextView with a custom textAppearance, I want to change that textAppearance based on the active style.

(if the purple theme is activated the text must be white and if the lime theme is activated the text must be lime)

Is there a way to do that from the XML?

Edit

I'm sorry if the title is misleading.

Upvotes: 2

Views: 824

Answers (1)

Ahmad
Ahmad

Reputation: 72533

(if the purple theme is activated the text must be white and if the lime theme is activated the text must be lime)

Try that:

<style name="PurpleTheme" parent="...">
    <!-- define the style for the text appearance here. Following is an example -->
    <item name="android:textAppearance">@style/MyWhiteText</item>
</style>

<style name="LimeTheme" parent="...">
    <!-- define the style for the text appearance here. Following is an example -->
    <item name="android:textAppearance">@style/MyLimeText</item>
</style>

<style name="MyWhiteText" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/white</item>
</style>

<style name="MyWhiteText" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/lime</item>
</style>

Or if you don't want to set the color to all TextViews then do this:

Declare an attribute:

<attr name="myTextViewColor" format="reference" />

Use it like this in your theme:

<style name="PurpleTheme" parent="...">
    <item name="myTextViewColor">@color/white</item>
</style>

<style name="LimeTheme" parent="...">
    <item name="myTextViewColor">@color/lime</item>
</style>

Now you can set the color of the specific TextView like this:

<TextView 
android:textColor="?attr/myTextViewColor"
[...] />

Upvotes: 1

Related Questions