app:srcCompat does not work for ImageView
Asked Answered
F

3

2

I'm adding vector drawable support to a library project, and using app:srcCompat to reference the vector drawable. The only view that appears to work is ImageButton and I'm not sure why.

Here's the relevant piece of my build.gradle

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

dependencies {
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:support-vector-drawable:24.0.0'
    compile 'com.android.support:animated-vector-drawable:24.0.0'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}

Here's my layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    >

    <ImageView
        android:id="@+id/iconActive"
        style="@style/Widget.MyCompany.Button.Icon"
        app:srcCompat="@drawable/activities"
        android:layout_marginTop="16dp"
        android:tint="@color/white"
        />

</LinearLayout>

If I simply change the ImageView to an ImageButton it works. Button also does not work.

Fosterling answered 23/6, 2016 at 23:32 Comment(0)
L
4

Maybe you could try changing ImageView to AppCompatImageView. So it becomes:

<android.support.v7.widget.AppCompatImageView
   android:id="@+id/iconActive"
   style="@style/Widget.MyCompany.Button.Icon"
   app:srcCompat="@drawable/activities"
   android:layout_marginTop="16dp"
   android:tint="@color/white"
/>
Lendlease answered 25/6, 2016 at 1:46 Comment(3)
That doesn't work either. Also, you don't have to explicitly use the AppCompat views, they are created auto-magically for you depending on the version of Android you are running, if you are including the library in your project.Fosterling
Actually, it doesn't seem to matter the version of Android.Fosterling
@ChristopherPerry If your activity doesn't extend AppCompatActivity then AppCompat views won't "auto-magically" be injected for you. https://mcmap.net/q/1330754/-app-srccompat-vector-drawable-shows-up-in-design-preview-but-doesn-39-t-show-up-in-appElisaelisabet
J
3

try this

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


<androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/img_cross"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            app:srcCompat="@drawable/cross" />
Jumper answered 17/5, 2020 at 11:31 Comment(0)
J
2

Work for me

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


    <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/img_cross"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                app:srcCompat="@drawable/cross" />
Jumper answered 17/5, 2020 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.