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
Boban
  • 351
  • 1
  • 4
  • 7

3 Answers3

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.

DrMorteza
  • 2,067
  • 2
  • 21
  • 29
  • Has anyone found a solution that doesn't make the app crash for API < 21? – Franco Nov 24 '17 at 04:23
  • on some Android versions vector drawable looks ugly (becomes distorted) – user25 Feb 26 '19 at 19:36
  • @user25 Can you say which devices? Or which Api versions? – DrMorteza Feb 27 '19 at 19:11
  • 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) – ueen Sep 09 '19 at 20:27
6

I managed to do it the following way:

<item
    android:gravity="center"
    android:drawable="@drawable/login" />
SapuSeven
  • 1,473
  • 17
  • 30
-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>
jonalmeida
  • 1,206
  • 1
  • 9
  • 24
JoseF
  • 1,261
  • 13
  • 30
  • I think ` android:gravity="center" app:srcCompat="@drawable/login" ` is not valid, did you mean ` `? – Franco Dec 11 '17 at 19:58
  • 5
    this solution did not work for me for whatever reason. Crashes with: ` tag requires a 'drawable' attribute or child tag defining a drawable` – Lukas1 Jul 13 '18 at 11:55
  • What a random answer! @JoseF Did you even test that yourself? – user25 Feb 26 '19 at 19:34
  • @JoseF well... `app:srcCompat="@drawable/login"` will never work, it's only for `ImageView` – user25 Feb 28 '19 at 05:17
  • but we can just use `android:drawable="@drawable/login"` in `item` (it even works for vector drawables) – user25 Feb 28 '19 at 05:19