Android DarkMode : Value night not working

后端 未结 2 1604
离开以前
离开以前 2020-12-19 11:28

I am developing Day/Night feature in my app so I read those documents and start developing it.

It\'s working fine with default value in Day or Night with deligate m

相关标签:
2条回答
  • 2020-12-19 11:56

    Try below dark mode code which I am use.

    Step - 1

    First of create night folder into your resource file like below image(i.e. values-night)

    Step - 2

    Create styles,strings and colors xml file for night mode same as below image and add your night mode color,string and style which you want to show in your app when night mode was apply.

    For better user experience add window Animation in your style.

    values --> style.xml

    <resources xmlns:tools="http://schemas.android.com/tools">
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    
        <style name="NoActionBarAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/white</item>
            <item name="colorPrimaryDark">@color/white</item>
            <item name="colorAccent">@color/main_click_txt</item>
            <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
        </style>
    
        <!-- Add this theme where you change mode -->
        <style name="NoActionBarWithChangeTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/white</item>
            <item name="colorPrimaryDark">@color/white</item>
            <item name="colorAccent">@color/main_click_txt</item>
            <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
            <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
        </style>
    
        <!-- This will set the fade in animation on your change theme activity by default -->
        <style name="WindowAnimationTransition">
            <item name="android:windowEnterAnimation">@anim/fade_in</item>
            <item name="android:windowExitAnimation">@anim/fade_out</item>
        </style>
    
    </resources>
    

    values-night --> style.xml

    <!-- Base application theme. values-night.xml -->
    <style name="NoActionBarAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
        <item name="colorAccent">@color/main_click_txt</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
    </style>
    
    <!-- Add this theme where you change mode -->
    <style name="NoActionBarWithChangeTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
        <item name="colorAccent">@color/main_click_txt</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
    </style>
    
    <!-- This will set the fade in animation on your change activity by default -->
    <style name="WindowAnimationTransition">
        <item name="android:windowEnterAnimation">@anim/fade_in</item>
        <item name="android:windowExitAnimation">@anim/fade_out</item>
    </style>
    

    fade_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
        <alpha
            android:duration="2000"
            android:fromAlpha="0.0"
            android:toAlpha="1.0">
        </alpha>
    </set>
    

    fade_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator">
        <alpha
            android:duration="2000"
            android:fromAlpha="1.0"
            android:toAlpha="0.0" >
        </alpha>
    </set>
    

    Step - 3

    Add this below code in your splash screen if you want to set night mode as per device mode first time when application installed.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!CommonUtils.isToogleEnabled(SplashActivity.this)) {
            if (CommonUtils.isDarkMode(SplashActivity.this)) {
                CommonUtils.setIsNightModeEnabled(SplashActivity.this, true);
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                CommonUtils.setIsNightModeEnabled(SplashActivity.this, false);
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        } else {
            if (CommonUtils.isNightModeEnabled(SplashActivity.this)) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }
        super.onCreate(savedInstanceState);
    
        ...
        //your code
    }
    

    Step - 4

    Add this below code in your all activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (CommonUtils.isNightModeEnabled(MainActivity.this)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        super.onCreate(savedInstanceState);
    
        ...
        //your code
    }
    

    Step - 5

    Change mode using below code

    private WeakReference<Activity> mActivity;
    
    binding.imgNightMode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mActivity = new WeakReference<Activity>(MainActivity.this);
            CommonUtils.setIsToogleEnabled(MainActivity.this, true);
            if (CommonUtils.isNightModeEnabled(MainActivity.this)) {
                CommonUtils.setIsNightModeEnabled(MainActivity.this, false);
                mActivity.get().recreate();
            } else {
                CommonUtils.setIsNightModeEnabled(MainActivity.this, true);
                mActivity.get().recreate();
            }
        }
    });
    

    Below methods are CommonUtils.

    private static final String NIGHT_MODE = "NIGHT_MODE";
    private static final String TOOGLE = "TOOGLE";
    
    public static boolean isNightModeEnabled(Context context) {
        SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
        return mPrefs.getBoolean(NIGHT_MODE, false);
    }
    
    public static void setIsNightModeEnabled(Context context, boolean isNightModeEnabled) {
        SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(NIGHT_MODE, isNightModeEnabled);
        editor.apply();
    }
    
    public static void setIsToogleEnabled(Context context, boolean isToogleEnabled) {
        SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(TOOGLE, isToogleEnabled);
        editor.apply();
    }
    
    public static boolean isToogleEnabled(Context context) {
        SharedPreferences mPrefs = context.getSharedPreferences("MY_PREF", MODE_PRIVATE);
        return mPrefs.getBoolean(TOOGLE, false);
    }
    
    public static boolean isDarkMode(Activity activity) {
        return (activity.getResources().getConfiguration()
                .uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
    }
    

    I hope this can help you!

    Thank You.

    0 讨论(0)
  • Change appcompat dependency from 1.1.0 to 1.0.0, There is some issue to update resource in 1.1.0. This trick worked for me.

     implementation 'androidx.appcompat:appcompat:1.1.0'//Remove 
     implementation 'androidx.appcompat:appcompat:1.0.0' // Add
    

    Hope this will help you!!

    0 讨论(0)
提交回复
热议问题