android - How to apply a Theme, which could be changed in runtime, to the UI? -
users of app have possibility change theme of application. can check in play store: https://play.google.com/store/apps/details?id=at.guger.musixs
until now, every activities onapplythemeresource
-method overridden.
this costly, cannot done in 1 way, because every activity has own peculiarities, e.g. 1 has no actionbar.
there 1 other problem. these styles not applied alertdialogs, can see if check app!
so there better way apply styles? saw module called appthemeengine
, able around themes, 1 requires superuser-permissions, it's useless normal costumers.
@override protected void onapplythemeresource(resources.theme theme, int resid, boolean first) { super.onapplythemeresource(theme, resid, first); mpreferences = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); int ithemeid = r.style.noactionbarthemeblue; string sprefblue = getresources().getstring(r.string.pref_color_blue); string sprefgray = getresources().getstring(r.string.pref_color_gray); string sprefgreen = getresources().getstring(r.string.pref_color_green); string spreforange = getresources().getstring(r.string.pref_color_orange); string sprefred = getresources().getstring(r.string.pref_color_red); string sprefcolor = mpreferences.getstring(getresources().getstring(r.string.keythemecolor), sprefblue); if (sprefcolor.equals(sprefblue)) { ithemeid = r.style.noactionbarthemeblue; } else if (sprefcolor.equals(sprefgray)) { ithemeid = r.style.noactionbarthemegray; } else if (sprefcolor.equals(sprefgreen)) { ithemeid = r.style.noactionbarthemegreen; } else if (sprefcolor.equals(spreforange)) { ithemeid = r.style.noactionbarthemeorange; } else if (sprefcolor.equals(sprefred)) { ithemeid = r.style.noactionbarthemered; } runtimeinfo.setthemeid(ithemeid); theme.applystyle(ithemeid, true); }
my styles.xml:
<resources> <style name="actionbarthemeblue" parent="theme.appcompat.light.darkactionbar"> <item name="android:windowbackground">@android:color/white</item> <item name="colorprimary">@color/blueprimary</item> <item name="colorprimarydark">@color/blueprimarydark</item> <item name="coloraccent">@color/blueaccent</item> </style> <style name="noactionbarthemeblue" parent="theme.appcompat.light.noactionbar"> <item name="android:windowbackground">@android:color/white</item> <item name="colorprimary">@color/blueprimary</item> <item name="colorprimarydark">@color/blueprimarydark</item> <item name="coloraccent">@color/blueaccent</item> </style> <style name="actionbarthemegray" parent="theme.appcompat.light.darkactionbar"> <item name="android:windowbackground">@android:color/white</item> <item name="colorprimary">@color/grayprimary</item> <item name="colorprimarydark">@color/grayprimarydark</item> <item name="coloraccent">@color/grayaccent</item> </style> <style name="noactionbarthemegray" parent="theme.appcompat.light.noactionbar"> <item name="android:windowbackground">@android:color/white</item> <item name="colorprimary">@color/grayprimary</item> <item name="colorprimarydark">@color/grayprimarydark</item> <item name="coloraccent">@color/grayaccent</item> </style> <style name="actionbarthemegreen" parent="theme.appcompat.light.darkactionbar"> <item name="android:windowbackground">@android:color/white</item> <item name="colorprimary">@color/greenprimary</item> <item name="colorprimarydark">@color/greenprimarydark</item> <item name="coloraccent">@color/greenaccent</item> </style> <style name="noactionbarthemegreen" parent="theme.appcompat.light.noactionbar"> <item name="android:windowbackground">@android:color/white</item> <item name="colorprimary">@color/greenprimary</item> <item name="colorprimarydark">@color/greenprimarydark</item> <item name="coloraccent">@color/greenaccent</item> </style> ... </resources>
i hope knows better way of changing themes! thanks!
Comments
Post a Comment