androidthemesandroid-night-mode

How to enable MODE_NIGHT_YES in every activity with setLocalNightMode()?


I recently wanted to create a setting, to enable a night mode in my android app, i searched in the internet and found a nice solution with AppCompatDelegate´s DayNight theme and a short code fragment... :

if(settingsSharedPreferences.getBoolean(getString(R.string.design_dark_design_key),false)) 
    {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

...to enable dark mode for my MainActivity, if it is enabled in my app settings. It worked perfectly and changed my MainActivity in "DarkMode"! But if i open another activity, this not appears in dark mode like main activity, but if i add the code lines above to this new activity it also starts with dark mode. So my question:

Do i have to call this code lines in every activity or is there a way to set global night mode for any activity in my app?

UPDATE:

I just called the code baove in a new activity before the super.onCreate() and setContentView() methods and my whole app theme changed. But if i call it in MainActivity just the Theme of my MainActivity changes... Its really strange. Can i change whole theme also in MainActivity?

Thank you in advanced!


Solution

  • Ok, I've found a solution! For anyone who has the same problem, this is the correct way to apply dark design for your whole application: I got it from this NICE site

    https://blog.iamsuleiman.com/daynight-theme-android-tutorial-example/

    It's pretty easy, you just have to use:

    AppCompatDelegate.setDefaultNightMode(mode);
    

    instead of

    getDelegate().setLocalNightMode(mode);
    

    Quite easy, but really useful!!! Happy coding.

    Sorry for my English, I'm German :)