I have force disable dark mode in my android application but still the app is in dark mode when my phone is in dark mode
I have also changed the theme for both light and night modes to light theme with no action bar but its not working
1)Add this line in each activity just below onCreate
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
OR
add this line in your themes under style section
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
Related
I start alpha tests of my reat natvie app and one of the testers reported to me that on his phone the app is looking like in dark mode:
I am really surprised because my application does not have any dark mode functionality implemented and should look like this:
Tester is using Xiaomi mi10 lite phone.
Most of my app screens has on top SafeAreaView with style like that:
container: {
flex: 1,
backgroundColor: colors.secondaryColor,
},
Where secondaryColor is '#abf0d1', sometimes backgroundColor is white like on second screenshot.
Does anyone have any idea what this color-inverting thing could be caused by?
This is because MIUI 12+ version has an advanced feature in Dark mode for Individual apps, This feature turns the Light theme app with no dark theme support to a Dark theme layout by inverting the layout colors.
If your app is only supporting Light theme you can prevent Force Dark mode by,
add a new property to your main AppTheme in res/values/ resources styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:forceDarkAllowed">false</item> // <-- add this
...
My new app doesn't support dark mode. When I install it on Xiaomi (with dark mode truned on) MIUI applies dark mode on it. MIUI has settings at "Settings -> Display -> More Dark mode options" (screenshot of "More Dark mode options"). This options is turned on for my app and forces dark mode despite that my app doesn't support it. Most other apps do not have this mode enabled. There are apps that are "white", but for them this mode is not enabled and they work correctly.
I found solution with adding the below line to the themes.xml:
<item name="android:forceDarkAllowed">false</item>
The problem is that, this line requires setting minSdkVersion = 29.
How to prevent MIUI enabling the option at "More Dark mode options" and forcing dark mode in my app (like in most other "white" apps) with keeping SDK version at 21?
MIUI 12 based on Android 10
Just copy your themes.xml file in a values-v29 folder and add <item name="android:forceDarkAllowed">false</item> only in the values-v29 variant of the file.
If you define many things in your theme file, might be a good idea to have something like:
values/themes.xml
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.NoActionBar">
... // Your attributes here
</style>
<style name="Theme.App" parent="Theme.App.Base">
</style>
values-v29/themes.xml
<style name="Theme.App" parent="Theme.App.Base">
<item name="android:forceDarkAllowed">false</item>
</style>
UPDATE: also, I don't think it crashes if you keep the item in your normal values folder, why do you think it's a problem in the first place? Doesn't something like this work?
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
I have two themes. One is:
<style name="ThemeDay" parent="Theme.AppCompat.Light.NoActionBar">
Other one is:
<style name="ThemeNight" parent="Theme.AppCompat.DayNight.NoActionBar">
I apply themes in every activity like this before super.onCreate()
if(GenelUtil.getNightMode()){
setTheme(R.style.ThemeNight);
}else{
setTheme(R.style.ThemeDay);
}
Theme is applied. But system doesnt behave like selected theme.
For example when system is light mode and app is dark mode, Navigation bar is still white, dialogs are still white etc..
But when I open instagram and switch to dark mode, system also behave like its in dark mode.
What do I do wrong here? How can I fix this?
DayNight theme is picking automatically dark or light params/colors basing on system settings (by default) or set/forced by you (e.g. using AppCompatDelegate.setDefaultNightMode())
when you have light theme in your system and you will use DayNight theme for your app then still not overriden colors will be same as in "light" theme
So far I have learnt from google, you have to use Theme.Appcompat.Daynight or Material Daynight theme to make your app support dark mode. And you need to use different styles.xml in values and values-night directories.
I am not willing to make my app support dark theme. But when I change android system theme (from notification panel) to dark, my app becomes dark.
I am using Theme.Appcompat.Light as base theme, did nothing for my app to support dark mode, still my app becomes dark. I am using cardview for the very first time, don't know if it may be the cause since I am very new in android programming.
Your little help will be much welcome. Please, check screenshots here:
System dark mode off
System dark mode on
You can use this in all application
class AppController: Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
in android 10 ,android forse dark to app to pervent that ,
add below to your defult theme in style :
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
Add this line inside onCreate method of the MainActivity before calling setContentView:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
Since Android 10, you can switch between dark mode and default light mode. I didn't make any closer research yet on this since it's a new topic. Is Dark mode color switching automatic by OS or is there any way to tell my App to switch different app-theme if Dark mode is turned on? Also dark mode is possibility for some Android 9 devices.
Because I made custom Dark theme with custom parameters and I set dark color for each of my color in resources (using custom attributes in attrs.xml and applied specific color resource to them inside theme in styles.xml). Same it is working for different color schemes of my App (blue, red, green for example). That way I can tell which color would be used for different views in my App and not system.
Only thing I need is to detect if dark mode is on/off in system. I can force user to turn on Dark mode in App settings (custom settings) but theme can be affected by system dark mode (turned on in Phone settings).
From official documentations:
In order to support Dark theme, you must set your app's theme (usually found in res/values/styles.xml) to inherit from a DayNight theme:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
You can also use MaterialComponents' dark theming:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
This ties the app's main theme to the system-controlled night mode flags and gives the app a default Dark theme (when it is enabled).
It means that if you use a DayNight theme, the OS itself handles the app theme. if you want to force app uses dark theme check this documentation.
UPDATE:
To detect the device theme:
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_YES:
…
break;
case Configuration.UI_MODE_NIGHT_NO:
…
break;
}