Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android vector drawable app:srcCompat not showing images

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.

like image 637
David Avatar asked Jul 06 '16 10:07

David


People also ask

What is not an advantage of using vector Drawables?

The drawback is that they might take longer to draw, etc since there's more of parsing and drawing going on than just using a bitmap. This should although be neglectable if you keep your vectors simple.

What is difference between SRC and srcCompat in Android?

From what I've gathered, seems the difference is that app:srcCompat is an attribute used when you want a vector drawable (vector images can be resized without losing image quality where png files lose image quality). Android:src would be the attribute for you if you were to go with a png drawable.

Does Android studio support SVG?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

Which of the XML files are responsible for providing vector graphics instructions in the new project of Android studio?

An animator XML file.


2 Answers

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)

like image 152
Jared Burrows Avatar answered Sep 20 '22 13:09

Jared Burrows


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

like image 38
Amaro Avatar answered Sep 18 '22 13:09

Amaro