Android Check Dark Mode or Light Mode on programmatically
Android Check Dark Mode or Light Mode on programmatically

Hi Guys, Welcome to Proto Coders Point, In this article will learn how to check if the device is in night node theme or light mode theme in android programmatically.

As you all know that switching to dark theme is avaiable on Android 10 & above version.

Android check dark mode or light mode programmatically

Now, when a Android Developer develop an application then, If device dark theme is enabled i.e(night mode), then automatically, the dark theme is been applied to the app(because) in theme.xml we have 2 theme one for light node and night mode, due to which our app theme don’t look good as certain places in app.

You’re app may face some UI_Mode appearance issue, at certain view in app and should be colored programmically so you might be wondering how can i check if night or day mode is enabled on my device progammically depending on which you can easy set proper them from you androd app


Solution is Here

Check DARK MODE or LIGHT MODE in Android

In Below snippet code I am going to change the Theme depending on our mobile device UI_MODE Theme (Light or Dark).

Below is just an example, on How to chec which UI_Mode is active,

Snippet Code

  int nightModeFlags =  view.getContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
     
    switch (nightModeFlags) {
     case Configuration.UI_MODE_NIGHT_YES:
           Log.e(TAG, "onClick: NIGHT YES ");
           AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
           break;

     case Configuration.UI_MODE_NIGHT_NO:
            Log.e(TAG, "onClick: LIGHT");
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;

     case Configuration.UI_MODE_NIGHT_UNDEFINED:
             //   doStuff();
             break;
    }