Changing Android styles by using theme -


i have app few different activities. different activities have different styled buttons, texts, etc... i've set components have various styles based on location/activity. (eg. style="@style/mainmenuactionbartitle, or style="@style/mainmenuactionbartagline). these styles set background (drawable, mipmap, or color), textcolor, etc...

the app offering theme packs change colors of these various components throughout application, , hoping there way have styles same name, different values based on theme of app. way can change theme whenever activity loaded whatever theme user has chosen.

there's info here on how change standard widget & feel using themes, changes , feel standard-un-styled widgets.

is there way accomplish using themes, or wrong direction altogether? there better/easier route?

edit: after doing more research , more fiddling, i've realized want isn't far off how can accomplish this. want change component styles when set theme of activity.

one solution i've discovered use attributes theme can point different styles.

attrs.xml

<resources>    <!-- attributes referencing whatever style theme needs set up. -->    <attr name="main_menu_button_style_play" format="reference" /> </resources> 

themes.xml

<resources>    <!-- base application theme. -->    <style name="apptheme" parent="theme.appcompat.light.noactionbar">        <!-- app specific attributes -->        <item name="@attr/main_menu_button_style_play">@style/mainmenu.button.play</item>    </style>     <!-- blue application theme. -->    <style name="apptheme.blue" parent="apptheme">       <!-- app specific attributes -->       <item name="@attr/main_menu_button_style_play">@style/mainmenu.button.play.blue</item>    </style> </resources> 

styles.xml

<style name="mainmenu.button.play">     <item name="android:background">#f76d3c</item>     <item name="android:text">play</item> </style>  <style name="mainmenu.button.play.blue">     <item name="android:background">#2ca8c2</item> </style> 

activity.xml

<button android:id="@+id/main_play_button"         style="?attr/main_menu_button_style_play"/> 

this works well, , allows me set theme in activity.oncreate() method.

the annoying problem have solution android studio complains button missing layout_width , layout_height though they're defined in style. guess doesn't follow attribute reference through chosen theme.

another approach ended using more heavily use attributes. creating attributes properties values want change between themes. so, instead of main_menu_button_style_play defines style reference, used main_menu_button_play_background. approach same amount of work specifying style because themes can inherit, ide understands it.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -