views:

37

answers:

2

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.

+2  A: 

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

which gives an excellent example of applying multiple themes dynamically. i can't seem to get it to work with an app widget, but that's a different issue.

farble1670
A: 

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.

  • Light and dark are actual "themes" as the platform defines it, which is a set of default values for resource attributes, rooted off of android:style/Theme. These are changed with android:theme in the manifest or setTheme() in the API.

  • enabled and disabled are states. They are used with StateListDrawable (via tag in drawable/) or ColorStateList (via tag in color/) to pick a drawable/color based on the enabled state.

For example here is a color that changes based on state: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/res/res/color/primary_text_dark.xml

And here is a drawable that changes based on state: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/res/res/drawable/btn_default.xml

hackbod
thanks. okay, use "label" (text color) and "value" (text color) if that helps. my point was to try and give an abstract example not actually represent enabled and disabled.
farble1670