Step 1: Create class as a ThemeConfiguration.

				
					package com.example.theme;

import static java.lang.annotation.RetentionPolicy.SOURCE;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.annotation.StringDef;
import androidx.appcompat.app.AppCompatDelegate;

import java.lang.annotation.Retention;
import java.util.Locale;


/**
 * created by Urvish Shiroya
 **/


public class ThemeConfiguration {

    @Retention(SOURCE)
    @StringDef({
            THEME_DARK,
            THEME_LIGHT,
            THEME_SYSTEM_DEFAULT,
            THEME_SYSTEM_BATTERY_SAVER
    })
    public @interface UiMode {
    }

    public static final String THEME_DARK = "dark";
    public static final String THEME_LIGHT = "light";
    public static final String THEME_SYSTEM_DEFAULT = "system_default";
    public static final String THEME_SYSTEM_BATTERY_SAVER = "battery_saver";

    private static final String SELECTED_MODE = "MODE.android.application";


    public static void setAppTheme(Activity activity) {
        switch (getThemeMode(activity)) {
            case THEME_DARK:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
            case THEME_LIGHT:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            case THEME_SYSTEM_DEFAULT:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                break;
            case THEME_SYSTEM_BATTERY_SAVER:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
                break;
        }
    }

    public static void saveAppTheme(Activity activity, @UiMode String mode) {
        persist(activity, mode);
    }


    public static String getThemeMode(Activity activity) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        return preferences.getString(SELECTED_MODE, Locale.getDefault().getLanguage().toLowerCase());
    }

    protected static void persist(Activity activity, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SELECTED_MODE, language);
        editor.apply();
    }

}

				
			

How to use.

				
					write below code in every activity in above of setContentView();
ThemeConfiguration.setAppTheme(this);
				
			

whare you change the theme write below code

				
					ThemeConfiguration.saveAppTheme(this, ThemeConfiguration.YOUR_THEME);
				
			

write below code for get current theme that you’ve set in application.

				
					ThemeConfiguration.getThemeMode(this);