Android Code Snippet Version safe themes

Update: If you are using B4A v8.0+ then you should just add:

B4X:
CreateResourceFromFile(Macro, Themes.DarkTheme) 'or Themes.LightTheme

Older versions:
If you are not explicitly setting the theme in the manifest editor then it will change based on the device version and targetSdkVersion value.

Assuming that targetSdkVersion is set to 26 (as soon required by Google) then the theme used by default will be:

Device version

Android 2.x - Always the old Android 2.x theme
Android 4.x - Theme.Holo (dark background)
Android 5.x - 6.x - Theme.Material (dark background)
Android 7.x+ - Theme.Material.Light.DarkActionBar (light background)

This behavior can be problematic. In most cases it is better to explicitly set the theme. The correct way to set it is with manifest code such as:

Dark theme
B4X:
SetApplicationAttribute(android:theme, "@style/DarkTheme")
CreateResource(values-v20, theme.xml,
<resources>
    <style
        name="DarkTheme" parent="@android:style/Theme.Material">
    </style>
</resources>
)
CreateResource(values-v14, theme.xml,
<resources>
    <style
        name="DarkTheme" parent="@android:style/Theme.Holo">
    </style>
</resources>
)

Light theme
B4X:
SetApplicationAttribute(android:theme, "@style/LightTheme")
CreateResource(values-v20, theme.xml,
<resources>
    <style
        name="LightTheme" parent="@android:style/Theme.Material.Light">
    </style>
</resources>
)
CreateResource(values-v14, theme.xml,
<resources>
    <style
        name="LightTheme" parent="@android:style/Theme.Holo.Light">
    </style>
</resources>
)
 
Last edited:

Jorge M A

Well-Known Member
Licensed User
Longtime User
Sorry for the silly question, does the name of the resource, like "values-v20" or "values-v14", have some meaning? or are arbitrary names? thanks.
 
Top