SoFunction
Updated on 2025-04-09

Detailed explanation of how to quickly adapt to dark mode on Android

Directly upload the code

public class DarkModeUtils {

    public static final String KEY_CURRENT_MODEL = "night_mode_state_sp";

    private static int getNightModel(Context context) {
        SharedPreferences sp = (KEY_CURRENT_MODEL, Context.MODE_PRIVATE);
        return (KEY_CURRENT_MODEL, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }

    public static void setNightModel(Context context, int nightMode) {
        SharedPreferences sp = (KEY_CURRENT_MODEL, Context.MODE_PRIVATE);
        ().putInt(KEY_CURRENT_MODEL, nightMode).apply();
    }

    /**
     * ths method should be called in Application onCreate method
     *
     * @param application application
     */
    public static void init(Application application) {
        int nightMode = getNightModel(application);
        (nightMode);
    }

    /**
      * Apply Night Mode
      */
    public static void applyNightMode(Context context) {
        (AppCompatDelegate.MODE_NIGHT_YES);
        setNightModel(context, AppCompatDelegate.MODE_NIGHT_YES);
    }

    /**
      * Apply day mode
      */
    public static void applyDayMode(Context context) {
        (AppCompatDelegate.MODE_NIGHT_NO);
        setNightModel(context, AppCompatDelegate.MODE_NIGHT_NO);
    }

    /**
      * Dynamic switching is required when following the system topic
      */
    public static void applySystemMode(Context context) {
        (AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        setNightModel(context, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }

    /**
      * Determine whether the app is currently in dark mode
      *
      * @param context context
      * @return Return
      */
    public static boolean isDarkMode(Context context) {
        int nightMode = getNightModel(context);
        if (nightMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
            int applicationUiMode = ().getConfiguration().uiMode;
            int systemMode = applicationUiMode & Configuration.UI_MODE_NIGHT_MASK;
            return systemMode == Configuration.UI_MODE_NIGHT_YES;
        } else {
            return nightMode == AppCompatDelegate.MODE_NIGHT_YES;
        }
    }

}

Usage process

It is obvious that this is the method to switch to dark mode. Before calling, you must first adapt to the dark style of the app.

How to adapt

  1. Color resources
    Create a new values-night folder and replace all the color values ​​used in the page with the color values ​​in dark mode.
  2. Image Resources
    Create a new mipmap-night/drawable-night folder and replace the pictures and style resources used in the page with the corresponding resources in dark mode.
  3. Status bar
    The status bar that determines what color to display isDarkMode is the last method in the code above. It is best to operate in BaseActivity, otherwise it will be troublesome if there are many activities.
  4. Call
    Call the above method to switch the dark mode of the app. The points to note are as follows:

Need to pay attention

Just call it directly on androidx. Use it on support and toggle dark mode in Activity, you need to call the() method dynamically. For the specific reasons, see the following source code:

Androidx version:

/**
     * Sets the default night mode. This is the default value used for all components, but can
     * be overridden locally via {@link #setLocalNightMode(int)}.
     *
     * <p>This is the primary method to control the DayNight functionality, since it allows
     * the delegates to avoid unnecessary recreations when possible.</p>
     *
     * <p>If this method is called after any host components with attached
     * {@link AppCompatDelegate}s have been 'started', a {@code uiMode} configuration change
     * will occur in each. This may result in those components being recreated, depending
     * on their manifest configuration.</p>
     *
     * <p>Defaults to {@link #MODE_NIGHT_FOLLOW_SYSTEM}.</p>
     *
     * @see #setLocalNightMode(int)
     * @see #getDefaultNightMode()
     */
    public static void setDefaultNightMode(@NightMode int mode) {
        switch (mode) {
            case MODE_NIGHT_NO:
            case MODE_NIGHT_YES:
            case MODE_NIGHT_FOLLOW_SYSTEM:
            case MODE_NIGHT_AUTO_TIME:
            case MODE_NIGHT_AUTO_BATTERY:
                if (sDefaultNightMode != mode) {
                    sDefaultNightMode = mode;
                    applyDayNightToActiveDelegates();
                }
                break;
            default:
                (TAG, "setDefaultNightMode() called with an unknown mode");
                break;
        }
    }

Support version:

public static void setDefaultNightMode(int mode) {
        switch(mode) {
        case -1:
        case 0:
        case 1:
        case 2:
            sDefaultNightMode = mode;
            break;
        default:
            ("AppCompatDelegate", "setDefaultNightMode() called with an unknown mode");
        }
    }

After comparison, we can find that after Androidx switched to dark mode, it took the initiative to call the apply method to rebuild the Activity. There is no support, it is just assignment. Therefore, you need to call the() method yourself when using the support version.

Summarize

This is the end of this article about how Android can quickly adapt to dark mode. For more related content on Android to dark mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!