VectorDrawable Usage Example

suggest change

Here’s an example vector asset which we’re actually using in AppCompat:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:tint="?attr/colorControlNormal">

    <path
        android:pathData="..."
        android:fillColor="@android:color/white"/>

</vector>

Using this drawable, an example ImageView declaration would be:

<ImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_search"/>

You can also set it at run-time:

ImageView iv = (ImageView) findViewById(...);
iv.setImageResource(R.drawable.ic_search);

The same attribute and calls work for ImageButton too. Before API 21, ImageView.setImageResource does not inflate a support-library vector. Use app:srcCompat or AppCompatResources.getDrawable. The ... path data and findViewById argument are placeholders.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents