201

I created my application before the android pie has been released, in every layout I put android: background = "white" it works fine in every device, but when my brother installed the application and enabled night mode my application becomes a disaster with one single move, everything turns into back and white movie.

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/COLOR_PRIMARY</item>
    <item name="colorPrimaryDark">@color/COLOR_PRIMARY</item>
    <item name="colorAccent">@color/COLOR_PRIMARY</item>
    <item name="cardViewStyle">@style/CardView</item>
    <item name="android:fontFamily">@font/helvetica</item>
</style>

my color primary is red.

sai ram
  • 2,019
  • 2
  • 8
  • 9
  • You must adapt your app for the night mode. Android-Q will out any time soon and all the app will get affected from the system settings . You can look for Dark theme tutorials .. – ADM Jul 24 '19 at 04:34
  • Does Android 9 (Pie) have such a feature? Did you mean Android Q? – Leo Jul 24 '19 at 04:42
  • @Leo No it's android pie – sai ram Jul 24 '19 at 04:46
  • That's the thing. Android Pie doesn't have a "Dark Theme". It was introduced in Android Q – Leo Jul 24 '19 at 04:49
  • 1
    Has anyone found a workaround for expo managed apps? (until this gets resolved https://github.com/expo/react-native-appearance/issues/60) – ramon_salla Dec 28 '21 at 11:03

31 Answers31

197

You can put this, at the first line in the onCreate method of your launcher activity.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Caesar
  • 9,483
  • 8
  • 40
  • 66
Ali Rezaiyan
  • 3,011
  • 3
  • 16
  • 27
149

Tested using Xiaomi Note 8 Pro MIUI 12.0.2 Android 10

Adding AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); to MainActivity.onCreate(); doesn't seems to be working

The working solution was to add <item name="android:forceDarkAllowed">false</item> inside our main AppTheme block in styles.xml

Example:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

Update for InputMethodService :

For InputMethodService / Maybe if we inflate layout in any Service
I found when in Dark Mode - Xiaomi MIUI 12.0.3 there's a Black color which gets inverted into White.
To prevent the inversion, so Black color will still be Black be sure to set a theme in InputMethodService.

At onCreate() before super.onCreate() call these methods

  • getApplication().setTheme()
  • setTheme()

For your information, I'm passing Light Theme (Theme.MaterialComponents.Light.NoActionBar) which have android:forceDarkAllowed=false.
In my case for InputMethodService just calling getApplication().setTheme() is not enough to prevent the inversion.

Fauzi Danartha
  • 1,709
  • 1
  • 8
  • 7
  • This is te answer that i have looking for. In my Xiaomi Redmi Note 8 when i have the nigth mode by default in system my app when i choce day mode dont work. But whit this false in my style is working fine. – Felix A Marrero Pentón Feb 04 '21 at 18:31
  • There's no styles.xml. Edit: adding it in both themes.xml worked. Edit2: setDefaultNightMode is still needed for it to work. – Benur21 Mar 09 '21 at 21:36
  • There is a bug with edittexts, edittext hints not visibile in this case. – Husniddin Muhammad Amin May 03 '21 at 07:28
  • 1
    Your solution is available only if my minimum API level is 29. How do you suggest to do it for an app that supports lower API level? – Yossi Feb 20 '22 at 12:47
  • @Yossi Hi, It ignores the minimum API if we add tools:targetApi="q" on android:forceDarkAllowed like this false Or you can create styles.xml for API 29 and add the android:forceDarkAllowed in there – Fauzi Danartha Feb 21 '22 at 02:30
109

In themes.xml change:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

to

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
Donovan Phoenix
  • 1,351
  • 1
  • 10
  • 8
103

React Native, Ionic (cordova, capacitor)

Actually, this answer is especially for React Native developers/apps:

Just like all of us know, the dark mode is fully available on Android 10

Dark theme is available in Android 10 (API level 29) and higher

And most likely this feature ruined your app design implementation, like SVG, font, and background colors. if you decided to fully disable force dark mode you should follow this approach:

  1. Append the following code inside your <style> tag in the styles.xml file:

    <item name="android:forceDarkAllowed">false</item>
    

    Note: file path: android/app/src/main/res/values/styles.xml

  2. After step 1 you shoule have something like below:

    <resources>
      <!-- Base application theme. -->
      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
          ...
          <item name="android:forceDarkAllowed">false</item>
      </style>
    </resources>
    
  3. Maybe the above steps don't help you but don't worry, just like is said in the top of this post, this feature is released on API level 29, this means you should change the compileSdkVersion to 29 or higher.

  4. To Change the compileSdkVersion you should edit the the compileSdkVersion in the app gradle properites existed in the android/app/build.gradle file:

    android {
        compileSdkVersion 29
    
  5. Maybe you face compileSdkVersion rootProject.ext.compileSdkVersion, don't worry you should change this in the android gradle properites existed in the android/build.gradle file:

    buildscript {
        ext {
            buildToolsVersion = "SomeVersion"
            minSdkVersion = SomeVersion
            compileSdkVersion = 29 // <======= this should be 29 or higher
            targetSdkVersion = SomeVersion
    

Hint: For being sure that you have a new build, completely delete your last build by using the rm -rf android/app/build command and uninstall the app on your Emulator/Device and then run yarn android again.

chenop
  • 4,743
  • 4
  • 41
  • 65
  • 1
    This answer belongs to me, if have question, just mention me. thanks. – AmerllicA Jan 27 '21 at 14:18
  • 1
    @AmerllicA I tried both your solution and the method in onCreate and nothing works. I made a new build and tried it on my Xiaomi Mi 10T Lite – Aldimir Feb 08 '21 at 09:18
  • @AldimirAleknsandrov, weird, because in any cases it should work, actually, with this solution you completely turned of the dark theme, please replace 30 instead of 29, maybe it works, please inform me with effects. – AmerllicA Feb 08 '21 at 12:07
  • @AmerllicA I actually have the default at 29, and the min at 21 – Aldimir Feb 08 '21 at 12:50
  • Looks like it doesn't work for Xiaomi devices because doesn't work either on my Xiaomi Mi 9. It could be possible because Xiaomi uses a custom theme OS (MIUI) – Lorenzo Imperatrice Feb 12 '21 at 14:21
  • Worked for my Redmi Note 8, many thanks! My friend scared me when he showed up the black screen to me in his downloaded prod version. I've run `npx react-native-clean-project` before trying it out, as OP suggested to clean it up. – Henrique Bruno Feb 21 '21 at 02:45
  • Is there a solution for expo? – Mitch Apr 28 '21 at 19:15
  • This solution still works on SDK 30 and React 0.66.1, thanks! – Dobin Mar 10 '22 at 20:35
  • not work for API 31 – Diefair May 03 '22 at 10:21
35

If you want to avoid Activity recreations you could set the flag on the Application class as mentioned here

I recommend setting it in your application class (if you have one) like so:

public class MyApplication extends Application {
    
    public void onCreate() {
        super.onCreate();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}
Evin1_
  • 12,292
  • 9
  • 45
  • 47
25

What suggested by Ali Rezaiyan is the correct way to proceed if you want disable the night mode all over your app.

Sometimes you just need to disable the night mode on some activities or fragment only (maybe because of legacy stuff).

So you can choose what to do:

  • Disable all over the app:

    • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  • Disable for a single Activity:

    • getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    • After calling this you probably need to call recreate() for this to take effect
MatPag
  • 41,742
  • 14
  • 105
  • 114
  • 1
    I had a problem that was using `AppCompatDelegate.setDefaultNightMode()` in activity and this caused a call to the `onNewIntent` method when the activity start. but `getDelegate().setLocalNightMode()` save me a day. thank you. – Reza Abedi May 10 '22 at 09:12
22

This worked for me (april 2021)

  1. add this line under your theme:

<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>

  1. delete themes.xml (night) file (under values folder)
Ehsan Rosdi
  • 835
  • 9
  • 18
  • added this line in the `styles.xml` along with the `xmlns:tools="http://schemas.android.com/tools"` in resources tag. It hasn't worked for me. Am I supposed to add it somewhere else? – Suraj Ingle Aug 13 '21 at 15:39
  • You should add it in your 'themes.xml' file. Can find it under : res > values > themes.xml – Ehsan Rosdi Aug 14 '21 at 00:57
  • I'm working with react native and apparently it doesn't have a themes.xml Instead it has the styles.xml whit the following tags: ` – Suraj Ingle Aug 15 '21 at 11:33
  • But still the activity is restarted on theme change. How to prevent that? – Samudra Ganguly Apr 18 '22 at 12:43
19

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

change both themes DayNight to Light

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">

Abubakar
  • 111
  • 3
  • 13
14

This worked for me, I changed from <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> to <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">.

File path: android/app/src/main/res/values/styles.xml

All code:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:textColor">#000000</item>
</style>
Murilo Dutra
  • 151
  • 1
  • 5
9

Do not Use below code.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

This will call your activity twice.

Just simply Use this

In your Theme file

First make Style parent as Light

Theme.MaterialComponents.Light.NoActionBar

Then add this Line Under your style

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

So your code is look like this.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/primary_color_app</item>
        <item name="colorPrimaryVariant">@color/status_bar_color</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

        <!-- Customize your theme here. -->
    </style>

Note : If you have android studio updated version make sure you need to make style same for Night Theme. Same Color as Light theme

Bhavin Solanki
  • 418
  • 2
  • 10
  • According to Android documentation "If this method is called after any host components with attached AppCompatDelegates have been 'created', a uiMode configuration change will occur in each." I added this in the App class and nothing got called twice and it applied to the whole app. – ravindu1024 Jun 01 '23 at 14:57
8

create a base application class which inherits Application class and inside the override oncreate method put this line:-

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

also don't forget to add this under name tag in android manifest.

zimmy9537
  • 103
  • 1
  • 5
  • 1
    This should be the accepted answer. There are many answers here that are misleading. the activity restart (as expected for a theme change) will only occur if this is added in an Activity. There are no restarts or other issues if this is added in the onCreate of your Application class as this answer suggests. See here: https://developer.android.com/reference/androidx/appcompat/app/AppCompatDelegate#setDefaultNightMode(int) – ravindu1024 Jun 01 '23 at 15:08
7

in layout of your activity that you want to disable force dark, place following attribute in parent layout:

android:forceDarkAllowed="false"

your layout should be like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_height="match_parent"
android:forceDarkAllowed="false">

<!-- ..... -->

</androidx.constraintlayout.widget.ConstraintLayout>
Roya Rastgar
  • 144
  • 1
  • 3
7

Just Modify the few changes below in your project (themes.xml):

If your Android studio has the latest version, then apply this on both "themes.xml" and "themes.xml(night)".

Change:

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

To:

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

And then just add the below line:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
ouflak
  • 2,458
  • 10
  • 44
  • 49
7

Change the DayNight to Light in AppTheme in the styles.xml file.

OrenDinur
  • 101
  • 1
  • 3
6

use this as parent for disable dark mode parent="Theme.AppCompat.Light.NoActionBar

Shubham
  • 91
  • 1
  • 4
4

Just add

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

after

super.onCreate(savedInstanceState);

on every Activity

Next add

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

in your Theme.

Ganesh MB
  • 1,109
  • 2
  • 14
  • 27
4

Go and add:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

in your theme, even if dark mode is enabled in you're device. Light theme is applied in your application.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Shubham
  • 41
  • 1
3

I have gone through a lot of solutions but none was robust until I removed the "values-night" folder from the "res" folder.

And change use parent theme: <style name="Theme.CryptoKite" parent="Theme.MaterialComponents.Light.DarkActionBar">
Vikash Sharma
  • 539
  • 8
  • 13
3

This line of code works for me.

<!--To disable dark mode-->
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
2

using the following line can help you to not overload dark mode in your app.

android:forceDarkAllowed="false"

Example of usage,

<LinearLayout 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:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:forceDarkAllowed="false"
tools:context=".SplashActivity">
Rishabh Gupta
  • 119
  • 1
  • 5
2

Another option for Cordova only developers:

From https://developer.android.com/guide/webapps/dark-theme

"WebView can't differentiate based solely on the presence of the media query, so it needs a color-scheme meta tag with dark value as indication that a web page has its own dark theme. To prevent double-darkening (that is, applying color inversion on top of media query customization) WebView evaluates @media (prefers-color-scheme: dark) to false in this mode."

So I added <meta name="color-scheme" content="dark"> to my html and presto - it now detects my prefers-color-scheme: dark query and doesnt apply auto darkening.

Mikepote
  • 6,042
  • 3
  • 34
  • 38
2

In addition to Fauzi Danartha's answer to IMS part. One can extend theme with

<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>

not from material design's themes but from android:Theme.DeviceDefault.InputMethod (since targetSdkVersion 14).

Also calling getApplication().setTheme() is not necessary, just setTheme() is enough.

Checked on MIUI 12.0.4.

2

for me I had to add <item name="android:forceDarkAllowed">false</item> in the Theme.App.SplashScreen style also, Im using expo bare.

<resources xmlns:tools="http://schemas.android.com/tools">
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textColor">#000000</item>
    <item name="android:forceDarkAllowed">false</item>
  </style>
  <style name="Theme.App.SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Below line is handled by '@expo/configure-splash-screen' command and it's discouraged to modify it manually -->
    <item name="android:windowBackground">@drawable/splashscreen</item>
    <item name="android:forceDarkAllowed">false</item>
    <!-- Customize your splash screen theme here -->
  </style>
</resources>

B. Mohammad
  • 2,152
  • 1
  • 13
  • 28
2

Never add this code:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

If you put it in your oncreate Method of MainActivity, the onCreate method will called twice!!! It take me an hour to find out that adding this line caused the problem!

just add this line in your style.xml:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

it looks like this in my code:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30
  • thanks a lot, this worked for my apps too(which build on react 17 and capacitor v5). i had similar problem when Xiaomi phone get darkmode active. – nalendro16 May 20 '23 at 07:13
2

Change both Light and Dark themes to the same colours. That will not affect output even if the device is in dark mode.

Inside both value/theme.xml and night/theme.xml

Change the following line

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

to

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

The app will be in dark mode but even in the dark mode it will display colours of the light mode and it will stay always light.

Usman Khan
  • 80
  • 9
1

I'm using the kotline. In my case, I've added the following lines in the below of oncreate method

in an activity class and

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

 }

and in the value->styles->Themes/style class, I add the following code

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:forceDarkAllowed" tools:ignore="NewApi">false</item>
</style>
Bhavin Solanki
  • 418
  • 2
  • 10
M Azam Khan
  • 302
  • 3
  • 15
  • If you want to force the use Light theme in the whole app, you can add that line of code within the onCreate of your Application class. – joninx Mar 24 '22 at 14:51
1

After adding <item name="android:forceDarkAllowed">false</item> to the styles.xml file, dont forget to change parent theme from

Theme.AppCompat.DayNight.NoActionBar

to

Theme.AppCompat.Light.NoActionBar

uninstall app from the emulator, run cd android && ./gradlew clean and it should be working fine.

1

simply deleting the darkmode theme from themes folder worked for me.

Amos Banda
  • 11
  • 1
0

Simply Change this

<style name="YourThemeName" parent="Theme.MaterialComponents.DarkActionBar">

To

<style name="YourThemeName" parent="Theme.AppCompat.Light.DarkActionBar">

In both theme and theme night

and make sure all colors are same in the theme and theme night

Mirza Ali
  • 171
  • 1
  • 1
  • 7
  • It doesnt affect.. Becuase android system automatically changes colors of app.. Background colors or text colors arent declared in styles for many times – Samir Alakbarov Apr 06 '21 at 15:50
0

I have this force night mode problem in redmi note 7 pro. But, when I use

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

this method in my application level class and the whole app theme was changed as light theme but actually I wanted the theme as night too.

And I found solution for particular view as:

android:forceDarkAllowed="false"

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white_black"
    android:elevation="@dimen/dim4"
    android:forceDarkAllowed="false"
    android:forceHasOverlappingRendering="false"
    app:itemIconTint="@drawable/highlighted_bottom_nav_item"
    app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
    app:itemTextAppearanceInactive="@style/BottomNavigationView"
    app:itemTextColor="@drawable/highlighted_bottom_nav_item"
    app:labelVisibilityMode="labeled"
    app:menu="@menu/bottom_nav_menu" />

And just fixed it.

Divyesh Kevadiya
  • 41
  • 1
  • 3
  • 9
-1

This is an easy way:

Copy the content of the first file located at res>values>themes and paste it into the second file in the same directory.

values

Shahnazi2002
  • 43
  • 1
  • 8