72

I have this problem, see the trace stack:

E/AndroidRuntime(2410): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: <bitmap> requires a valid src attribute

My xml file looks like:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/btn_1"/>
    </item>
</layer-list>

btn_1 is another xml file in drawable resources When i'm using an image(*.png) instead of xml drawable it's ok.

Can I use a drawable resource as src to bitmap? Just in case here is my btn_1.xml file. It doesn't work even if btn_1 file have no items.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/btn_arrow_bg_red"/>
<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/btn_arrow_white" />
</item>
</layer-list>
Rudolf Real
  • 1,948
  • 23
  • 27
Alexander Gubarets
  • 743
  • 1
  • 5
  • 7
  • thx. but hmm. what I should do? i need to place one drawable xml resource above another, but my resource is scaling... what i must do to avoid scaling an xml drawable resource? and have no gravity parameter... – Alexander Gubarets Mar 20 '12 at 15:10
  • I got this error (` requires a valid src attribute`) when I use **vector image** for bitmap. My solution is - Use @Santiago Rivas answer - Or user another image (not vector image) – Linh Aug 15 '18 at 03:44

4 Answers4

126

To avoid error: Binary XML file line #XXX: requires a valid src attribute

inside a layer-list, use:

<item android:drawable="@drawable/image" />

instead of:

<item>
  <bitmap android:src="@drawable/image"/>
</item>
Santiago Rivas
  • 1,285
  • 2
  • 8
  • 3
59

You cant have an xml drawable as source for bitmap. Because for example if it was possible, then it could mistakenly create a black-hole by calling xml to itself.

Lets suppose, you have an xml drawable A which has a bitmap whos source is drawable B. But in drawable B, it has a bitmap whos source is drawable A. This will create a circular loop which cant be resolved. That is why you need to provide an image as a source for bitmap to avoid any confusion

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Thanks for this Waqas, I have the exact same question (but didn't merge as this is marked as answered) - but I'm asking for the solution: http://stackoverflow.com/questions/10055892/preventing-shapes-scaling-in-a-layeredlist-with-or-without-using-bitmap – ataulm Apr 07 '12 at 16:42
  • 24
    @waqaslam, your answer is correct: you can't have an XML drawable as the source for a bitmap tag. What I find confusing is your explanation: the circular reference can happen in any context when referencing XML resources. It seems to me it's a design constraint in the framework – Jose_GD Mar 05 '14 at 14:45
  • The second part is just irrelevant – Farid Feb 15 '21 at 07:20
  • Why this code works on API 18? – Seyyed Mar 25 '22 at 12:10
15

Minimum SDK: API 23

drawable/yourDrawable.xml

In the following code I have a vector as a background as the first item and in the second item centered logo with a custom size above the background.

<layer-list
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/background_in_svg_1">

    </item>
    <item
        android:width="200dp"
        android:height="60dp"
        android:gravity="center_vertical|center_horizontal"
        android:drawable="@drawable/logo_in_svg_2"/>
</layer-list>

Note: The cumulative distribution as of today from API 23 is 84.9%. So you might consider an alternative for lower APIs

GL

Source

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
3

If you want to use selector for changing tint attribute, create selector for color only.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_pressed="true" />
    <item android:color="@color/white" android:state_activated="true" />
    <item android:color="@color/green" />
</selector>

And then use it as color in android:tint

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:tint="@color/tint_menu_item"
    android:src="@drawable/ic_menu_home" />
aphanite
  • 59
  • 1
  • 4