Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between style and android:theme attributes?

Tags:

What are the key differences between android:theme and style attributes used for views like buttons and textviews in android layout xml files?

How to use them?

and When to use which?

like image 240
Vaibhav Surana Avatar asked Feb 17 '18 17:02

Vaibhav Surana


People also ask

What is the style of 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.

Is styles xml the same as themes xml?

There is no functional difference between styles. xml and themes.

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.


1 Answers

There are two key differences:

First, attributes assigned to a view via style will apply only to that view, while attributes assigned to it via android:theme will apply to that view as well as all of its children. For example, consider this style resource:

<style name="my_background">     <item name="android:background">@drawable/gradient</item> </style> 

If we apply it to a LinearLayout with three child TextViews by using style="@style/my_background", then the linearlayout will draw with a gradient background, but the backgrounds of the textviews will be unchanged.

If instead we apply it to the LinearLayout using android:theme="@style/my_background" then the linearlayout and each of the three textviews will all use the gradient for their background.

The second key difference is that some attributes only affect views if they are defined in that view's theme. For example, consider this style resource:

<style name="checkboxes">     <item name="colorAccent">#caf</item>     <item name="colorControlNormal">#caf</item> </style> 

If I apply this to a CheckBox using style="@style/checkboxes", nothing will happen. If instead I apply it using android:theme="@style/checkboxes", the color of the checkbox will change.

Just like the first rule said, styles containing theme attributes will apply to all children of the view with the android:theme attribute. So I can change the color of all checkboxes in a linearlayout by applying android:theme="@style/checkboxes" to my linearlayout.

like image 194
Ben P. Avatar answered Oct 12 '22 02:10

Ben P.