Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use textColorPrimary as background color in a style?

Tags:

android

styles

I want to create a style which uses the android textColorPrimary as a background color. I tried the following which does not work, the result is my layout not beeing displayed at all.

<style name="horizontalLine">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/textColorPrimary</item>
</style>

How do I use textColorPrimary as background color in a style?

like image 615
johannes Avatar asked Jul 24 '13 19:07

johannes


1 Answers

This syntax seems to work for me, when trying to use attributes:

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="?android:textColorPrimary"
        android:text="Hello"/>

(or)

<style name="MyStyle">
   <item name="android:textColor">?android:textColorPrimary</item>
</style>

I can change the app theme from Holo to Holo.Light and the text color will change automatically to fit.

It doesn't work when I set it as a background of a View though - Android will crash complaining that the drawable referenced is a state list that does not specify drawables (it is a state list of colors).

    Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: <item> tag requires a 'drawable' attribute or child tag defining a drawable
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
    at android.content.res.Resources.loadDrawable(Resources.java:1950)
    ... 39 more

I am using HoloEverywhere which lets me reference the resources directly, but you should get a similar problem in the native case. I don't think the primary, non-selected, non-activated (etc.) color used as a component in the state list xml is exposed through an attribute.

In any case, the text color used is dependent on the theme that you, the app developer, chooses. If you choose to use the Holo (dark) theme then your text will be a light color, and the user won't be able to affect this. You don't need to make the your line color dynamic for your app.

like image 170
antonyt Avatar answered Sep 19 '22 20:09

antonyt