Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to detect if night mode is on when using AppCompatDelegate.MODE_NIGHT_AUTO

I'm using Androids built in day/night mode functionality and I'd like to add an option to my app for AppCompatDelegate.MODE_NIGHT_AUTO

I'm having a problem though because my app requires certain things to be colored programmatically, and I can't figure out how to check if the app considers itself in night or day mode. Without that, I can't set a flag to choose the right colors.

Calling AppCompatDelegate.getDefaultNightMode() just returns AppCompatDelegate.MODE_NIGHT_AUTO which is useless.

I don't see anything else that would tell me, but there must be something?

like image 546
Ben987654 Avatar asked May 24 '17 23:05

Ben987654


People also ask

How do I know if my android is in night mode?

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.

How do you disable night mode in my application even if night mode is enable in Android 9.0 PIE )?

Disable all over the app: AppCompatDelegate. setDefaultNightMode(AppCompatDelegate. MODE_NIGHT_NO)

Do I have night mode on this phone?

On Android, click your profile image in the top left, then click settings and privacy > display and Sound, and toggle on dark mode. Gmail: Go to settings > general settings > theme. Then choose light, dark or system default.

How programmatically prevent dark mode in Android application?

If you want to disable Force Dark for specific views, you can do so by adding a android:forceDarkAllowed attribute to the view in the XML. You can also use setForceDarkAllowed() to do so programmatically.


2 Answers

int nightModeFlags =     getContext().getResources().getConfiguration().uiMode &     Configuration.UI_MODE_NIGHT_MASK; switch (nightModeFlags) {     case Configuration.UI_MODE_NIGHT_YES:          doStuff();          break;      case Configuration.UI_MODE_NIGHT_NO:          doStuff();          break;      case Configuration.UI_MODE_NIGHT_UNDEFINED:          doStuff();          break; } 
like image 176
ephemient Avatar answered Sep 20 '22 08:09

ephemient


If you are kotlin developer then you can use below code to judge dark mode.

when (context.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)) {     Configuration.UI_MODE_NIGHT_YES -> {}     Configuration.UI_MODE_NIGHT_NO -> {}     Configuration.UI_MODE_NIGHT_UNDEFINED -> {} } 

For more about dark theme mode

https://github.com/android/user-interface-samples/tree/main/DarkTheme

like image 39
Saurabh Khare Avatar answered Sep 20 '22 08:09

Saurabh Khare