Contextual / redefined theme color

I ran into a problem and I tried several ways to handle this, still unsuccessfully.

My application uses several themes, such as: Halloween, Chirstmas, etc., and I use some color attributes in widgets, such as TabLayout background, text color, etc., to contextualize the application.

Question: how to use the same color attributes with different values ​​depending on the context of the topic?

So, basically here are the usual ways to declare colors:

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

But the theme and colors are immutable, so I thought maybe I could override these colors within each theme, for example:

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

=> failed

Another declaration, declare these colors as attributes:

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

And use the theme in my XML as follows: " ?attr/mapLoadingIndicatorColor ". But these features are only allowed from the Lollipop version and cause crashes before.

I read a lot about theme customization, color redefinition, but I have not found a clear solution to this situation.

Thank you anyway.

+5
source share
1 answer

You mentioned that:

And use a theme in my XML like this: "? Attr / mapLoadingIndicatorColor". But these features are only allowed with the Lollipop version and the cause of the accident earlier.

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

In the activity_main.xml file (in the 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 the values ​​folder):

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

In styles.xml (in the 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'm still completely new to Android programming, if you could indicate where you found the statement that ?attr/mapLoadingIndicatorColor would cause pre-Lollipop to crash, that would be great! (I cannot find it anywhere, I know that you cannot use the elevate pre-Lollipop attribute)

+1
source

Source: https://habr.com/ru/post/1240860/


All Articles