Can’t disable Night Mode in my application even with values-night

Issue

The problem is that when I look at the preview on the software everything is fine, but when I run the app on my phone everywhere is used the color white it turn dark (drawables and backgrounds). I haven’t used an alpha on the drawables, only android:color"@color/white".

The source of the problem is that my phone had the Night Mode enable, so it automatically changed the colors in the app.

To try to disable the Night Mode I created a folder values-night and copied my @styles and @colors to match the Day and Night Mode.

styles.xml (night)

<resources>

    <!-- Base application theme. -->
        <style name"MyTheme" parent"Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name"android:windowBackground">@color/colorAccent</item>
        <item name"colorPrimary">@color/colorPrimary</item>
        <item name"colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name"colorAccent">@color/colorAccent</item>
    </style>

</resources>

colors.xml (night)

<?xml version"1.0" encoding"utf-8"?>
<resources>
    <color name"colorPrimary">#1d1d1d</color>
    <color name"colorPrimaryDark">#1d1d1d</color>
    <color name"colorAccent">#F8F8F8</color>
    <color name"textColor">#1d1d1d</color>
</resources>

In my MainActivity.xml

<android.support.constraint.ConstraintLayout
    xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:app"http://schemas.android.com/apk/res-auto"
    xmlns:tools"http://schemas.android.com/tools"
    android:layout_width"match_parent"
    android:orientation"vertical"
    android:background"@color/colorAccent"
    android:theme"@style/MyTheme"
    android:layout_height"match_parent"
    tools:context".MainActivity">

In my Manifest.xml

 android:theme"@style/MyTheme"

I checked the answers on How to disable night mode in my application even if night mode is enable in android 9.0 (pie)? and Dark Theme in android 9.0 changes my app layouts ugly and did the values-night folder, but it didn’t resolve the problem.

But the problem persist, any idea what am I missing here? Thanks in advance.

Solution

I had the same issue on my Xiaomi Redmi Note 7. I tried this code inside MainActivity.create():

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

But it doesn’t work on some of Xiaomi devices including mine, though it works on other phones.

So the only solution I’ve found is to add <item name"android:forceDarkAllowed" tools:targetApi"q">false</item> to each of your AppTheme in styles.xml. Like this:

<style name"MyTheme" parent"Theme.AppCompat.Light.NoActionBar">
    <item name"android:forceDarkAllowed" tools:targetApi"q">false</item>
</style>

It worked fine for my phone.

Answered By – Captain Jorjik

Leave a Comment