In Android themes, there is a problem with the simplest: custom color for each theme

I'm new to Android development and although coding makes perfect sense, IMHO themes for Android aren't

I have a problem doing this simple task:

I have (for example) a color called "blah"

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="blah">#F0F0F0</color> <resources> 

which is used universally in XML layouts or code, as well as using various representations as "@ color / blah"

I just wanted to make this change for each topic.

So when I use MyTheme1, blah should be # F0F0F0, and when I use MyTheme2, blah should be # 00FF00

I read about topics and still cannot figure out how to implement this simple task, since my application does not require special styles, etc., only colors for each topic.

Thanx in advance

UPDATE:

After the link provided by Mohamed_AbdAllah, I managed to create several custom colors by defining them in attrs.xml and styles.xml:

attrs.xml

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

styles.xml

 <style name="AppBaseThemeDark" parent="android:Theme.Black"> <item name="color_item_title">@color/White</item> </style> 

But now a much more serious problem arises.

Can I use color successfully? color_item_title in all views, so the buttons and text actually get that color.

But using this? color_item_title in custom drawings or listview layouts (THESE VIEWS THAT ARE STRESSED DURING RUNTIME) will fail.

So use? color_item_title inside the listitem schedule is broken at runtime with the message of the blower on this line :(

It also breaks down into my blueprints: (inflation error again)

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@color/Black" /> <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp" /> <stroke android:width="1dp" android:color="?color_item_title" /> </shape> 
+8
android android-layout themes android-theme
source share
3 answers

After searching and trying, I got a hint that there are some things you can do and some things you cannot .

You can

a) Define your own attributes in attrs

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

b) Define your values ​​in your styles. xml

 <style name="AppBaseThemeDark" parent="android:Theme.Black"> <item name="color_gallery_backround">@color/White</item> </style> <style name="AppBaseThemeWhite" parent="android:Theme.Light"> <item name="color_gallery_backround">@color/Black</item> </style> 

c) Use them in any representation how? color_gallery_backround or? attr / color_gallery_backround for example

 android:background="?attr/color_gallery_backround" 

YOU DONT BE

a) You cannot access this attribute from custom drawings because they are not valid during inflation

therefore you can not use? attr / color_gallery_backround here:

customshape.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="**?attr/color_gallery_backround**" /> <stroke android:width="2dp" android:color="@color/Black" /> </shape> 

b) You cannot access this attribute in a view that will be inflated during (e.g.) ListView as a ListItem

Thus, the topic of "support" of attributes, at least prior to API 10, has limits of depth.

Solutions

As suggested, you need to create DUAL separate drawings using different colors and configure the attributes to link to these drawings in accordance with the theme:

attrs.xml

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

and for each topic that can be used in styles.xml:

 <resources> <style name="AppBaseThemeDark" parent="android:Theme.Black"> <item name="rounded_background">@drawable/round_rect_shape_dark</item> </style> <style name="AppBaseThemeLight" parent="android:Theme.Light"> <item name="rounded_background">@drawable/round_rect_shape_white</item> </style> </resources> 

Now you can reference your drawable as? rounded_background, and let the theme select it.

For bloated views in a ListView listitem, you must programmatically set the colors.

+3
source share

The only way to achieve what you want is to define two different colors, for example blah and blah2 , and refer to them in your themes

0
source share

This works for me, as John S. suggested, but in the definition of attributes I had to add "color | reference" instead of "reference". So my attrs.xml looks like this:

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

The best regattas!

0
source share

All Articles