android - How to switch themes (night mode) without restarting the activity? -
i have made few apps support multiple themes, had restart app when user switches theme, because settheme()
needs called before setcontentview()
.
i okay it, until discovered app. can seamlessly switch between 2 themes, , transitions/animations too!
please give me hints on how implemented (and animations too). thanks!
@alexander hanssen's answer has answered this... don't know why not accepted... maybe because of finish()/startactivity(). voted , tried comment cannot...
anyway, described in terms of styles.
<style name="appthemelight" parent="theme.appcompat.light"> <!-- customize theme here. --> <item name="android:windowanimationstyle">@style/windowanimationtransition</item> </style> <style name="appthemedark" parent="theme.appcompat"> <!-- customize theme here. --> <item name="android:windowanimationstyle">@style/windowanimationtransition</item> </style> <!-- set fade in animation on activities default --> <style name="windowanimationtransition"> <item name="android:windowenteranimation">@android:anim/fade_in</item> <item name="android:windowexitanimation">@android:anim/fade_out</item> </style>
but instead of finish/start new intent:
intent intent = new intent(this, <yourclass>.class); startactivity(intent); finish();
i do:
@override protected void oncreate(bundle savedinstancestate) { // must before super call or setcontentview(...) // pick theme day or night settings settheme(somesettings.get(preffered_theme) ? r.style.appthemelight : r.style.appthemedark); super.oncreate(savedinstancestate); } // somewhere in activity button switches theme btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // decide theme use day or night , save somesettings.save(preffered_theme, isday()); activity.this.recreate(); } });
the effect shown in video...
Comments
Post a Comment