Android Question Best practice for Android Dark theme?

Similar threads

B4A Code Snippet Theme Colors
B4A Code Snippet Full Screen Theme
Other B4A v8.0 has been released!
B4A Code Snippet Change the action bar height
B4A Code Snippet Version safe themes


Licensed User
Hi all,

I have a question concerning the Dark theme that has been introduced with Android 10.
What is the best practice to support Dark themes in B4A?
How can I select an image based on the night mode flag?

thanks a lot,


Staff member
Licensed User
You can use AppCompat for this and set the theme to be:
CreateResource(values, theme.xml,
    <style name="MyAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
If you use the default colors then most views will look properly automatically.

You can check whether night mode is active:
Sub IsNightMode As Boolean
   Dim ctxt As JavaObject
   Log(ctxt.RunMethodJO("getResources", Null).RunMethodJO("getConfiguration", Null).GetField("uiMode"))
   Return Bit.And(ctxt.RunMethodJO("getResources", Null).RunMethodJO("getConfiguration", Null).GetField("uiMode"), 0x30) = 0x20
End Sub
You can set the default night mode if you don't want it to be based on the system default:
Sub SetDefaultNightMode (Mode As Int)
   Dim jo As JavaObject
   jo.InitializeStatic("").RunMethod("setDefaultNightMode", Array(Mode))
End Sub
The constants are listed here:

Note that the activity will be recreated if the theme changes.