L.Aaron
L.Aaron

Reputation: 43

What is the difference between attr of "colorPrimary" & "android:colorPrimary"?

For example: in values-v21/styles.xml

<style...>
    <item name="**android:**colorPrimary">@color/primaryColor</item>
</style>`

it works, while in values/styles.xml, it must be like this:

<style...>
     <item name="colorPrimary">@colorPrimary</item>
</style>`

why like this ?

Upvotes: 2

Views: 1206

Answers (3)

daemmie
daemmie

Reputation: 6460

In Short:

The style items without the android: prefix are used by the support lib. The ones without are provided by the API.

You might have noticed, that android studio gives you warnings, if the style items you use are below your min API if they haven't got the android prefix cause they might be ignored in older API versions.


Explanation:

For Example the Actionbar was introduced in API 11.

Normal usage :

<item name="android:actionBarStyle" tools:targetApi="11">@style/CustomActionBarTheme.AppCompat.ActionBar</item>

Support lib usage:

<item name="actionModeStyle">@style/CustomActionBarTheme.AppCompat.ActionMode</item>

If you you look into the Appcompat Theme, you will notice, that the Theme uses different values files according to your android version.

So the the default values files are in the values folder. But if we got a Lollipop device the values-v21 folder is used. Let's have a look into the files (this is the start of the main Theme):

values:

<style name="Platform.AppCompat" parent="android:Theme">
        <item name="android:windowNoTitle">true</item>

        <!-- Window colors -->
        <item name="android:colorForeground">@color/bright_foreground_material_dark</item>
        <item name="android:colorForegroundInverse">@color/bright_foreground_material_light</item>
        <item name="android:colorBackground">@color/background_material_dark</item>
        <item name="android:colorBackgroundCacheHint">@color/abc_background_cache_hint_selector_material_dark</item>
        <item name="android:disabledAlpha">@dimen/abc_disabled_alpha_material_dark</item>
....

values-v21:

<style name="Theme.Material">
        <item name="colorForeground">@color/bright_foreground_material_dark</item>
        <item name="colorForegroundInverse">@color/bright_foreground_material_light</item>
        <item name="colorBackground">@color/background_material_dark</item>
        <item name="colorBackgroundCacheHint">@color/background_cache_hint_selector_material_dark</item>
        <item name="disabledAlpha">@dimen/disabled_alpha_material_dark</item>
        <item name="backgroundDimAmount">0.6</item>

To summarize it: The support lib will use the values/drawables etc. if they are available in the used API. Otherwise it will use the values/etc. of the support lib.

Upvotes: 3

momvart
momvart

Reputation: 1999

When you set colorPrimary, you're setting a custom attribute which is made by support library to support material theme in API < 21. But android:colorPrimary is an attribute which is available in android API 21 and later for setting the theme.

Upvotes: 0

Bijesh P V
Bijesh P V

Reputation: 798

New version of android provides new Values

You can check Maintaining Compatibility

Upvotes: 0

Related Questions