91

I'm using support library to show vector images on android kitkat. When I test my app on emulater I don't see any of these images. I made a separate layout for android lollipop and above and it workd perfectly (I think because I'm using src attribute instead of srcCompatHere's the code where I'm usign support library

<LinearLayout android:layout_alignParentBottom="true"
android:id="@+id/lake_detail"
android:background="@drawable/my_fishing_plan_footer_line"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="90dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
            android:layout_marginRight="3dp"
            android:id="@+id/fire_logo"
            android:layout_width="20sp"
            android:layout_height="20sp">

            <ImageView
                android:tint="#d74313"
                app:srcCompat="@drawable/circle_icon"
                android:layout_width="30sp"
                android:layout_height="30sp" />

            <ImageView
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                app:srcCompat="@drawable/lauzaviete"
                android:layout_width="25dp"
                android:layout_height="25dp" />

        </RelativeLayout>

and it's strange because I see the images on android studio preview window.

David
  • 3,055
  • 4
  • 30
  • 73

8 Answers8

209

Original Answer

Use android.support.v7.widget.AppCompatImageView instead of ImageView in your layout, like this:

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

  <android.support.v7.widget.AppCompatImageView
    android:tint="#d74313"
    app:srcCompat="@drawable/circle_icon"
    android:layout_width="30sp"
    android:layout_height="30sp" />

  <android.support.v7.widget.AppCompatImageView
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    app:srcCompat="@drawable/lauzaviete"
    android:layout_width="25dp"
    android:layout_height="25dp" />
</LinearLayout>

See the AppCompatImageView docs here and app:srcCompat here.

Also, make sure to do the following:

Setup your build.gradle

android {
  defaultConfig {
    vectorDrawables {
      useSupportLibrary = true
    }
  }
}

Docs: https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html#com.android.build.gradle.internal.dsl.VectorDrawablesOptions:useSupportLibrary

Extend your Activity with AppCompatActivity

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

    setContentView(R.layout.activity_main);
  }
}

When using app:srcCompat, make sure to have the correct declarations in your layout:

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

Optional (warning: please read docs): setCompatVectorFromResourcesEnabled in your Application class

public class App extends Application {

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

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

Docs: https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setCompatVectorFromResourcesEnabled(boolean)

Elementary
  • 2,153
  • 2
  • 24
  • 35
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 2
    Why doesn't it just work with ImageView in supportLibVersion = '25.0.1'? – Etienne Lawlor Nov 24 '16 at 05:01
  • @toobsco42 got any solution for supportLibVersion = '25.0.1'?? – LOG_TAG Nov 28 '16 at 19:01
  • So my issue was that i was not extending AppCompatActivity. I was extending Activity so i wasn't using the right LayoutInflater and the ImageView was not being replaced with an AppCompatImageView automatically. – Etienne Lawlor Nov 28 '16 at 19:10
  • 1
    @toobsco42 If you look at the `ImageView` docs, https://developer.android.com/reference/android/widget/ImageView.html, does not have `srcCompat` but `AppCompatImageView` does: https://developer.android.com/reference/android/support/v7/widget/AppCompatImageView.html. – Jared Burrows Nov 29 '16 at 05:56
  • 9
    Why should I use `AppCompatImageView` in layout. The doc says: "This will automatically be used when you use ImageView in your layouts. You should only need to manually use this class when writing custom views." – Ordon Dec 23 '16 at 14:14
  • @Ordon Yes you are right. I do know when this was posted, you would get an IDE lint warning when using `ImageView`. I seem to no longer get that anymore. – Jared Burrows Dec 23 '16 at 18:48
  • 1
    That's the most complete answer ever. Thanks so much. – leoneboaventura Apr 24 '17 at 07:08
  • 4
    @JaredBurrows #JustForRecords it is not necessary to extend activity from AppCompatActivity if you use app:srcCompat attribute on android.support.v7.widget.AppCompatImageView – Ewoks May 10 '17 at 15:37
  • I was using the SVG along with the react-native-splash-screen and found that ImageView works great when `setContentView` of the SplashActivity, but does not work when `setContentView` of the `PhoneWindow` backing the `Dialog`. For that reason I had to use `android.support.v7.widget.AppCompatImageView` which worked on both Activity and Dialog. – kendavidson Jul 01 '19 at 13:17
19

I had a similar issue and after following all steps from Jared Burrows's answer the problem was not solved.

Turns out that the "app" namespace inside my layout file was set as:

xmlns:app="http://schemas.android.com/tools"

instead of:

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

After changing this the problem was fixed

Community
  • 1
  • 1
Amaro
  • 501
  • 5
  • 10
  • Could you tell me the difference between both? – Raunit Verma Feb 26 '23 at 06:14
  • 1
    These schemas are just identifiers for where some features are implemented under the hood by Android, meaning that they don't point to a document like in a URL. Different schemas are used for various purposes. In this case, the srcCompat property was something implemented as part of an initiative to provide certain features to older versions of Android, requiring a different namespace (res-auto) definition than the original. The "xmlns:tools="http://schemas.android.com/tools" namespace is dedicated to features tied to the IDE, like changing a property only at Android Studio level – Amaro Feb 27 '23 at 18:27
15

Incase anyone else runs into this problem and is using androidx, then try using androidx.appcompat.widget.AppCompatImageView

Tsepo Nkalai
  • 1,492
  • 11
  • 18
  • 3
    Why should I use AppCompatImageView in layout. The doc says: "This will automatically be used when you use ImageView in your layouts. You should only need to manually use this class when writing custom views." – S. Gissel Oct 12 '19 at 07:28
  • 1
    @S.Gissel well, I guess you might be using an updated version. At the time I tried this that was the only solution that worked for my case – Tsepo Nkalai Oct 12 '19 at 07:40
  • Thank you so much...This fixed my issue. The drawable DOES NOT SHOW when you are inflating the layout in a service and is using ```ImageView```. https://stackoverflow.com/q/68128001/11110509 Need ```AppCompatImageView``` for it to work. – DIRTY DAVE Jun 25 '21 at 09:13
5

In summary:

  1. Put vector drawable support in your module (build.gradle)

    defaultConfig {           
         vectorDrawables.useSupportLibrary = true
     }
    
  2. Instead of android:src="@drawable/icon" use app:srcCompat="@drawable/icon"

  3. Make sure your Activity extends AppCompatActivity without this step is not possible to show vector image with app:srcCompat

Gabriel Perez
  • 373
  • 6
  • 11
1

use:

android:background="@drawable/circle_icon"

instead of:

app:srcCompat="@drawable/circle_icon"
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
1

Implement and app:srcCompact and then you can use it on the ImageView

implementation 'com.android.support:appcompat-v7:28.0.0'

Make sure you implement the right version.

Then in your build.gradle set android.defaultConfig.vectorDrawables.useSupportLibrary = true

defaultConfig {

    //...

    vectorDrawables {
        useSupportLibrary true
    }
}
Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
1

The problem was with my Code as well. Solution: As Android is upgrading to Androidx Artifacts, I used Instead of Regular and it Worked!

Abhishek
  • 33
  • 7
0

Additional be sure that your vector drawables located in drawable and not in drawable-anydpi.

I end up often with problems if the graphics located in drawable-anydpi folder.

Fabi755
  • 1,495
  • 1
  • 12
  • 29