Search code examples
androidandroid-studiomaterial-designandroid-themeandroid-styles

Android applying theme by version


I'm developing an application that starts with a listview. I'm in trouble with the setting of the themes for the application. Material design can be applied to Android 5.0 and above. So I created a values-21 folder and create styles.xml as follow:

<resources>
  <!-- Inherit from the light Material Theme -->
  <style name="MyCustomTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/My_LightBlue</item>
  </style>
</resources>

In Android under 5.0 version this theme is not applied because it not read the values-21 folder. I have to create the styles.xml also in values standard folder? It is mandatory to create a new theme to change te color of the app bar (this is my objective).

I'm a bit confused... Thank you


Solution

  • From https://developer.android.com/training/material/compatibility.html

    You can configure your app to use the material theme on devices that support it and revert to an older theme on devices running earlier versions of Android:

    1. Define a theme that inherits from an older theme (like Holo) in res/values/styles.xml.
    2. Define a theme with the same name that inherits from the material theme in res/values-v21/styles.xml.
    3. Set this theme as your app's theme in the manifest file.

    If the layouts that you design according to the material design guidelines do not use any of the new XML attributes introduced in Android 5.0 (API level 21), they will work on previous versions of Android. Otherwise, you can provide alternative layouts. You can also provide alternative layouts to customize how your app looks on earlier versions of Android.

    Create your layout files for Android 5.0 (API level 21) inside res/layout-v21/ and your alternative layout files for earlier versions of Android inside res/layout/. For example, res/layout/my_activity.xml is an alternative layout for res/layout-v21/my_activity.xml.

    check this answer and blog