2

I read about app:srcCompat on stackoverflow and also from other resources that the main purpose of this attribute is to support for vector-drawable. But I got confused when android:src is also working fine with vector-drawable.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M10.18,9"/>
    <path
        android:fillColor="#FF000000"
        android:pathData="M21,16v-2l-8,-5V3.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S10,2.67 10,3.5V9l-8,5v2l8,-2.5V19l-2,1.5V22l3.5,-1 3.5,1v-1.5L13,19v-5.5l8,2.5z"/>
</vector>

I saved the above code in abc.xml file in drawable directory, then I use it with android:src

<ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@drawable/abc" />

It is working fine. Then where app:srcCompat is helpful or is there any difference between them? or I understand it wrong?

Community
  • 1
  • 1
Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • What is your `minSdkVersion` and `targetSdkVersion`? What version of Android is on your test device or emulator? – Code-Apprentice Mar 25 '17 at 11:14
  • Emulator's android version is 7.0 – Asif Mushtaq Mar 25 '17 at 11:15
  • This is why you do not see a difference. Vector drawables were introduced with Nougat. – Code-Apprentice Mar 25 '17 at 11:16
  • Ooh great let me confirm you mean app:srcCompat is for older android versions? because older verion's android:src was not working with vector? – Asif Mushtaq Mar 25 '17 at 11:19
  • @Code-Apprentice can you help me to find the difference? I mean which android version should I use to check the difference? help me to choose the following versions. which minSdkVersion? which targetSdkVersion? which Emulator's Android Version? which Project's Version? – Asif Mushtaq Mar 25 '17 at 11:21
  • Sorry, VectorDrawable was introduced in Lollipop API 21. If you create an AVD for an API version prior to this, you should see the difference. (What @riggaroo said) – Code-Apprentice Mar 25 '17 at 11:22

1 Answers1

6

Yes, there is a difference. There is an article here about it. Basically app:srcCompat will work on older APIs when loading up vector drawables. You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop, you are probably running the android:src on Lollipop or higher.

riggaroo
  • 2,584
  • 20
  • 34
  • Great thanks. Can you answer on my other question? http://stackoverflow.com/q/43014279/2769917 I really struggling to find the purpose of android:ems. – Asif Mushtaq Mar 25 '17 at 11:26