Can't use srcCompat for ImageViews in android

Don't

android:srcCompat="@drawable/wallpaper"

Do

app:srcCompat="@drawable/wallpaper"

as it srcCompat attribute is actually defined within AppCompat library.

Important you will need to add appropriate namespace for this.

xmlns:app="http://schemas.android.com/apk/res-auto"

Important

what you are getting it seems like it is just a lint error that can be ignored. I have tried and have the same error, but it is working correctly.

you can use tools:ignore="MissingPrefix" to avoid seeing this error temporarily.


Combining many answers into one answer:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/primary">


    <android.support.v7.widget.AppCompatImageView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:id="@+id/logoImageView"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/my_logo_vector"
        android:tint="@color/white"
        />

</RelativeLayout>

This worked for me. No lint errors. Use AppCompatImageView.


First (in build.gradle)

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

Second

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_traffic_black_24dp"
    tools:ignore="MissingPrefix" />

Third

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

Here we go.


I just changed:

app:srcCompat="@drawable/wallpaper"

to

android:src="@drawable/wallpaper"

This worked for me.


Call app:srcCompat instead of android:srcCompat .

Don't

android:srcCompat="@drawable/your_image"

DO

app:srcCompat="@drawable/your_image"

Finally

    <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/wallpaper"/>

My gradle version is 1.5

Upgrade your Gradle version.

Solution

Gradle Plugin 2.0+  
com.android.tools.build:gradle:2.0.0-beta2

Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices, both VectorDrawable and AnimatedVectorDrawable are now available through two new Support Libraries support-vector-drawable and support-animated-vector-drawable.

Add this

    // Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 } 

You’ll note this new attribute only exists in the version 2.0 of the Gradle Plugin. If you are using Gradle 1.5 you’ll instead use

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }  

Go Through Support Vector Drawables