4

I have two themes that I switch back and forth between in my Android application. In some places, I have TextViews that I want to remain the default color. In other places, I want them to be another color (let's say orange). When changing themes, I'd like only the TextViews that were previously orange to become blue.

Is there a way to do this simply in Android? I had something in mind such as a class tag in html/css but can't seem to find anything.

EDIT: To clarify what I was referring to with the html/css equivalent:

<div>This text is black</div>
<div class="redDiv">This text is red</div>

.redDiv {
    color:red;
}
Alex
  • 1,100
  • 1
  • 9
  • 23
  • Look this answer: http://stackoverflow.com/questions/3078081/setting-global-styles-for-views-in-android Hope it helps – krossovochkin May 14 '14 at 14:00
  • How about having two style declarations for textviews, and apply the persistent one to your desired textviews from the layout? – rekaszeru May 14 '14 at 14:02
  • @krossovochkin I was looking at this answer prior to asking this question. That is how I would change ALL of my TextViews, I only want to change some. – Alex May 14 '14 at 14:14
  • @rekaszeru A good solution, but the problem in my case is that this change needs to happen simply by changing the overall theme of the application :-/ – Alex May 14 '14 at 14:15
  • 1
    So each theme has their specific textview style, and share the same persistent style for the textviews you don't want to change. – rekaszeru May 14 '14 at 14:21
  • Ah I see what you're saying now. So I could define the default style in-line for the persistent TextViews, and leave the others at the mercy of the changing theme. – Alex May 14 '14 at 14:33

1 Answers1

10

Let's say you have two themes in your application: MyFirstTheme and MySecondTheme:

<style name="MyFirstTheme" parent="android:Theme">
    <item name="android:textViewStyle">@style/MyFirstTheme.TextView</item>
    [...]
</style>
<style name="MySecondTheme" parent="android:Theme">
    <item name="android:textViewStyle">@style/MySecondTheme.TextView</item>
    [...]
</style>

The textview styles defined in your styles.xml could look like:

<style name="MyFirstTheme.TextView" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@android:color/black</item>
    [...]
</style>
<style name="MySecondTheme.TextView" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@color/orange</item>
    [...]
</style>
<style name="PersistentTheme.TextView" parent="@style/MyFirstTheme.TextView">
    <item name="android:textColor">@color/red</item>
</style>

So when you have a layout with two TextViews, you can have the one completely follow the active theme by not setting anything extra to it, and you can apply other appearance to your other TextView instance by specifying a style attribute for it:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <!-- This textview will change text color when other style applied -->
    <TextView
        android:id="@+id/tv_styledependent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- This textview will always have the same (red) text color -->
    <TextView
        android:id="@+id/tv_persistent"
        style="@style/PersistentTheme.TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_styledependent" />

</RelativeLayout>
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • 1
    This is the solution I ended up going with, I've been waiting for you to post it as an answer so I could accept :) – Alex May 14 '14 at 19:06