248

I have found a myriad of libraries in order to use SVG images in Android and avoid the frustrating creation of different resolutions and dropping files for each resolution. This becomes very annoying when the app has many icons or images.

What would be a step-by-step process of the simplest-to-use library for using SVG images in Android?

Also I use Android Studio and Illustrator for generating my icons and images.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • Possible duplicate of [What are best practices for using SVG icons on Android?](http://stackoverflow.com/questions/9647770/what-are-best-practices-for-using-svg-icons-on-android) – serv-inc May 11 '17 at 07:14
  • Short video on how to import svg in android studio: https://www.youtube.com/watch?v=8e3I-PYJNHg – Zohab Ali Oct 07 '18 at 06:01

10 Answers10

524

First, you need to import SVG files by the following simple steps.

1. Right click on your project's drawable folder (app/res/drawable)
2. Click New
3. Select Vector Asset

If the image is available on your computer, select the local svg file.

After that, select the image path. An option to change the size of the image is also available on the right side of the dialog if you want to. In this way, the SVG image is imported into your project.

After that, for using this image, use the same procedure:

@drawable/ic_image_name
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Pallavi Jain
  • 5,319
  • 2
  • 11
  • 3
  • 9
    Short and useful answer, without headache when you do import external svg files to project. – CodeToLife May 01 '17 at 12:01
  • 5
    the `Next` button is always disabled. any idea why ? – mrid Dec 29 '17 at 07:17
  • So, how do we change the color of the svg? – Oniya Daniel Sep 27 '18 at 13:35
  • @mrid if it is something you have to do just once you may use Inkscape or vectr.com (for free) or Adobe Illustrator etc.. – Sir Von Berker Mar 17 '20 at 17:39
  • There is a bug in Android Studio that does so that sometimes you will need to open `res/drawable/file.xml` (a file that's created automatically when you import the SVG file) and replace every occurrence of `−` with `-`. If you don't do that, your app will crash. See my bug report here: https://issuetracker.google.com/issues/195687730 – Donald Duck Aug 07 '21 at 10:29
20

UPDATE: DO NOT use this old answer. Better use Pallavi Jain's answer

I found svg-android to be quite easy to use, so the step-by-step instructions are here:

  1. Download the library from: https://code.google.com/p/svg-android/downloads/list. The latest version at the moment of writing this is: svg-android-1.1.jar

  2. Put the JAR file in the lib directory.

  3. Save your *.svg file in the res/drawable directory (in Illustrator, it is as easy as pressing Save as and select .svg)

  4. Code the following in your activity using the SVG library:

     ImageView imageView = (ImageView) findViewById(R.id.imgView);
     SVG svg = SVGParser.getSVGFromResource(getResources(), R.drawable.example);
     // The following is needed because of image accelaration in some devices, such as Samsung
     imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
     imageView.setImageDrawable(svg.createPictureDrawable());
    

You can reduce the boilerplate code like this:

Very easily I made a simple class to contain the past code and reduce the boilerplate code, like this:

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

public class SvgImage {

    private static ImageView imageView;
    private Activity activity;
    private SVG svg;
    private int xmlLayoutId;
    private int drawableId;


    public SvgImage(Activity activity, int layoutId, int drawableId) {
        imageView = (ImageView) activity.findViewById(layoutId);
        svg = SVGParser.getSVGFromResource(activity.getResources(), drawableId);
        // Needed because of image acceleration in some devices, such as Samsung
        imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        imageView.setImageDrawable(svg.createPictureDrawable());
    }
}

Now I can call it like this in activity:

    SvgImage rainSVG = new SvgImage(MainActivity.this, R.id.rainImageView, R.drawable.rain);
    SvgImage thunderSVG = new SvgImage(MainActivity.this, R.id.thunderImageView, R.drawable.thunder);
    SvgImage oceanSVG = new SvgImage(MainActivity.this, R.id.oceanImageView, R.drawable.ocean);
    SvgImage fireSVG = new SvgImage(MainActivity.this, R.id.fireImageView, R.drawable.fire);
    SvgImage windSVG = new SvgImage(MainActivity.this, R.id.windImageView,R.drawable.wind);
    SvgImage universeSVG = new SvgImage(MainActivity.this, R.id.universeImageView,R.drawable.universe);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • 2
    When I try to add SVG files I am getting error saying, Error: The file name must end with .xml or .png – Sujay U N Aug 12 '17 at 16:41
  • 1
    Why is `imageView` `static`? I see a hugh red bug flag here. `SvgImage rainSVG = new SvgImage(MainActivity.this, R.id.rainImageView, R.drawable.rain); SvgImage thunderSVG = new SvgImage(MainActivity.this, R.id.thunderImageView, R.drawable.thunder);` rainSVG does reference contain thunderImageView – DSchmidt Aug 25 '17 at 12:13
  • @DSchmidt that is just a pointer to the imageview. If it was my code I would make the whole class static with one method called LoadSVG. and as you can see there is no point of loading data to private variables that can not be accessed. – Nasreddine Galfout Jun 30 '20 at 18:53
  • @SujayUN use assets folder. – Mahmut K. Sep 28 '22 at 08:54
16

Android Studio supports SVG from 1.4 onwards

Here is a video on how to import.

Pradeep Mahdevu
  • 7,613
  • 2
  • 31
  • 29
15

Try this:

Enter image description here

The next step:

Enter image description here

And now edit the image or icon name and save it:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sandeep Pareek
  • 1,636
  • 19
  • 21
  • 1
    Step by step i like it, save my life! – Chris Ho Feb 24 '22 at 01:21
  • This is missing a whole lot. What application is this? Android Studio? In what context and how is *"Vector Asset"* being invoked (adding it? - adding it to what?)? A context menu? Where and what? Why is *"Vector Asset"* being invoked? What is the idea? What is the general idea? Importing SVG files into Android Studio? Something else? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/67056210/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 27 '22 at 00:53
  • it's only for limited SVG attributes, I guess author was talking about using SVG in Android without converting it to limited Vector Drawable. Coil can do it - https://coil-kt.github.io/coil/svgs/ for example if you want to use SVG with shadows and so on – user924 May 30 '23 at 08:12
7

Rather than adding libraries which increases your APK file size, I will suggest you to convert SVG to drawable using Android SVG to VectorDrawable.

And add vectorDrawables.useSupportLibrary = true in Gradle.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sanny Nagveker
  • 418
  • 3
  • 7
6

1. You need to convert SVG to XML to use in an Android project.

1.1 You can do this with Android SVG to VectorDrawable, but it does not support all the features of SVG, like some gradients.

1.2 You can convert via Android Studio, but it might use some features that only supports API 24 and higher that cause crashes your app in older devices.

And add vectorDrawables.useSupportLibrary = true in the Gradle file and use it like this:

<android.support.v7.widget.AppCompatImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/ic_item1" />

2. Use this library SVG-Android that supports these features

Add this code in the application class:

public void onCreate() {
    SVGLoader.load(this)
}

And use the SVG like this:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_android_red"/>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sajad abbasi
  • 1,988
  • 2
  • 22
  • 43
4
  1. Right-click on the drawable directory, press New, and then go to Vector assets
  2. Change the asset type from clip art to local
  3. Browse your file
  4. Input the size
  5. Then click Next and then Done

Your usable SVG image will be generated in the drawable directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

You can use the Coil library to load an SVG file. Just add these lines in file build.gradle:

// ... Coil (https://github.com/coil-kt/coil)
implementation("io.coil-kt:coil:0.12.0")
implementation("io.coil-kt:coil-svg:0.12.0")

Then add an extension function:

fun AppCompatImageView.loadSvg(url: String) {
    val imageLoader = ImageLoader.Builder(this.context)
        .componentRegistry { add(SvgDecoder(this@loadSvg.context)) }
        .build()

    val request = ImageRequest.Builder(this.context)
        .crossfade(true)
        .crossfade(500)
        .data(url)
        .target(this)
        .build()

    imageLoader.enqueue(request)
}

Then call this method in your activity or fragment:

your_image_view.loadSvg("your_file_name.svg")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aminul Haque Aome
  • 2,261
  • 21
  • 34
3

Try the SVG2VectorDrawable Plugin. Go to PreferencesPluginsBrowse Plugins and install SVG2VectorDrawable. It is great for converting svg files to vector drawable.

Once you have installed you will find an icon for this in the toolbar section just to the right of the help (?) icon.

Rahul Thakur
  • 824
  • 1
  • 12
  • 27
-1

Load SVGs using the Coil library. Note that this library is built upon Kotlin Coroutines.

Import the extension library:

implementation("io.coil-kt:coil-svg:2.1.0")

And add the decoder to your component registry when constructing your

ImageLoader:
val imageLoader = ImageLoader.Builder(context)
    .components {
        add(SvgDecoder.Factory())
    }
    .build()

That's it!

Click here for more information.

Mattia Ferigutti
  • 2,608
  • 1
  • 18
  • 22