Setting a color based on theme [duplicate]
Asked Answered
C

1

16

I have an accent color that I use through out my layouts. I have it defined as a style that I apply to a TextView.

My application lets the user choose between a dark theme and a light theme. I would like to tweak the accent color based on the selected theme.

  • You can't define a color in a theme
  • You can't define a state list color based on theme
  • You can't separate resources based on theme (as you can for version or screen size)
  • You can't update the resource color from code (??? not 100% on this ???)

How should I control my accent color based on the selected theme?

The dev guide gives an example of a theme using a custom color, which is close to what I want. I need to be able to change the color at runtime. I know I could go into my code and the X places where this style is being used I could ferrit out the component and set the color that way. But I would think I could accomplish this with some combination of style/theme.

Thanks

Carpathoukraine answered 2/2, 2012 at 18:15 Comment(0)
C
0

The way I resolved this was by creating subclasses of the android.widget.TextView. I created a TextViewAccented and a TextViewWarning. I use these TextViews in my XML where I want the accent or warning color. I remove any color references from the XML.

The subclasses contain the three default constructors. All three constructors call a private method called setColor(). setColor() then inspects the selected theme and can use the desired shade based on the currently selected theme.

It's a fairly clean mechanism, but it's hard to believe that I can't code this in a theme. That seems exactly what a theme should be for.

Carpathoukraine answered 7/2, 2012 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.