Android layer-list : bitmap issue with app:srcCompat
Asked Answered
C

3

28

After updating to Studio 2.3 when i tried to create a layer list with a vector drawable it prompt to use app:srcCompat instead of android:src in bitmap.

can any one help me to add vector drawable to layer list ?

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@color/colorWhite" />

<item>
    <bitmap
        android:gravity="center"
        app:srcCompat="@drawable/login">
    </bitmap>

</item>

</layer-list>

Already added

vectorDrawables.useSupportLibrary = true
Clodhopper answered 9/3, 2017 at 9:26 Comment(5)
Any luck with this?Platon
No. nothing so farClodhopper
ok. I'm going to try something. let me know if you come across something as well.Platon
I'm having exact same problem. Still no joy with this?Epitome
reported as bug issuetracker.google.com/issues/123822220Buschi
V
22

I found a solution for your problem

Modify second item to this:

<item android:gravity="center" android:drawable="@drawable/login"/>

UPDATE

this will cause crash on API < 21. There is not any better way.

Vance answered 8/4, 2017 at 6:53 Comment(4)
Has anyone found a solution that doesn't make the app crash for API < 21?Cacodyl
on some Android versions vector drawable looks ugly (becomes distorted)Didier
@Didier Can you say which devices? Or which Api versions?Vance
You can add a png for API < 21 this will lead to distorted images though, but it doesnt crash anymore, by now you might considering rasing the minSDK to 21 (targeting 85% of Android phones as of now)Supposition
H
6

I managed to do it the following way:

<item
    android:gravity="center"
    android:drawable="@drawable/login" />
Hulking answered 26/9, 2018 at 20:38 Comment(1)
error: attribute drawable (aka app.name:drawable) not foundLyontine
G
-4

Instead of doing :

<item>
    <bitmap
        android:gravity="center"
        app:srcCompat="@drawable/login">
    </bitmap>
</item>

You need to declare the background as follow:

<item
    android:gravity="center"
    app:srcCompat="@drawable/login"/>

Also, make sure that the element app schema is presented:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    ....
</layer-list>
Gastrovascular answered 11/12, 2017 at 13:47 Comment(5)
I think <item> android:gravity="center" app:srcCompat="@drawable/login" </item> is not valid, did you mean <item android:gravity="center" app:srcCompat="@drawable/login"> </item>?Cacodyl
this solution did not work for me for whatever reason. Crashes with: <item> tag requires a 'drawable' attribute or child tag defining a drawableJohore
What a random answer! @Gastrovascular Did you even test that yourself?Didier
@Gastrovascular well... app:srcCompat="@drawable/login" will never work, it's only for ImageViewDidier
but we can just use android:drawable="@drawable/login" in item (it even works for vector drawables)Didier

© 2022 - 2024 — McMap. All rights reserved.