29

I have activity like this:

package com.nkdroid.daynighttheme;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.TextView;

public class ModeActivity extends AppCompatActivity {

    private TextView txtModeType;
    int modeType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_mode);
        txtModeType = (TextView) findViewById(R.id.txtModeType);
        modeType = AppCompatDelegate.getDefaultNightMode();

        if (modeType == AppCompatDelegate.MODE_NIGHT_AUTO) {
            txtModeType.setText("Default Mode: Auto");
        } else if (modeType == AppCompatDelegate.MODE_NIGHT_YES) {
            txtModeType.setText("Default Mode: Night");
        } else if (modeType == AppCompatDelegate.MODE_NIGHT_NO) {
            txtModeType.setText("Default Mode: Day");
        }
    }
}`

Is it possible to get which mode (day or night) is active now if default mode set to AUTO?

Narek
  • 3,813
  • 4
  • 42
  • 58

5 Answers5

43

You can get the current mode using the following code,

int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're in day time
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're at night!
    case Configuration.UI_MODE_NIGHT_UNDEFINED:
        // We don't know what mode we're in, assume notnight
}

The following article by Chris Banes explains it nicely.

harshithdwivedi
  • 1,411
  • 16
  • 37
17

Somehow @harshithdwivedi's answer didn't work for me when the night mode is set from inside the app (using AppCompatDelegate). Otherwise it works fine.

So I had to add some additional checks like this:

public static boolean isNightModeActive(Context context) {
    int defaultNightMode = AppCompatDelegate.getDefaultNightMode();
    if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_YES) {
        return true;
    }
    if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_NO) {
        return false;
    }

    int currentNightMode = context.getResources().getConfiguration().uiMode
            & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
            return false;
        case Configuration.UI_MODE_NIGHT_YES:
            return true;
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
            return false;
    }
    return false;
}
algrid
  • 5,600
  • 3
  • 34
  • 37
4

More simple way...

val Context.isDarkMode
    get() = if (getDefaultNightMode() == MODE_NIGHT_FOLLOW_SYSTEM)
        resources.configuration.uiMode and UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
    else getDefaultNightMode() == MODE_NIGHT_YES
Renetik
  • 5,887
  • 1
  • 47
  • 66
3

If you are a kotlin developer, then you can use the code below to check which mode your app is in..

val mode = context?.resources?.configuration?.uiMode? and Configuration.UI_MODE_NIGHT_MASK
when (mode) {
    Configuration.UI_MODE_NIGHT_YES -> {}
    Configuration.UI_MODE_NIGHT_NO -> {}
    else -> {} //covers Configuration.UI_MODE_NIGHT_UNDEFINED
}

For more about the dark theme modes see;

https://github.com/googlesamples/android-DarkTheme/

KraffMann
  • 322
  • 1
  • 4
  • 14
Saurabh Khare
  • 1,227
  • 13
  • 24
0
mode = AppCompatDelegate.getDefaultNightMode();

        SharedPreferences preferencesnight  = android.preference.PreferenceManager.getDefaultSharedPreferences(MyApplication.this);

        if (preferencesnight.getBoolean(KEY_SIGNATURE,false))

        {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
          
        }

  else if(mode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

        {
            AppCompatDelegate.setDefaultNightMode(mode);
        }

        else

        {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
          
        }
 
Starbax
  • 1,005
  • 2
  • 12
  • 32
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '22 at 13:53