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>
Related
the problem is:
When I open the application, I set the theme according to the theme of the device. In android code it is = mode_night_follow_system.
Let's imagine that the theme of the system(device) is dark.
When logging into the mobile app, I do
AppCompatDelegate.setDefaultNightMode(mode_night_follow_system)
The application changes the color to dark, but it feels like it does not take my colors indicated in color-dark.
The problem is only for xiaomi phones with android 10+
On other models Samsung, Huawei, etc., there are no problems.
What is the problem?
I had the same issue this solved my problem Speciall the second option.
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>
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
...
According to the docs, I only need to set android:forceDarkAllowed=true in my activity manifest and inherit theme from parent="Theme.MaterialComponents.DayNight". I tried that, but it didn't work.
Here's my manifest file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:forceDarkAllowed="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And here's my styles.xml styles:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
</style>
<style name="AppTheme.Dark" parent="Theme.MaterialComponents.DayNight">
...
</style>
I tried to get the theme name of the activity by using this code below:
resources.getResourceName(
packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
.applicationInfo.theme
)
It shows me com.example.name:style/AppTheme and not AppTheme.Dark. How can I make it so that when I run the application, the MainActivity automatically sets itself to use AppTheme.Dark (i.e. dark mode) using android:forceDarkAllowed?
That is not how dark mode works on Android 10.
First, Android does not magically append .Dark to your theme at any point, regardless of whether or not the user enabled dark mode. You have android:theme="#style/AppTheme". So, Android will use AppTheme all the time, and your AppTheme.Dark will not be used.
Second, the idea behind DayNight is that you use that as you base theme all the time, and Android will switch between normal and dark modes based on user request. So, if you switch AppTheme to extend DayNight, and get rid of AppTheme.Dark, you will be matching what the documentation calls for.
android:forceDarkAllowed is for cases where you are not in position to use a DayNight theme, and you want Android to try to automatically convert your UI to a dark theme. If you wanted to keep AppTheme extending Light instead of DayNight, this might work (I personally have not tried it with a MaterialComponents theme). But, even then, it will only be when the user has enabled dark mode (e.g., tapped on the notification shade tile).
For example, in this sample app, I use DayNight for my theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
I have different definitions for those colors in res/values/ and res/values-night/. I do not use forceDarkAllowed. And, when the user toggles night mode, my UI goes from:
to:
How can I make it so that when I run the application, the MainActivity automatically sets itself to use AppTheme.Dark (i.e. dark mode) using android:forceDarkAllowed?
You don't.
If you want a dark theme all the time, use a regular theme (not Light, not DayNight).
If your app uses a dark theme (such as Theme.Material), Force Dark will NOT be applied. Similarly, if your app's theme inherits from a DayNight theme, Force Dark will NOT be applied, due to the automatic theme switching.
Dark theme is available in Android 10 (API level 29) and higher. Make sure that your device's API level is 29 + that your target SDK in the gradle is 29.
There are two ways to apply dark theme:
DayNight - custom-able theme - you control how dark/light theme will look like
Force dark - you control the light theme, and if the user chooses dark mode the system will convert your views to dark theme at runtime.
There are settings to allow dynamically setting day or night regardless of the current system setting.
Each of the options map directly to one of the AppCompat.DayNight
modes:
Light - MODE_NIGHT_NO
Dark - MODE_NIGHT_YES
System default - MODE_NIGHT_FOLLOW_SYSTEM
To switch the theme, call AppCompatDelegate.setDefaultNightMode().
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#change-themes
This would apply to setting a DayNight theme. If you are trying to take advantage of the Force Dark in Android 10, it applies to the .Light sub-themes. It will not be applied to any of the DayNight or inherently dark themes.
Apps must opt-in to Force Dark by setting
android:forceDarkAllowed="true" in the activity's theme. This
attribute is set on all of the system and AndroidX provided light
themes, such as Theme.Material.Light.
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#force-dark
Below that, the documentation also specifies how to exclude certain items, either in resources or code.
Force Dark can be controlled on specific views with the
android:forceDarkAllowed layout attribute or with
setForceDarkAllowed().
Beyond that, there are ways to detect the current system light / dark setting and respond to it using the options above, but that is for another post.
It's not a great solution for a dark theme and I apologize for any overlap with the accepted answer, but I do not agree with claiming something isn't possible because it isn't preferred.
I have defined
<item name="android:statusBarColor">#color/some_color</item>
<item name="android:navigationBarColor">#color/some_color2</item>
and it works well in emulator with API 22 but in my Huawei with the same Android version status bar and navigation bar color not works. It remains the same like before.
I can add that another attributes like android:textApperance or android:background works fine there is a problem just with those two.
Place this is your values/styles.xml
<style name="MyMaterialTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>
and set this theme to your Activity in manifist
like this
<activity
android:name=".MainHomeFragmentActivity"
android:theme="#style/MyMaterialTheme" />
Huawei has not implemented in EMUI 3.1 based on Lollipop 5.1.1 which have colors of status bar. So there are 2 ways to solve it.
1-)
The files you need:
[emui3.1.png https://drive.google.com/file/d/0BwQ4XeikVQ3kU2ZNVW53UFQ4WDQ/view?usp=sharing]
[emui3.1bg.png https://drive.google.com/file/d/0BwQ4XeikVQ3kVnh5UTBrS3F4aW8/view?usp=sharing]
This is an EMUI theme from EMUI offical BBS(this theme works well but without Translucent Status Bars): https://drive.google.com/file/d/0BwQ4XeikVQ3kRmdSZ19LblpiQ00/view?usp=sharing
HUAWEI LAUNCHER
Download any Huawei theme and get .hwt file.
Use any unzip sofaware and unzip it.
Put emui3.1bg.png into it(I think you will now how to do it.), then zip again, make sure it likes before(Well, added a photo...), change its name to XXX.hwt
Put the new .hwt into (NOT SDCARD1) /sdcard/HWThemes(if there is not it, you must create one)
Open THEMES APP on your EMUI phone and change to the theme you changed before.
Open emui3.1.png with system library, and set it to the launcher wallpaper.
It maybe works, if not, do 1-6 again.
Here is a tutorial: https://www.youtube.com/watch?v=HTweBQgjXr8
2-) You need to root your device. You can check here to root : https://www.kingoapp.com/root-huawei.htm
then [https://drive.google.com/file/d/0BwQ4XeikVQ3kZGJDMWpuSGF0bDA/view?usp=sharing] just flash this zip
After setting SeekBar with Holo style, I got warning that it's not supported pre-Holo APIs.
If I leave it this way, will this crash the app or pull pre-Holo style anyway? It does not crash on the emulator and I don't have 2.3.3 device.
The reason for asking his is odd behaviour. I tried to manually set style for Holo and pre-Holo using res/values-v11/ directories and placing styles.xml in each and setting the style of SeekBar to style="#style/settings_seekbar" .
Style for API 11+ looked like this
<style name="settings_seekbar">
<item name="android:seekBarStyle">#android:style/Widget.Holo.SeekBar</item>
</style>
and style for APIs older than 11 looked like this
<style name="settings_seekbar">
<item name="android:seekBarStyle">#android:style/Widget.SeekBar</item>
</style>
So it looked like this would work. But on either device with Android 4.0+, I don't see Holo's theme, but the old thick-yellow theme.
If this is the proper way of settings styles (in case the first solution will crash a device), where did I make a mistake thus Holo theme never appeared on newer devices?
It seems we can safely use a theme from the upper SDK because I found no indicators that it will ever crash the app.
If the theme does not exist, Android will use the appropriate lower-level theme.
The error we can see on the image is just a warning that UI will not look the same in the SDKs which do not support this theme.