Mehdi Fanai
Mehdi Fanai

Reputation: 4059

how to avoid styles duplication in backwards compatible android applications

I have an application built upon appcompat compatibility library. I have some customised styles for my application such as buttons, UI, etc in values/styles.xml. I have values-11/styles.xml values -16/stylex.xml values-21/styles.xml

My problem is: I dont want to repeat customised styles code in each styles.xml file. I want the newer versions inherit from main syles.xml and write only the new styles but this method does not work and i get errors saying i have not set a theme for my activity.

Here are my styles: values/styles.xml:

<resources>
<style name="Holo.Demo" parent="Theme.AppCompat" />
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="mycustomstyle">@drawable/ic_menu_add_user_holo_light</item>

values-v11/styles.xml:

<resources>
<style name="Holo.Demo.Theme.Light.DarkActionBar" parent="Holo.Demo.Theme.Light.DarkActionBar">

</style>

in my andoirdmanifest file, i have referred my activity theme to

Holo.Demo.Theme.Light.DarkActionBar

Upvotes: 0

Views: 121

Answers (1)

snachmsm
snachmsm

Reputation: 19223

when you are creating new project in Eclipse will be generated two styles in values/styles.xml:

<!--
    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="Theme.AppCompat.Light">
    <!--
        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>

for v11:

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

and v14:

    <!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

theme for whole application (tag in xml):

android:theme="@style/AppTheme"

read comments, they are useful. first of all - use AppCompat always if you are using it anyway. otherwise: second - you might wrote some lines inside "global" AppTheme for, lets suppose, styling button or textview, both are present across all versions/apis. if you want to change smth introduced in API 14 just wrote proper lines in v14 style AppBASETheme

Upvotes: 3

Related Questions