Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch themes (night mode) without restarting the activity?

I have made a few apps that support multiple themes, but I always had to restart the app when user switches theme, because setTheme() needs to be called before setContentView().

I was okay with it, until I discovered this app. It can seamlessly switch between two themes, and with transitions/animations too!

enter image description here

Please give me some hints on how this was implemented (and animations too). Thanks!

like image 971
user1032613 Avatar asked May 11 '15 17:05

user1032613


People also ask

How do I apply a Dark theme to all apps?

Turn Dark theme on or off in your phone's settings Important: When you turn on Dark theme for your phone, many apps also use Dark theme. On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

Should you keep dark mode on all the time?

Dark mode may work to decrease eye strain and dry eye for some people who spend a lot of time staring at screens. However, there's no conclusive date that proves dark mode works for anything besides extending the battery life of your device. It doesn't cost anything and won't hurt your eyes to give dark mode a try.

How do you change the day and night theme on Android?

Use the system setting (Settings -> Display -> Theme) to enable Dark theme. Use the Quick Settings tile to switch themes from the notification tray (once enabled). On Pixel devices, selecting the Battery Saver mode enables Dark theme at the same time. Other OEMs may or may not support this behavior.


1 Answers

@Alexander Hanssen's answer basically has answered this... Don't know why it was not accepted... Maybe because of the finish()/startActivity(). I voted for it and I tried to comment but cannot...

Anyway, I would do exactly what he described in terms of styles.

<style name="AppThemeLight" parent="Theme.AppCompat.Light">     <!-- Customize your theme here. -->     <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item> </style> <style name="AppThemeDark" parent="Theme.AppCompat">     <!-- Customize your theme here. -->     <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item> </style> <!-- This will set the fade in animation on all your activities by 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 with new intent:

Intent intent = new Intent(this, <yourclass>.class); startActivity(intent); finish(); 

I would do:

@Override protected void onCreate(Bundle savedInstanceState) {      // MUST do this before super call or setContentView(...)     // pick which theme DAY or NIGHT from settings     setTheme(someSettings.get(PREFFERED_THEME) ? R.style.AppThemeLight : R.style.AppThemeDark);      super.onCreate(savedInstanceState); }  // Somewhere in your activity where the button switches the theme btn.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {          // decide which theme to use DAY or NIGHT and save it         someSettings.save(PREFFERED_THEME, isDay());          Activity.this.recreate();     } }); 

The effect is as shown in the video...

like image 115
GKA Avatar answered Oct 01 '22 10:10

GKA