Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DarkMode : Value night not working

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 method AppCompatDelegate.setDefaultNightMode(*).

For customize night theme color I create values-night folder and inside I create colors.xml file like below.

res -> values -> colors.xml
res -> values-night -> colors.xml

After I place that color but not applying in Night theme! Its very strange why value-night colors is not applying always its showing default night color? I have researched some but can't find the solution.

Note: Recreate activity is resolved my issue but I don't want to recreate

Here is my build.gradle file

enter image description here

Advance Thanks.

like image 316
Mohit Suthar Avatar asked Feb 10 '20 09:02

Mohit Suthar


People also ask

How do I set day and night mode on Android?

Use the system setting (Settings -> Display -> Theme) to enable Dark theme. Use the Quick Settings tile to switch themes from the notification tray (once enabled). On Pixel devices, selecting the Battery Saver mode enables Dark theme at the same time.

How do you disable night mode in my application even if night mode is enable in Android?

Turn Dark theme on or off in your phone's settings On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How do you make all apps darker on Android?

On the mobile app, open Apps Settings” from your profile picture. On Android, tap on your profile picture, then on Settings, and then General. There, choose Appearance and tap the checkbox next to Dark theme. On iOS, open Settings, then go to General and flip the Dark theme toggle switch.

How to enable Dark/Night Mode on Android devices?

Go to Settings–>Display and tap Night Mode. It might be called Dark Mode, depending on the device you have. You can set it to enable the feature manually. You can also set up specific times of the day to enable Dark/Night Mode based on your usage. Night Mode is baked into the Android settings of most phones.

When should I Turn on night mode on my phone?

You can use Night Mode throughout the day. The best time to have the filter on is when it’s dark outside or in your room. If you use your phone in bed before going to sleep, you should enable Night Mode. Why can’t I find Night Mode on my phone?

Why is the global dark mode not working on my Device?

Now, if the Global Dark mode is not working as intended when enabled via the regular settings, make sure to check for the option in Developer options. To enable Developer options, navigate to Settings > System or About Phone.

How to get WhatsApp dark mode on Android?

Additionally, Dark Theme for apps like WhatsApp is perpetually stuck in the Beta version. Now, since the official beta program for many apps is full, you can sideload the APK of the latest beta version and get the dark mode that way.


1 Answers

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)

Create night mode folder

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.

Create night mode xml files

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.

like image 65
Hardik Talaviya Avatar answered Nov 01 '22 09:11

Hardik Talaviya