Newaj
Newaj

Reputation: 4450

Proper way of implementing night mode feature

I know by using AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); night mode can be implemented in an activity. But what I want is that - there'll be an option in settings for activating night mode in all the activities & fragment in the app. How can I do that?

Upvotes: 2

Views: 4406

Answers (2)

Mudassir
Mudassir

Reputation: 13174

You can use NightModeHelper to achieve this, just add the following line at the top of your activity's onCreate just after super.onCreate(); The idea here is to do it before we create any views. So the new views will use the correct Configuration.

mNightModeHelper = new NightModeHelper(this, R.style.AppTheme);

See a demo here https://github.com/zouliping/AndroidNightMode

Upvotes: 2

Arnold Brown
Arnold Brown

Reputation: 1433

One way to achieve this:

  • When you select the action night mode in setting activity(or any) you can maintain a flag(& save in sharedpreference or any temp storage).

  • Check this flag on each and every activity & fragment and if the flag is positive set the night mode as you used,

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

Upvotes: 7

Related Questions