David David - 10 days ago 5
Android Question

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
srcCompat
Here'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.

Answer

Original Answer

Use android.support.v7.widget.AppCompatImageView instead of ImageView like this:

   <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" />

See the AppCompatImageView docs here and appc:srcCompat here.

Also, make sure to do the following:

Setup your build.gradle

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

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);
  }
}

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);
  }
}