android night mode implementation

Recently, the project needs to implement the night mode function. There are roughly two implementation methods:

  1. Set two sets of themes for daytime and nighttime, and use the method of setTheme to let Activity set the theme;
  2. Support day/night mode switching through Android Support Library: UiMode in the appcompat package (day and night modes are available after v23)

Method 1 needs to modify all the original layout files, the workload is huge, and it is not suitable for the current project that we have been working on for a long time, so we decided to use the second method. The specific implementation steps are as follows:

1. Add dependency packages:

compile 'com.android.support:appcompat-v7:24.2.1'

2. Initialize the uimode mode in the application:

public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    initThemeMode();

 

Record the last set mode through Sharedpreferences

private void initThemeMode() {
    if (!Constants.enableNightModel) return;
    isNight = SharedpreferencesUtil.isNightMode(this);
    if (isNight) {
        // night mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        //day mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}

 

Set day/night mode method for settingactivity to call

public void setTheme(AppCompatActivity activity, boolean mode,boolean recreate) {
    if (!Constants.enableNightModel) return;

    if (isNight == mode) {
        return;
    }
    if (!mode) {
        //day mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else {
        //day mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
    isNight = mode;
    SharedpreferencesUtil.saveNightMode(this,isNight);
    if (recreate){
        activity.recreate();
    }
}

 

3. Set the color/drawable corresponding to the day and night modes

 

3.1 Set two sets of colors

The color value names in color should be the same

Such as day mode T5 represents white

Night Mode T5 Code Gray Black

The color reference in the original layout file does not need to be changed

Note: For example, there is a color value 

<color name="A1">#FF3232</color>

The color in values ​​contains A1, the color in values-night does not have A1, the day and night mode can be accessed normally, and the color value in the day mode will be read in the night mode

But conversely, there is A1 in the color in values-night, and there is no A1 in the color in values, the night mode is normal, and the day mode is crashed

3.2 Setting image resources

Put the pictures in the corresponding mode into the corresponding folder, and the picture names should be consistent

 

Fourth, set the day and night mode in the settings page

private void setNightMode(boolean nightMode) {

        MyApplication.getInstance().setTheme(context, nightMode, false);
    }

This method is only valid for newly opened pages, and the opened pages need to call the recreate method to restart the page.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517088&siteId=291194637