31

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?

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
Vaibhav Surana
  • 342
  • 4
  • 11
  • https://developer.android.com/guide/topics/ui/look-and-feel/themes.html – ADM Feb 17 '18 at 17:27
  • Thanks for the link @ADM, I already referred to the android docs about theming, but there was no clear mention or example of using `android:theme` with a **layout** or **view**. But they surely showed an example of using `android:theme` with **application** and **activity**, so I thought if `android:theme` was not meant to be used with **layouts** and **views**. – Vaibhav Surana Feb 18 '18 at 08:05

1 Answers1

41

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.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • 4
    Interesting. And how do you know which attributes only affect views if they are used in a theme and not a style? Is there a list somewhere? – SMBiggs Mar 14 '19 at 04:07
  • _. . .will apply to that view as well as all of its children._ -- This realization stumped me for a few hours. Can I ask how you discovered this information? – CzarMatt Nov 05 '19 at 19:34
  • 1
    @CzarMatt Unfortunately, the answer is "experimentally". I observed the exact scenario described in my answer during a real-world project I was working on. I applied a theme that set a gradient background to my `Toolbar`, and noticed that the title and the overflow icon got their own smaller gradients. – Ben P. Nov 05 '19 at 20:03
  • @BenP. Thanks for the reply - I know that feeling all too well. Also, after combing over the docs again, it turns out there's a note callout here: https://developer.android.com/guide/topics/ui/look-and-feel/themes#Styles that describes this fun issue. Perhaps the docs were updated with that info between the time I read them and recently. ¯\_(ツ)_/¯ – CzarMatt Nov 06 '19 at 17:09