Why and when we need to add android: prefix to style?

3.2k Views Asked by At

I have seen that some Style attributes require android prefix and some don't need it. What is the reason. like

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Why we haven't use android:windowActionBar and android:windowNoTitle

2

There are 2 best solutions below

2
On BEST ANSWER

Based on SDK version Android styles are grouped in different namespaces. In order to override a Theme you have to follow it's namespace.

That's the reason you don't need the android: prefix when extending an AppCompat Theme, but if you wanted something else, let's say Theme.Holo - you'd have 2 different styles for the them - one for pre-Lollipop devices, and one for -21, the latter having the android: prefix before each style attribute.

5
On

It depends on which Themes you're using and which context it is. These attributes are defined by different sources.

If the attribute name is prefixed with android:, it's a framework attribute and can only be used for the Android versions having it defined.

If the attribute is not prefixed at all, the attributes are defined by your own application. This includes all attribute definitions you pulled in with libraries.

In your example you're defining a theme for AppCompat which is part of the support library and thus of your application. The framework widgets won't recognize these colors directly.