Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference from drawable to style

My app with tabs has two themes. In each theme tabs have different images in selected and unselected state. How I can properly reference to image by theme?

For example. I have in themes.xml

<?xml version="1.0" encoding="utf-8"?>

<style name="LightTheme" parent="@android:style/Theme.Light">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_light</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_light</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_light</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_light</item>
</style>

<style name="DarkTheme" parent="@android:style/Theme.Black">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_dark</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_dark</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_dark</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_dark</item>
   </style>

Also I have a tab_shows.xml and tab_news.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="@drawable/ic_tab_shows_selected_light"/>
<item  android:state_selected="false" android:drawable="@drawable/ic_tab_shows_unselected_light" />

How I can reference to needed image in selector according to current theme? This not work for me

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item  android:state_selected="false" android:drawable="?tabShows" />

In layout files this works, I mean reference to style via ?styleName

like image 699
Georgy Gobozov Avatar asked Sep 23 '11 13:09

Georgy Gobozov


1 Answers

Build your style A and style B

in your case you put android:drawable="@drawable/ic_tab_shows_selected_light" instead of background (I just copied snipets from my code) #000

    <style name="styleB">
        <item name="android:background">#000</item>
    </style>

your theme A

<style name="Theme.A">
        <item name="pageBackground">@style/styleA</item>
    </style>

theme B

<style name="Theme.Blue">
        <item name="pageBackground">@style/styleB</item>
    </style>

in your attr.xml

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

finally in your widget you do style="?pageBackground"

like image 120
Lukap Avatar answered Oct 15 '22 11:10

Lukap