21

I've got in attrs.xml

<resources> 
    <!-- theme specific colors -->
    <attr format="reference|color" name="foreground" />
    <attr format="reference|color" name="background" /> 
</resources>

And then in theme.xml

<style name="MyTheme" parent="android:Theme.Black">
    <item name="android:windowNoTitle">true</item>
    <item name="foreground">#0000FF</item>
    <item name="background">#00FF00</item>
</style>

I also created color selector named forground_to_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_pressed="true" android:color="?background"/> <!-- pressed -->
    <item android:state_focused="true" android:color="?background"/> <!-- focused -->
    <item android:color="?foreground"/> <!-- default -->
</selector>

Now I'd like to use it all together in TextView:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/forground_to_background" />

Unfortunately it doesn't work. Instead of having nice green blue colors I've got only one color - red. TextView is always red. When I change TextView to use "?foreground" color will change. Also when I change in colors selector from "?xxxx" to hardcoded value as "#00f" color start to work.

Where is problem? What am I doing wrong?

Edit: I believe it is duplicate of problem/bug Can a selector resource use a color defined in a style?

Edit2: Moreover when I try use this TextView in ListView application crashes. It cannot parse XML.

Community
  • 1
  • 1
Mikooos
  • 5,360
  • 5
  • 31
  • 45

3 Answers3

16

You cannot reference ?attr/ when choosing colors for a selector. What you can do, if you want per-theme colors in your selector, is create multiple selectors which reference @color/ and @drawable/, and then have a "reference" attr which associates one of the selectors with the given style.

<attr name="forground_to_background" format="reference" />

You then have to set the text color like

android:textColor="?attr/forground_to_background"

I believe the text was always red because Android was interpreting the attr's integer value as a color (red), rather than using it as a lookup for what you actually wanted.

gelakinetic
  • 423
  • 5
  • 10
8

The reason why this happens is that I have different Context. While inflating Context is aware of my theme attrs, but to the ListView adapter I passed ApplicationContext that wasn't aware of those attrs. Now I don't know why it doesn't know about them ;)

Mikooos
  • 5,360
  • 5
  • 31
  • 45
  • For more information about context go here: http://www.doubleencore.com/2013/06/context/ Different context knows different things... – Mikooos Nov 26 '13 at 11:01
  • Such a silly mistake i was banging my head to the wall. I never thought i would go wrong with such a simple mistake. You saved my day. – Ashok Varma Aug 16 '15 at 17:40
  • Had the same problem, was using application context, you saved my day! – GuilhE Mar 08 '20 at 23:11
0

Are you sure if you applying MyTheme to the activity or the textview? Another thing you can try is that instead of using the "?" operator in your forground_to_background.xml, trying using "@" instead. see if that fixes your problem

CChi
  • 3,054
  • 1
  • 20
  • 15