Using theme references in XML drawables requires API level 21
Asked Answered
S

2

9

Is there a way to make following code compatible with lower API levels:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="?android:attr/selectableItemBackground"/>

    <item android:gravity="bottom">
        <shape android:shape="rectangle">
            <size android:height="1px"/>
            <solid android:color="#ccc"/>
        </shape>
    </item>

</layer-list>

The part: android:drawable="?android:attr/selectableItemBackground" produces this message:

Using theme references in XML drawables requires API level 21 (current min is 15)

Smriti answered 14/1, 2017 at 14:1 Comment(0)
A
-1

use android:drawable="?attr/selectableItemBackground"

Azpurua answered 14/1, 2017 at 14:7 Comment(1)
This removes the design-time message but produces the run-time error: <item> tag requires a 'drawable' attribute or child tag defining a drawable on older APIs (I tested it on API 17 and 19).Smriti
J
-1

Create an attars.xml file under the values folder.

In the attars.xml file, create an item for selectableItemBackground:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="selectableItemBackground" format="reference" />
</resources>

In your styles.xml define this attribute with your drawable:

<item name="selectableItemBackground">@drawable/yourDrawable</item>

And then in your drawable file, reference the attribute:

<item android:drawable="?attr/selectableItemBackground"/>
Jannette answered 6/4, 2018 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.