Android Code Snippet Disabling system darkmode in your app

I added this code to the code below to my apps manifest to disable forced dark mode by the system in my app. I thought it might be useful for someone, especially on Android phones like Xiaomi that forces your app to use dark mode irrespective of the theme in the manifest. There is an option to turn off forced dark mode per app in Xiaomi, the problem is that users don't even know they can, so your app will look bad if not optimized for dar mode. By adding this to the manifest, it will prevent the system from enforcing dark mode in your app (till you optimize your app for it anyway, which is actually the better choice.

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

The theme manifest looks like this:
B4X:
SetApplicationAttribute(android:theme, "@style/Custom")
CreateResource(values-v20, theme.xml,
<resources>
  <style
  name="Custom" parent="@android:style/Theme.Material.Light.NoActionBar">
     <item name="android:windowTranslucentNavigation">false</item>
     <item name="android:windowTranslucentStatus">true</item>
     <item name="android:forceDarkAllowed">false</item>
  </style>
</resources>
)
 

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
it happens :c

1613071458254.png
 

fbritop

Active Member
Licensed User
Longtime User
I added this code to the code below to my apps manifest to disable forced dark mode by the system in my app. I thought it might be useful for someone, especially on Android phones like Xiaomi that forces your app to use dark mode irrespective of the theme in the manifest. There is an option to turn off forced dark mode per app in Xiaomi, the problem is that users don't even know they can, so your app will look bad if not optimized for dar mode. By adding this to the manifest, it will prevent the system from enforcing dark mode in your app (till you optimize your app for it anyway, which is actually the better choice.

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

The theme manifest looks like this:
B4X:
SetApplicationAttribute(android:theme, "@style/Custom")
CreateResource(values-v20, theme.xml,
<resources>
  <style
  name="Custom" parent="@android:style/Theme.Material.Light.NoActionBar">
     <item name="android:windowTranslucentNavigation">false</item>
     <item name="android:windowTranslucentStatus">true</item>
     <item name="android:forceDarkAllowed">false</item>
  </style>
</resources>
)

Thanks @Marvel , it will not load with an error:

B4X:
java.lang.RuntimeException: Unable to start activity ComponentInfo{cl.abreme.android/cl.abreme.android.main}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Do you have a small working project to have a look at?

I also used this post, with no luck either:
https://www.b4x.com/android/forum/threads/disabling-system-darkmode-in-your-app.127411/#content
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
Just use ONLY this in your manifest, it should work.

B4X:
SetApplicationAttribute(android:theme, "@style/Custom")
CreateResource(values-v20, theme.xml,
<resources>
  <style name="Custom" parent="@android:style/Theme.Material.Light.NoActionBar">
     <item name="android:windowTranslucentNavigation">false</item>
     <item name="android:windowTranslucentStatus">true</item>
     <item name="android:forceDarkAllowed">false</item>
  </style>
</resources>
)
 
Top