Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a background to a Vector Asset that I want to use as my app's icon?

Tags:

android

I used the Vector Asset to create an Icon that I want for my app. I set it in the manifest file and it works. However it has a transparent background. I would like to fill that transparent background with a solid color.

Here is the XML file:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
       android:viewportHeight="24.0"
       android:viewportWidth="24.0" 
       android:height="48dp"
       android:width="48dp" >

    <path android:fillColor="#FF000000" android:pathData="M12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1s3.1,1.39 3.1,3.1v2L8.9,8L8.9,6zM18,20L6,20L6,10h12v10z"/>
</vector>

How can I set background color to this?

like image 594
aarelovich Avatar asked Jan 04 '18 21:01

aarelovich


People also ask

How do you add a background to a vector file?

You can now create a vector background like below for yourself in less than 1 minute ! Open Vecta.io, and create a drawing. Draw a rectangle, and fill it with the background color you want. Drop your preferred shape into the drawing, then run the plugin - Scatterer to randomly distribute the shapes around your drawing!

What file type is used for the launcher icon foreground and background vectors?

xml , create alternative drawable resources in your app for backward-compatibility with Android 8.0 (API level 26). You can then use the <adaptive-icon> element to define the foreground, background, and monochromatic layer drawables for your icons.


1 Answers

Since there is no direct way to make a background to a VectorDrawable! So the alternative way is to use a group to arrive to the same Appearance (like a background).

The logic is to make another path before your own vector drawable path so that it may act as a background (No one will notice the difference!) when added together in a group.

For your case this image (with an amber background)!

background on vector drawable

Was made by adjusting your code like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp"
    android:height="48dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
  <group>
    <path android:name="square"
        android:fillColor="#FFFF6F00"
        android:pathData="M0,0 L24,0 L24,24 L0,24 z" />
    <path android:fillColor="#FF000000"
        android:pathData="M12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1s3.1,1.39 3.1,3.1v2L8.9,8L8.9,6zM18,20L6,20L6,10h12v10z"/>

  </group>
</vector>

So from there you can change the shape (to circular may be) or color (amber to blue) to fit your needs.

like image 112
Xenolion Avatar answered Sep 28 '22 01:09

Xenolion