Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 519
Zar E Ahmer Avatar asked Dec 20 '16 09:12

Zar E Ahmer


People also ask

What is the use of style .xml in Android?

A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.

What is the difference between style and theme in Android?

A style is a collection of attributes that specify the appearance for a single View . A style can specify attributes such as font color, font size, background color, and much more. A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy—not just an individual view.

What is style explain with example in Android?

In android, the style is defined in a separate XML resource file and we can use that defined style for the Views in XML that specifies the layout. The Styles in android are similar to CSS styles in web design. Following is the example of defining a TextView control with required style attributes in an XML layout file.

How do I create a style folder in Android?

Right click on res folder, choose New --> Android resource file, set the same name for the new file "styles", in Available qualifiers: choose the last item "Version" and finally set "Platform API level" 21. Show activity on this post.


2 Answers

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.

like image 172
Ognian Gloushkov Avatar answered Oct 17 '22 20:10

Ognian Gloushkov


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.

like image 28
tynn Avatar answered Oct 17 '22 21:10

tynn