Android Vector Drawable With Examples

In this tutorial, we’ll be discussing Android Vector Drawable. Furthermore, we’ll be implementing them in our Android Application.

Android Vector Drawable

Often we use PNG as our drawable images. In order for the PNG images to work for different screen sizes, we create multiple PNG assets with different sizes and densities. Subsequently, PNG images take up extra space and lead to large APK sizes of the Android Apps.

This is where Vector Drawable comes to our rescue! They are your replacement for PNG images.

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information.

They can be scaled according to the screen size without loss in quality. They are rendered quickly onto the screen too. VectorDrawable are an XML file.

You can add a new Vector Asset in your drawable folder using New | Vector Asset.


Thus we can create Vector drawables of Material Design icons. The code for the VectorDrawable looks like this:


They are set in the vector tag. android:viewportWidth and android:viewportHeight are used in setting the width and height of the drawable bounds. Within these dimensions, the vector drawable is drawn on the canvas.

path is the tag that creates the drawable. Inside the path we create lines, curves, arcs and set the border, background color. We do so path commands in the pathData.

Vector Drawables were introduced since Android Lollipop and higher but thanks to backward compatibility, they are compatible with earlier versions too.

Creating Path for Vector Assets

The path commands consist of an alphabet followed by coordinates. Imagine creating paths as doing a painting. Uppercase alphabets represent absolute position. Lowercase represent relative position.

  • Mmoveto command. It’s a way of saying move your pencil to the given coordinate on the view. Example M 100 100 moves an imaginary pencil to the 100, 100 coordinate on the canvas.
  • Lline to command. Is used to draw a line from the current position to the position specified.
    Example : M 100 100, L 120 100 draws a horizontal line.
  • z – This is used to close a path. It draws a line from the last position to the first position. Example M 100 100 L 120 100 L 120 120 L 100 120 Z creates a square.
  • C – This is used to create a bezier curve. We need to specify three sets of coordinates after this. The first and second would the two control points between the initial point and the end point.
  • A – is used to draw an arc. After a you need to specify the following format: (rx ry x-axis-rotation large-arc-flag sweep-flag x y). rx ry and x axis are the two radii specified.

We can specify more than one paths in a vector by setting the path tags inside a group tag.

Let’s create some random interesting Vector Drawable in our Android Application using the above knowledge.

Android Vector Drawable Example Project Structure


Android Vector Drawable Code

The code for the activity_main.xml layout is given below:

Let’s look at each of the Vector Drawables implementation one a time.


In the above code, we’ve created two paths. Each a right-angled triangle.

Setting this on our LinearLayout would make the background look like:



In the above code, we’re trying to create a W symbol using lines.

Note: Instead of a separate path, we can merge all in a single path command. Though you can use multiple paths for getting a clear understanding of what each path does.

Finally, our application’s output looks like this:

This brings an end to this tutorial. You can download the final Android VectorDrawable project from the link below:

By admin

Leave a Reply

%d bloggers like this: