androidthemes

Best practice for providing multiple Android application themes


I'm trying to work out how to organize an android application that will have multiple themes. here's a simplified example of my problem,

2 themes, "light" and "dark",
within each theme, two text colors: "enabled" and "disabled"

now the problem is that when i define my TextView, i don't want to call out "light" or "dark" there, i want to just specify the theme at the application level and have it applied. basically, i want CSS selectors. i want to be able to define my theme like,

<style name="Light.enabled" .../>
<style name="Light.disabled" .../>

and my text view like,

<TextView style="@style/.enabled" .../>
<TextView style="@style/.disabled" .../>

and have it apply "enabled" or "disabled" based on whether i've called out "light" or "dark" at the application level.

this page, http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

shows an app that

  1. defines a style, say "light.enabled"

    #000000

  2. defines an attribute reference, say "enabled"

  3. defines a style (theme) item like,

    @style/light.enabled

  4. uses the attr to define the style in the view,

this is what i want, but it doesn't work for me. the only diff is that i'm using an appwidget. the author sets the theme on the activity. the only place i can set it is Context.setTheme(), and at the "application" tag in the manifest. neither of which seems to make a difference.


Solution

  • You can't apply themes to app widgets. You'll just need to have different XML that uses different styles.

    Also, it is confusing when you talk about light vs. dark and enabled vs. disabled as similar things. They are very different in the platform.

    For example here is a color that changes based on state:
    https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/color/primary_text_dark.xml

    And here is a drawable that changes based on state:
    https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/drawable/btn_default.xml