How to disable night mode in my application even if night mode is enable in android 9.0 (pie)?
AndroidAndroid Problem Overview
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.
Android Solutions
Solution 1 - Android
You can put this, at the first line in the onCreate
method of your launcher activity.
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Solution 2 - Android
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.
Solution 3 - Android
In themes.xml change:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
to
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
Solution 4 - Android
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:
-
Append the following code inside your
<style>
tag in thestyles.xml
file:<item name="android:forceDarkAllowed">false</item>
Note: file path:
android/app/src/main/res/values/styles.xml
-
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>
-
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
compiledSdkVersion
to 29 or higher. -
To Change the
compiledSdkVersion
you should edit the thecompiledSdkVersion
in the app gradle properites existed in theandroid/app/build.gradle
file:android { compileSdkVersion 29
-
Maybe you face
compileSdkVersion rootProject.ext.compileSdkVersion
, don't worry you should change this in the android gradle properites existed in theandroid/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.
Solution 5 - Android
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);
}
}
Solution 6 - Android
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
Solution 7 - Android
This worked for me (april 2021)
- add this line under your theme:
<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>
- delete themes.xml (night) file (under values folder)
Solution 8 - Android
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
change both themes DayNight to Light
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
Solution 9 - Android
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>
Solution 10 - Android
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>
Solution 11 - Android
use this as parent for disable dark mode parent="Theme.AppCompat.Light.NoActionBar
Solution 12 - Android
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>
Solution 13 - Android
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.
Solution 14 - Android
This line of code works for me.
<!--To disable dark mode-->
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
Solution 15 - Android
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">
Solution 16 - Android
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.
Solution 17 - Android
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">
Solution 18 - Android
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.
Solution 19 - Android
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>
Solution 20 - Android
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
Solution 21 - Android
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.
Solution 22 - Android
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.
Solution 23 - Android
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
Solution 24 - Android
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>