1

I try to learn Android Themes and got into trouble setting a TextView TextColor to another color then this global:

<item name="android:textColor">@color/white</item>

I created this:

 <item name="chatBubbleTextColor">@color/material_bohemia_500</item>

and thought I could use it in the TextView xml like

android:textColor="?attr/chatBubbleTextColor"

but i cannot get it to work maybe it does not work like that?
I know I can do like this:

<style name="BohemiachatBubbleTextColor" parent="android:Theme">
    <item name="android:textColor">@color/material_bohemia_500</item>
</style>

But do I really have to do like this? I only want to create a color attribute not creating a new style

Here is the Theme, it´s two Themes and the chatBubbleTextColor is different for both

Bohemia App Theme and Red App Theme

<!-- Base Theme -->
<style name="BaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Attributes for all APIs -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="dialogTheme">@style/AppTheme.Dialog</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert</item>
    <item name="colorControlHighlight">@color/selector_black_pressed</item>
    <!-- Theme for the Preferences -->
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
    <!-- Theme for the pacv_placesAutoCompleteTextV -->
    <item name="pacv_placesAutoCompleteTextViewStyle">@style/Widget.AppCompat.EditText</item>



<!-- Default App Theme -->
<style name="AppTheme" parent="BaseTheme">
    <!-- API specific attributes 14+ -->
    <item name="selectableRectDrawable">@drawable/state_list_selectable_rect_black</item>
    <item name="selectableRectDrawableInverse">@drawable/state_list_selectable_rect_white</item>
    <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_black</item>
    <item name="selectableRoundedRectDrawable">@drawable/state_list_selectable_rounded_rect_black</item>
    <item name="selectableRoundedRectDrawableInverse">@drawable/state_list_selectable_rounded_rect_white</item>
    <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_black</item>
</style>



<!-- Bohemia App Theme -->
<style name="BaseTheme.Bohemia" parent="AppTheme">
    <!-- Attributes for all APIs -->
    <item name="colorPrimary">@color/material_bohemia_400</item>
    <item name="colorPrimaryDark">@color/material_bohemia_500</item>
    <item name="colorAccent">@color/material_bohemia_a100</item>
    <item name="dialogTheme">@style/AppTheme.Dialog.Bohemia</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Bohemia</item>
    <item name="android:windowBackground">@color/material_bohemia_600</item>
    <!-- Sets the color of the control when it is not activated like an unchecked checkbox. -->
    <item name="colorControlNormal">@color/material_bohemia_a200</item>
    <!-- Chat bubble -->
    <item name="chatBubbleTextColor">@color/material_bohemia_500</item>

</style>

<style name="AppTheme.Bohemia" parent="BaseTheme.Bohemia">
    <!-- API specific attributes 14+ -->
    <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_bohemia</item>
    <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_bohemia</item>
    <!-- Add your custom overall styles here -->
</style>

<!-- Red App Theme -->
<style name="BaseTheme.Red" parent="AppTheme">
    <!-- Attributes for all APIs -->
    <item name="colorPrimary">@color/material_red_500</item>
    <item name="colorPrimaryDark">@color/material_red_700</item>
    <item name="colorAccent">@color/material_red_a700</item>
    <item name="dialogTheme">@style/AppTheme.Dialog.Red</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Red</item>
    <item name="android:windowBackground">@color/material_red_300</item>
    <!-- Chat bubble -->
    <item name="chatBubbleTextColor">@color/material_red_500</item>
</style>

<style name="AppTheme.Red" parent="BaseTheme.Red">
    <!-- API specific attributes 14+ -->
    <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_red</item>
    <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_red</item>

    <!-- Add your custom overall styles here -->
</style>
Tord Larsen
  • 2,670
  • 8
  • 33
  • 76
  • it should be `android:textColor="@color/chatBubbleTextColor"` – Pztar Feb 09 '17 at 15:08
  • Possible duplicate of [Android, How to theme font colour for just TextView Widgets?](http://stackoverflow.com/questions/15960029/android-how-to-theme-font-colour-for-just-textview-widgets) – Bradley Wilson Feb 09 '17 at 15:11

3 Answers3

2

I found the answer to my own question here.

Basically it goes like this:

In the file attr.xml I define this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="ChatBubbleBackGroundColor" format="reference|color" />
    <attr name="ChatBubbleTextColor" format="reference|color" />
</resources>

Next I add to my two BaseThemes:

<style name="BaseTheme.Red" parent="AppTheme">
   <item name="ChatBubbleBackGroundColor">@color/material_red_a200</item>
   <item name="ChatBubbleTextColor">@color/material_red_a700</item>
</style>

<style name="BaseTheme.Orange" parent="AppTheme">
   <item name="ChatBubbleBackGroundColor">@color/material_orange_a200</item>
   <item name="ChatBubbleTextColor">@color/material_orange_a700</item>
</style>

and finally in my layout:

<TextView
    android:id="@+id/quoteTitle"
    android:textColor="?ChatBubbleTextColor"
    android:BackGround="?ChatBubbleBackGroundColor"
    ...
</TextView>
Alex
  • 171
  • 2
  • 10
Tord Larsen
  • 2,670
  • 8
  • 33
  • 76
0

Into your TextView use style="@style/chatBubbleTextColor" instead of android:textColor="?attr/chatBubbleTextColor". Something like this

<TextView
                style="@style/chatBubbleTextColor"
                android:id="@+id/my_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 />
an_droid_dev
  • 1,136
  • 14
  • 18
  • So do I really have to create a new style like `style name="chatBubbleTextColor"....` why not add to `"BaseTheme.Bohemia"`? – Tord Larsen Feb 09 '17 at 15:16
  • @ErikHellberg yes, of course, if you want to use "BaseTheme.Bohemia" use style="@style/BaseTheme.Bohemia". – an_droid_dev Feb 09 '17 at 15:32
  • But now both my Red Theme and Bohemia have the same `chatBubbleTextColor`. I really need to get the `chatBubbleTextColor` inside the `BaseTheme.Bohemia` and `BaseTheme.Red` . How do I do that? Like my question say How to set TextView text color to specific Theme color – Tord Larsen Feb 09 '17 at 15:37
  • @ErikHellberg into your themes add yout color – an_droid_dev Feb 09 '17 at 16:43
0

You have set color for chatBubbleTextColor in this theme <style name="BaseTheme.Bohemia" parent="AppTheme"> if you apply this theme to any activity with in that if you set it to any TextView color android:textColor="?attr/chatBubbleTextColor" then it will work if set chatBubbleTextColor in AppTheme Style it will be available to entire app

Hemanth S
  • 669
  • 6
  • 16