What Is The 'Best Practice' Way of Creating an Icon Button with an Svg

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.

how can we add SVG icons to create the ribbon

You can use the following formats of icons for Ribbon controls: bmp, png and ico. Vector graphics (SVG) is not supported.

You need to convert your SVG file into one of the supported file formats. The ico images can embed images with different resolutions, so the MS Office may pick up the most appropriate one.

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

SVG icons vs. PNG icons in modern web sites

Reasons SVG may be a good choice:

  • it seamlessly supports browsers of any size, especially with css's background-size
  • you can scale them up/down will, such as to to a hover effect
  • you can embed SVGs and do real-time modifications to them with JavaScript and the DOM
  • you can style SVGs and parts of SVGs with CSS (changing colors, outlines, etc.)
  • you can generate SVGs dynamically, either on the client or server. Due to their text based nature, you don't need low-level libraries or powerful servers to create them.

Reasons PNG may be a good choice:

  • browser support
  • existing tooling for creating PNG spritesheets
  • most people have a PNG compatible editor on their computer
  • your graphics are photos or other difficult to vectorize images

Other concerns:

  • some SVG editors may store metadata in your SVGs, increasing file size and possibly unintentionally exposing data

    • e.g. when you export a PNG in Inkscape it did/does save the absolute path to this directory in the SVG when you save
    • SVG compressors may remove this, but I haven't tested it (feel free to edit if you have)

How to use an SVG file in a SvgIcon in Material-UI

<path /> is an SVG path, i.e. the internal bits of the SVG. the SvgIcon component really should be able to take a path, but it doesn't :(

instead you can create a component like https://github.com/callemall/material-ui/blob/56c113217d7d05d8bb0712771b727df81984d04b/src/svg-icons/action/home.js

with your svg source in place of the path. (I recommend minifying it a bit using https://jakearchibald.github.io/svgomg/)

Using SVG in android selector for buttons

I have the answer to my own question.
In your activity just add this static block

static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}


Related Topics



Leave a reply



Submit