Android XML theme inheriting from two parent themes?

Gautam picture Gautam · Sep 27, 2012 · Viewed 10.3k times · Source

Android styles and themes always seem to make my head spin. I wanted to use the Holo UI across different versions of Android for my app. So I decided to extract the necessary resources by browsing the source.

I came across the following in android-15\data\res\values\themes.xml and I'm confused as to what exactly is being 'inherited' and from where:

<style name="Theme.Holo.Light" parent="Theme.Light">
    ...
    ...
</style>

The Android API Guide says :

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period.

But from the code above, it seems like Theme.Holo.Light is inheriting from Theme.Holo and from Theme.Light.

How does that work, or what am I not reading properly?

Answer

Shitesh picture Shitesh · Dec 7, 2012

I've been wondering about this as well, so I wrote a simple test app to try it. Resources file looks like this:

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

<style name="AppTheme.TestTheme" parent="android:Theme.Light">

</style>

So I apply AppTheme.TestTheme to the activity in the manifest file. AppTheme makes the window full-screen & not have a title bar. Theme.Light makes the window background light instead of the default dark. When the parent="android:Theme.Light" attribute is specified, the window is white and not fullscreen -- this means that the parent="..." attribute takes precedence over the name prefix, and the hierarchy appears to be TestTheme <- Theme.Light (light) <- Theme (dark).

With parent="android:Theme.Light" removed, the screen is dark and fullscreen, so the TestTheme <- AppTheme (fullscreen) <- AppBaseTheme <- Theme (dark) hierarchy is in place.

When parent="..." is specified, it makes no difference when I remove the prefix or not. So parent="..." seems to take definitely precedence. AppTheme.TestTheme does not inherit from both parents at once.

Now, looking at the default themes.xml, it seems that Theme.Holo.Light inherits from Theme.Light, and then all of the Holo stuff is specified manually in its description. So they named it Theme.Holo.Light not because it inherits from Holo, but because they wanted a name that describes it as 'the light version of Holo'. And because they wanted to be $@&!ing confusing.

This was tested on Gingerbread 2.3.3.