Tom
Tom

Reputation: 3627

Android / how to best define themes / values

I am trying to figure out the best way to pick best themes for new and old Android versions, e.g. by reading this Trying to use holo theme in Android not working where they define Holo in values-11 and Black in values

Black

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.MyTheme" parent="@android:style/Theme.Black" />
</resources>

Holo

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.MyTheme" parent="@android:style/Theme.Holo" />
</resources>

Question 1) Will that work no matter what minSdkVersion setting I use? I guess that Android for never versions will assume values-11 is better match than values.fallback when no specific folder matches

Question 2) Assuming above (which I think is the case), how/where do I best define my MyTheme extensions. Suppose I have:

<style name="MyTheme2" parent="MyTheme">
  <item name="android:windowTitleSize">44dip</item>
  <item name="android:windowTitleBackgroundStyle">@style/MyWindowTitleBackground</item>            
</style>               
<style name="MyWindowTitleBackground">
  <item name="android:background">@android:color/transparent</item>
  <item name="android:padding">0px</item>
</style>

Do I have to duplicate that in both values styles.xml files? Or can I somehow place that in a shared folder? From my understand, I don't think that is possible?

I am new to Android, so I am just trying to figure out what would be the best thing to do :)

Upvotes: 1

Views: 2109

Answers (1)

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

Independent customization should be created in values/styles. Follow this template: (ANDROID DEFAULT)

In values/styles

<resources>

    <!--
        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:style/Theme.Black">
        <!--
            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">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

In values-v11/styles

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- API 11 theme customizations can go here. -->
    </style>

</resources>

Upvotes: 1

Related Questions