You'll need at least 2 styles, best inheriting from base styles, e.g. Theme.Material
variants, or if you use appcompat
then Theme.AppCompat
variants. In each style override values such as colours, drawables etc with theme-specific values.
values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- original theme attributes -->
...
<item name="android:textColorPrimary">#FFFFFF</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- alternative theme attributes -->
...
<item name="android:textColorPrimary">#000000</item>
</style>
This will be sufficient if you only use framework or appcompat
attributes (e.g. colorAccent
, android:textColorPrimary
etc) in your layouts. But if you need your own attributes (e.g. a drawable with color that is different per theme), then you will need to define custom attributes.
values/attrs.xml
<attr name="themedMenuStoryDrawable" format="reference" />
<attr name="themedMenuCommentDrawable" format="reference" />
...
Specify theme-specific values for your custom attributes:
values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- original theme attributes -->
...
<item name="themedMenuStoryDrawable">@drawable/ic_subject_white_24dp</item>
<item name="themedMenuCommentDrawable">@drawable/ic_mode_comment_white_24dp</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- alternative theme attributes -->
...
<item name="themedMenuStoryDrawable">@drawable/ic_subject_black_24dp</item>
<item name="themedMenuCommentDrawable">@drawable/ic_mode_comment_black_24dp</item>
</style>
Then refer to your custom attributes with ?attr/
prefix in layouts, menus etc:
menu/my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@id/menu_comment"
android:icon="?attr/themedMenuCommentDrawable" />
<item android:id="@id/menu_story"
android:icon="?attr/themedMenuStoryDrawable" />
</menu>
Check out my blog post for the complete guide.