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
Build your style
A
and styleB
in your case you put
android:drawable="@drawable/ic_tab_shows_selected_light"
instead of background (I just copied snipets from my code) #000your theme A
theme B
in your attr.xml
finally in your widget you do
style="?pageBackground"
You can find your answer here http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html
Edit
(Additional information by Lukap in comments)
themes.xml
and set the definitions of your styles there.attrs.xml
.styles.xml
.But you will need read more about the
attrs.xml
Instead, we are referring to the value of some other attribute –
activatedBackgroundIndicator
– from our inherited theme. Whatever the theme defines as being theactivatedBackgroundIndicator
is what our background should be.