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.
You can put this, at the first line in the onCreate method of your launcher activity.
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
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.
In themes.xml change:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
to
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
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 the styles.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 compileSdkVersion to 29 or higher.
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
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.
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);
}
}
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
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)
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
change both themes DayNight to Light
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
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>
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>
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
use this as parent for disable dark mode
parent="Theme.AppCompat.Light.NoActionBar
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>
Change the DayNight to Light in AppTheme in the styles.xml file.
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.
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.
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">
This line of code works for me.
<!--To disable dark mode-->
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
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.
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">
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.
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.
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>
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.
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>
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
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.
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
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.
I'm actually creating an app an I would like to use Material Theme Light.
So I put the line android:theme = "#android:style/Theme.Material.light"
on the manifest and no errors are displayed.
However when launching the app on my phone it crashes at launch although no errors are displayed in my xml/java sources.
I'm sure this line is the cause, because when I change the manifest to #style/AppTheme, it works and lauches.
It's driving me crazy, I need your help please.
It depends on your activity, What does your activity extend from? If it extends from AppCompatActivity you can't set material theme.
you should set AppTheme as your theme android:theme="#style/AppTheme"
and in style file, set AppCompat Theme as your parent:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Try this it will work click on values in resources than open styles.xml file and change this
with following
i was getting the same issue and it is due to the update in the material lib
Im using Theme.Holo.Light.DarkActionBar but I dont really like the Light Dialogs/AlertDialogs.
I want to change every Dialog to the dark Holo Dialog.
<style name="CustomDialogTheme" parent="#android:style/Theme.Holo.Dialog">
<item name="#android:background">#9933CC</item>
<item name="#android:textColor">#02FA07</item>
</style>
<style name="Holo.Light.DarkActionBar" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:dialogTheme">#style/CustomDialogTheme</item>
<item name="android:alertDialogStyle">#style/CustomDialogTheme</item>
</style>
Thats what I tried but it has no effect on any Dialogs.
Ok nvm Im stupid its android:alertDialogTheme instead of android:alertDialogStyle. But this messes up the Preference Dialogs, so I just keep using the Light Dialogs.
In AndroidManifest.xml under the application tag, add this line :
android:theme="Holo.Light.DarkActionBar"
The issue is that the styles you're changing android:background and android:textColor are not dialog attributes.
You can see a full list here under Theme.Holo.Dialog on line 1617.
The ones you have to change are probably android:windowBackground and textAppearance
My android application uses the following style:
<style name="AppTheme"
parent="android:style/Theme.Holo" ></style>
It is assigned for the whole application in the AndroidManifest.xml file :
<application
android:theme="#style/AppTheme"
[...]
Using this style, menus look like this :
However, if I use the light theme instead :
<style name="AppTheme"
parent="android:style/Theme.Light" ></style>
Menus will look like this :
My question is :
How can I apply the HOLO theme for the whole application and use only the LIGHT theme for menus ??
I just want to apply the dialog "style" of the LIGHT theme and apply the HOLO theme for everything else.
I believe you are looking for this:
<application
android:theme="#style/theme.holo.light>
Try to use "android:itemBackground" to change the background color of the menus
You can make your own white style. Put this into style.xml and give theme in AndroidManifest file this theme
<style name="MyGow.Default.NoActionBar" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/silver</item>
<item name="android:actionModeBackground">#color/white</item>
</style>