Android themes trouble implementing the simplest: custom per theme color -
i'm beginner in android development and, although coding makes perfect sence, android themes imho don't
i have trouble implementing simple task:
i have (for example) color named "blah"
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="blah">#f0f0f0</color> <resources>
which used everywhere in xml layouts or code , different view complonents "@color/blah"
i make color value change per-theme
so when use mytheme1, blah should #f0f0f0 , when use mytheme2 blah should #00ff00
i've been reading themes , still cant find out how implement simple task, since app not require special styles , on, per-theme colors.
thanx in advance
update:
after link provided mohamed_abdallah, managed create custom colors defining them in attrs.xml , 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 more serious problem arises
i can use color ?color_item_title in every view, buttons , text gets color.
but using ?color_item_title on custom drawables or listview layouts (that views inflated during runtime) causes crash.
so using ?color_item_title inside listview listitem layout crashes @ runtime inflater message @ line :(
it crashes @ drawables: (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>
after searching , trying, got tip there things can , some things cant.
you can
a) define own attributes in attrs
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="color_gallery_backround" format="color|reference" /> </resources>
b) set values in 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 view ?color_gallery_backround or ?attr/color_gallery_backround example
android:background="?attr/color_gallery_backround"
you cant
a) cant access attribute custom drawables, not valid during inflation
so you cant use ?attr/color_gallery_backround in 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) cant access attribute in view inflated during (for example) listview listitem view
so theme "support" attributes @ least api 10 has depth limits.
solutions
as suggested, have create 2 separate drawables using different colors , set attributes reference these drawables according theme:
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="rounded_background" format="reference" /> </resources>
and on each theme, setup actual drawable use 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 can reference drawable ?rounded_background , let theme choose it.
for inflated views inside listview listitem layouts, have programmatically set colors.
Comments
Post a Comment