Android Code Snippet Version safe themes

Discussion in 'Code Snippets' started by Erel, Dec 27, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

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

    Code:
    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
    Code:
    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
    Code:
    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: Mar 12, 2018
  2. Jorge M A

    Jorge M A Active Member Licensed 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.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes. values-v14 will be used on API level 14+ and values-v20 will be used on API level 20+ (Android 5+).
     
    Jorge M A and Claudio Oliveira like this.
  4. Multiverse app

    Multiverse app Active Member Licensed User

    Can the above be used while using appcompact?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Multiverse app likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice