12

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.

Emma.bk
  • 189
  • 1
  • 1
  • 11

5 Answers5

20

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.

Litva Nick
  • 483
  • 8
  • 11
  • 1
    This issue only occur in Redmi phones but in my case I just add this in theme its work like a charm. Thank you false – Anwar Zahid Oct 13 '21 at 05:30
  • The app theme is not changed, but still the activity is restarted whenever the device theme is being changed. Any suggestion to prevent that as well? – Samudra Ganguly Apr 18 '22 at 12:37
3

Just delete themes.xml(night) xml file or add Theme.AppCompat.Light.NoActionBar to themes.xml(night) xml file

Omar Adel
  • 47
  • 3
2

Step 1: Delete themes(night).xml from your values folder

Step 2: Change the parent attribute of your app style in themes.xml from

<style name="Theme.AppName" parent="Theme.MaterialComponents.DayNight.NoActionBar">

to

<style name="Theme.AppName" parent="Theme.MaterialComponents.Light.NoActionBar">

Note the change from "DayNight" to "Light"

Now, even if your phone is set to dark-mode, since no values for night exists and you are inheriting the light version of whatever Material Theme you choose, your app's theme should remain the same.

This is what worked for me. Let me know if it works for you too.

  • But it does not prevent the activity from restarting on theme change. I want my app to be completely immune to theme change. Any suggestion? – Samudra Ganguly Apr 18 '22 at 12:36
-1

Just delete themes-night file, that's all, works for me

Compound Z
  • 9
  • 1
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 01 '21 at 05:39
-1

1..Just copy your primary color from them(Light) and replace them(night) main color.

change this

style name="Theme.FakeWhatsApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"

to this

style name="Theme.FakeWhatsApp" parent="Theme.MaterialComponents.Light.NoActionBar"