0

I have a couple of Themes in an app and it works ok. Now I wanted to set a chat bubble text color to color red when user select BaseTheme.Red, and text color orange when user select BaseTheme.Orange (see code below)

It´s only the chat bubble text right that I want to be like red for the ´Red´ and Orange for the Orange Theme and all other TextView text color in the app will have default Theme color.

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

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

I created this: Inside the BaseTheme.Red

<item name="chatBubbleTextColor">@color/material_red_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?

How can I with the Themes below make this work?

Here is two Themes Red and Orange:

    <!-- 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>



    <!-- Orange App Theme -->
    <style name="BaseTheme.Orange" parent="AppTheme">
        <!-- Attributes for all APIs -->
        <item name="colorPrimary">@color/material_orange_500</item>
        <item name="colorPrimaryDark">@color/material_orange_700</item>
        <item name="colorAccent">@color/material_orange_a700</item>
        <item name="dialogTheme">@style/AppTheme.Dialog.Orange</item>
        <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Orange</item>
        <item name="android:windowBackground">@color/material_orange_300</item>
    </style>

    <style name="AppTheme.Orange" parent="BaseTheme.Orange">
        <!-- API specific attributes 14+ -->
        <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_orange</item>
        <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_orange</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 attribute not working-->
        <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
  • I have one question why do you change the entire theme instead of changing the text color by something like this? `#f00 textView.setTextColor(getResources().getColor(R.color.errorColor));` – Mehran Zamani Feb 09 '17 at 20:37
  • When you do like you say setting color like this `textView.setTextColor(getResources().getColor(R.color.errorC‌​olor));` that means that text color will be the same for all Themes. I don't understand what you say with: " I have one question why do you change the entire theme" – Tord Larsen Feb 09 '17 at 20:46
  • are you going to set multiple theme to one textview at the same time? when you set a color text for one particular textview does it apply to other views? – Mehran Zamani Feb 09 '17 at 20:59
  • My suggestion is you could study this [Android-Material-Themes-Demo](https://github.com/StevenByle/Android-Material-Themes-Demo) to understand what my question is about – Tord Larsen Feb 09 '17 at 21:13
  • can't you define a style just for your chat bubble and set the textcolor there? For example, by using a style, you can take this layout XML: `` And turn it into this: `` – Mehran Zamani Feb 09 '17 at 21:32
  • Thanks but how do you suggest that the `"@style/CodeFont"` should be applied? In my app user can switch between Them Red and Theme Orange right, so the `CodeFont` must resist inside both Themes Red and Orange. Please answer my question and will accept it if it solve this problem – Tord Larsen Feb 10 '17 at 04:36
  • define a third theme say RedCustom and in that define style for the textcolor you want and set this theme only for your bubble chat. the rest of your appearence would be the same like you wanted. **It´s only the chat bubble text right that I want to be like red for the ´Red´ Theme and all other TextView text color in the app will have default Theme color.** – Mehran Zamani Feb 10 '17 at 06:30
  • I dont think you understand, User can switch Theme!! and Doing it like that and when user switch to Orange Theme the chat bubble will have the Red textcolor not Orange. – Tord Larsen Feb 10 '17 at 06:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135355/discussion-between-mehran-zamani-and-erik-hellberg). – Mehran Zamani Feb 10 '17 at 07:57

1 Answers1

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 BaseTheme:

<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>
Sajal Narang
  • 397
  • 1
  • 14
Tord Larsen
  • 2,670
  • 8
  • 33
  • 76