52

I'm using the Design Support Library 23.2. I've added these lines in my build.gradle as my Gradle Plugin is version 1.5

defaultConfig {
        applicationId "com.abc.xyz"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        generatedDensities = []
    } 

      aaptOptions {  
        additionalParameters "--no-version-vectors"  
      }  
     }

as it's specified in here

But I can't use the srcCompat attribute for my imageview.

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

where @drawable/wallpaper is a vector resource file

<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="M4,4h7V2H4c-1.1,0 -2,0.9 -2,2v7h2V4zm6,9l-4,5h12l-3,-4 -2.03,2.71L10,13zm7,-4.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S14,7.67 14,8.5s0.67,1.5 1.5,1.5S17,9.33 17,8.5zM20,2h-7v2h7v7h2V4c0,-1.1 -0.9,-2 -2,-2zm0,18h-7v2h7c1.1,0 2,-0.9 2,-2v-7h-2v7zM4,13H2v7c0,1.1 0.9,2 2,2h7v-2H4v-7z"/>

It says

Error:(14) No resource identifier found for attribute 'srcCompat' in package 'android'

My Gradle version is 1.5. How can I use srcCompat?

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Neeraj
  • 1,769
  • 3
  • 24
  • 41
  • None of the solutions worked for me. It turns out, the answer is to use androidx.appcompat.widget.AppCompatImageView (ImageView and android.support.v7.widget.AppCompatImageView did NOT work) – ONE Dec 06 '20 at 07:44

12 Answers12

127

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
29

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.

Micro
  • 10,303
  • 14
  • 82
  • 120
13

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.

Artem Garkusha
  • 331
  • 5
  • 16
  • I have all three of those .. the `xmlns` ones properly on the root node for the activity's layout. I get a red underline on the `app:srcCompat` line with the warning:_Unexpected namespace prefix 'app' found for tag ImageButton_ and if I ignore it and install on a device that needs API 19 I get: `Unable to start activity ComponentInfo{xyz.selfenrichment.robertotomas.homeroast/xyz.selfenrichment.robertotomas.homeroast.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class ImageButton` – roberto tomás Apr 15 '16 at 19:48
  • What should a person who is using Eclipse, rather than Android Studio, do? – Solace Jul 16 '16 at 11:24
6

I just changed:

app:srcCompat="@drawable/wallpaper"

to

android:src="@drawable/wallpaper"

This worked for me.

Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110
vanquan223
  • 139
  • 2
  • 5
3

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

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
3

there is multiple things which you have to care about First of all use:

defaultConfig {
            vectorDrawables.useSupportLibrary = true
        }

or

vectorDrawables {
        useSupportLibrary = true
    }

and Secondly use

 <android.support.v7.widget.AppCompatImageView
                android:id="@+id/changeLanguages"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:adjustViewBounds="true"
                android:background="@color/transparent"
                android:scaleType="fitCenter"
                app:srcCompat="@drawable/ic_transfer" />

Always use AppCompatImageView or Button because vector image is not supported by Simple ImageView

if all the above methods not worked then use

public class App extends Application {

  @Override public void onCreate() {
    super.onCreate();

    // Make sure we use vector drawables
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  }
}

If you are using Activity then extend your Activity with AppCompatActivty

public final class MainActivity extends AppCompatActivity {    
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
  }
}
1

You are using android:srcCompat. It should instead by app:srcCompat as it is an attribute defined within AppCompat, not within the android: namespace.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
1

I know am late but It might help someone else. I was facing the same issue even though I was using app:SrcCompat.After spending a lot of time I figured it was due to drawables. My issue simply resolved by moving all drawables from drawable(v21) to simple drawable.

jj1
  • 41
  • 1
  • 4
0

update your gradle plugin to 2.0+

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

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

as per google developer's blog guid lines

http://android-developers.blogspot.in/2016/02/android-support-library-232.html

ErShani
  • 392
  • 2
  • 9
0

From, Android developer site

This library is now a dependency of the v7 AppCompat library, allowing developers and AppCompat to easily use vector drawables.

To use VectorDrawableCompat within an ImageButton or ImageView, use the app:srcCompat XML attribute or setImageResource() method.

To keep referencing attribute IDs on API level 20 or lower, add the following appt flag to your build,gradle file:

If you are building with Android Plugin for Gradle 1.5.0 or lower, add the following to your build.gradle file:

android {
  defaultConfig {
    // Stops the Gradle’s automatic rasterization of vectors
    generatedDensities = []
  }
   // Flag that tells aapt to keep the attribute ids
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

If you are building with Android Plugin for Gradle 2.0.0 or higher, add the following to your build.gradle file:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

you need to add

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

to your layout for it to work. My "app:srcCompat" was highlighted in red until i added it.

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
0

Make sure the picture you are trying to use is PNG format.

Copy the picture.

Go to mipmap on the 1.Project TAB in the android hierarchy.

Click on mipmap once then(On windows ctrl + v ), when you name the photo make sure its lower case letters and no symbols.

The photo should be useable.

Set your image view, (you may have to use a filler color temporarily).

Click on the imageView and then click the srcCompact (the one without the paintbrush and type on @mipmap/"Your pictures name" (This step is in the attributes tab)

That should work!

If you need a Content Description I just used @String/StringPicture

Tyler
  • 1