Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contextual/override theme color

I'm facing a problem and i tried several ways to face it, still unsuccessful.

My app is using multiple themes like : Halloween, Chirstmas, etc and i'm using some colors attributes on widget like TabLayout background, Text Color, etc. to contextualized the app.

The question is : how do I use the same colors attributes with differentes values depending of a Theme context ?

So, basically here's the normal ways to declare colors :

<color name="mapMarkerSelectedTextColor">@android:color/white</color>
<color name="mapLoadingIndicatorColor">@color/white</color>

But, theme and colors are immutable so I thought, maybe I can override those colors inside each theme like :

    <item name="mapMarkerUnselectedTextColor">@color/christmas_red</item>
    <item name="mapMarkerSelectedTextColor">@color/white</item>

=> unsuccessful

Other lead, declare those colors as attributes :

<attr name="mapLoadingIndicatorColor" format="reference|color" />
<attr name="map_autocomplete_accent_color" format="reference|color" />

And use theme in my XML like this : "?attr/mapLoadingIndicatorColor". But this features is only allowed since Lollipop version and cause crashs before.

I've reading a lot about theme customisation, color overriding, but never found a clear solution about this situation.

Thanks anyway.

like image 755
marshallino16 Avatar asked Jan 18 '16 16:01

marshallino16


1 Answers

You mentioned that:

And use theme in my XML like this : "?attr/mapLoadingIndicatorColor". But this features is only allowed since Lollipop version and cause crashs before.

I'm not sure that ?attr/something cannot be used pre-Lollipop (Lollipop has API level 21) because I used it on devices with API level 16 in emulator and it works fine. I used it like below to change the background color of a button when different theme is chosen:

In activity_main.xml (in layout folder):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A button"
        style="?attr/myButton"/>

</LinearLayout>

In attrs.xml (in values folder):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myButton" format="reference"></attr>
</resources>

In styles.xml (in values folder):

<resources>

<!-- default theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="myButton">@style/defaultButtonStyle</item>
</style>

<style name="defaultButtonStyle" parent="android:Widget.Button">
    <item name="android:background">@color/green</item>
</style>

<!-- custom theme -->
<style name="AppTheme.CustomTheme">
    <item name="myButton">@style/customButtonStyle</item>
</style>

<style name="customButtonStyle" parent="android:Widget.Button">
    <item name="android:background">@color/blue</item>
</style>

</resources>

Actually, I am still quite new to Android programming, if you could specify where did you find the statement that ?attr/mapLoadingIndicatorColor will cause crashes pre-Lollipop, it will be great! (I can't find it anywhere, I only know you cannot use elevate attribute pre-Lollipop)

like image 88
karansky Avatar answered Oct 05 '22 07:10

karansky