Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 300
Emma.bk Avatar asked Sep 07 '20 16:09

Emma.bk


People also ask

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

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.

Why can't I turn off night mode?

In the menu, find Settings and tap on it. Tap on General. Find the Theme option, tap it, and you will get to choose between Light and Dark. There is a third option that will set the time in accordance with your device's global dark mode settings.

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

Disable all over the app: AppCompatDelegate. setDefaultNightMode(AppCompatDelegate. MODE_NIGHT_NO)

How do I permanently turn off dark mode?

Open Google in your mobile device's web browser. 2. At the top left, tap the three-line menu and then tap Dark theme: on to disable dark mode. From the same drop-down, you can also tap Settings for further customization.


3 Answers

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.

like image 41
Abdulwahab Hassan Avatar answered Oct 10 '22 09:10

Abdulwahab Hassan


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.

like image 147
Litvinov Nikita Avatar answered Oct 10 '22 08:10

Litvinov Nikita


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

like image 4
Omar Adel Avatar answered Oct 10 '22 09:10

Omar Adel