Abhishek Bansal
Abhishek Bansal

Reputation: 5345

Theme attributes in color selector for API <22

I have a few color selectors like this

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorAccent" android:state_checked="true"/>
    <item android:color="@android:color/white" android:state_checked="false"/>
</selector>

These color selectors work fine for API 23 and above. For API 22 ?attr/colorAccent doesn't seem to work. I always see a Red color instead. this works when not used in a selector. Ideally it is suppose to work for API 21+

Upvotes: 4

Views: 245

Answers (2)

Dan Zol
Dan Zol

Reputation: 1

You can create selector programmatically. Use for this ColorStateList.

val states = arrayOf(
        intArrayOf(android.R.attr.state_checked),
        intArrayOf(-android.R.attr.state_checked),
        )

val colors = intArrayOf(colorAccent, colorWhite)

val selectorColors = ColorStateList(STATES, colors)

You can get the colors through context.withStyledAttributes()

Upvotes: 0

Adib Faramarzi
Adib Faramarzi

Reputation: 4060

This is expected behavior for Pre-Lollipop, since it was added in lollipop (see here). For compatibility, you can create multiple files for your drawable for your multiple themes.

Upvotes: 0

Related Questions