Svg Support on Android

SVG support on Android

The most complete answer is this:

  • The Android 2.x default browser does not natively support SVG.
  • The Android 3+ default browsers DO support SVG.

To add SVG support to 2.x versions of the platform, you have two basic choices:

  1. Install a more capable browser (like Firefox or Opera Mobile - both support SVG)
  2. Use a JavaScript polyfill that can parse SVG and render it to an HTML5 canvas

The first option is okay if you're just trying to make SVG work for personal uses or a limited (controllable) set of users. It's not a great option if you want to use SVG while targeting a large, uncontrolled user base.

In the later case, you want to use a polyfill. There are many JavaScript libraries available today that can prase SVG and render to a canvas. Two examples are:

  • canvg
  • fabric.js

Using a polyfill, you can render your SVG in a canavs on all versions of Android 2.x.

For a more complete example of this approach, you can refer to this blog post that discusses the use of the canvg polyfill for making Kendo UI DataViz charts (SVG-based) work on Android 2.x. Hope that helps!

What is the easiest way to use SVG images in Android?

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 in your computer then select the local svg file.

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

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

@drawable/yourimagename

When i use .svg file ERROR @image is not supported in Android Studio

The Android Studio SVG importer only supports a subset of SVG. Remember that it is converting the SVG to a VectorDrawable. So only things that work in a VectorDrawable can be imported from the SVG. Basically that means you must stick to just the vector shapes - rectangles, ellipses, paths etc

See: Which SVG elements are supported by Android studio and which are not?

If your SVG only contains PNG images, then there is no point in using an SVG anyway. Just import your PNGs to your project and use an ImageView to display them.

If you really need to display an SVG. Then use a library that properly supports SVGs. Such as my one: AndroidSVG.

Does android have *native* support SVG image as drawable resources (icons)?

The answer has changed. Since version 21, Lollipop the operating system has had support for VectorDrawable. This is added to by a support library that allows you to use VectorDrawable well back into previous versions. These include support for importing most of the important SVG vector commands and having Android Studio automatically convert them to a VectorDrawable.

Use Vector Image Assets for all versions. -- Also even in older versions it's entirely possible to do fonts. Basically you give it a font asset then load up a specific letter of the font as an icon. Since fonts are glyphs and vector glyphs at that, you can very much get vector icons pretty far back.

https://fortawesome.github.io/Font-Awesome/icons/

What are best practices for using SVG icons on Android?

For Android older than Lollipop, your best practice for SVG on Android is going to be to use a tool to convert your SVG to PNG at the size(s) you're interested in. Existing SVG support for Android is not comprehensive of what you're likely to find in an SVG file, and even if it were, the support is not built into the OS so using them directly for icons is definitely out.

Beginning with Lollipop (API 21) see What are best practices for using SVG icons on Android?. Thanks to @MarkWhitaker @AustynMahoney for pointing this out.

SVG support on smart-phone browsers

Information about this topic really is rare (but that's nothing new when we talk about SVG, unfortunately...)

There are a few test results from the Tiny Test Suite available (end of 2008):
http://www.w3.org/Graphics/SVG/1.2/Tiny/ImpReport.html
BitFlash, eSVG, Ikivo and Motorola SVG are of special interest here. Opera Mobile is not covered by this test but it uses Ikivo as far as I know.

And there is a pretty current discussion here:
http://tech.dir.groups.yahoo.com/group/svg-developers/message/63147
Until now the results of this discussion are rather poor, but maybe you can join in there...

However, considering the pain (native) SVG development causes with "desktop browsers" I would not really recommend it for mobile browsers unless you have a very specific target group (with SVG enabled devices) or other serious reasons to use SVG.

I'm sorry that I couldn't contribute more, maybe it was a little help anyway.

Android Studio does not import all parts of an SVG Vector

The version of the SVG that is displayed in the converter window is rendered by a built-in SVG renderer in Android Studio. It's a preview of the SVG. It does not necessarily reflect what the output of the converter will be.

So the bug you are seeing is with the built-in SVG renderer.

VectorDrawables don't really support gradient fills. At least the converter doesn't support them. So even if the displayed SVG were perfect, the generated VectorDrawable won't include the gradient circles anyway.

So you have a few alternative approaches:

  1. Change your circles to solid fill and then convert to VectorDrawables.
  2. Like #1, but add gradients by using predefined gradient definitions.
  3. Use an actual SVG rendering library (like AndroidSVG) in your app.
  4. Switch to using a bitmap background (ie PNG)
  5. Draw the background yourself using Canvas methods.

Personally, I would go with #3.



Related Topics



Leave a reply



Submit