Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does theme inheritance work in Android?

Tags:

android

I have the following theme in res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppBaseTheme" parent="@android:style/Theme.NoTitleBar">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

If I remove the <item name="android:windowNoTitle">true</item> line, my activities have the titlebar on them. However, if I keep that line there, then they do not have a titlebar (which is what I want).

I'm curious why parent="@android:style/Theme.NoTitleBar" seems to have no effect? Does parent not do what I think it does? I thought it worked like this:

@android:style/Theme.NoTitleBar --> AppBaseTheme --> AppTheme
^                                   ^                ^
|                                   |                |
|                                   |                Has everything AppBaseTheme
|                                   |                does, unless it's overridden
|                                   |
|                                   Has everything @android:style/Theme.NoTitleBar
|                                   does, unless it's overridden (which I'm not
|                                   doing)
|
Sets whatever it needs to to not have a title, which I assume
is done by setting <item name="android:windowNoTitle">true</item>

However, I've also found that if I set <item name="android:windowNoTitle">true</item> in AppBaseTheme it also has no effect; I have to set it in AppTheme. Then what the heck is the point of specifying the parent?

like image 715
Cornstalks Avatar asked Jan 25 '13 22:01

Cornstalks


People also ask

How does Theme work in Android?

A theme defines a collection of named resources which can be referenced by styles, layouts, widgets, and so on. Themes assign semantic names, like colorPrimary , to Android resources. Styles and themes are meant to work together.

What is parent style android?

Parent attribute means that your current themes extends this Parent theme. It has all its attributes that you can override in your current style. It is a kind of inheritance for styles.

What is theme give example in Android?

A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View. Thus, when a style is applied as a theme, every View in the Activity or application will apply each style property that it supports.


1 Answers

I think I just figured it out. It turns out that when I created the project, Android created a res/values-14/styles.xml file, and inside of that was <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">.

It turns out that the res/values-14/styles.xml file was overriding my default res/values/styles.xml because the device I was testing on has API level 14, and thus it preferred the files in values-14 than the default values.

Inheritance does seem to work like I thought it did; I just wasn't realizing things were being overridden by the files created when I first made my project.

like image 115
Cornstalks Avatar answered Oct 26 '22 12:10

Cornstalks