how do you use svg in drawable folder? - android

i am creating a bottom navigation bar for a android app following this guide
however, when using svg in android:icon and building it, it shows a error
Error: The file name must end with .xml or .png
i found out that i needed to move the svg to the assets folder. but if i move it, i can't use the svg... any help is much appreciated, thank you

The answer above gives a link to convert pictures from SVG to XML, but this tool is not for free(only for one SVG file)
So why not to use ANDROID build-in tool?
The best way is to go to res-> drawable-> Vector Asset -> Local file (SVG, PSD) ->select your SVG file from the place you save it.
and you are done :)

Android uses vector drawables which are xml files rather than SVG's.
You will have to convert the SVG to a vector drawable. Many ways of doing it but a handy web tool is available here.
It won't do complex SVG's but copes with the majority.

Related

How to create my own Vector Drawble? (Android)

I need to use an icon of a contract on my app.
Usually, I'd create a new Vector Asset and lookup for an icon that might do the job.
Unfortunately, this time I couldn't find anything similar to my needs, so I want to create one!
I tried reading about the issue here but it wasn't really helpful.
can someone tell me how to do that and what is that pathData?
Thanks!
You can create vector assets from SVG sources. If you want to create your own icon, you should probably use a graphics program that can export drawings as SVG. Inkscape for example is free and available for a lot of platforms.
When you have the SVG file, in AndroidStudio select File -> New -> Vector Asset. In the upcoming dialog, select Asset Type 'Local file' and then pick/enter the path to your SVG.
You can download from the web / create by photoshop your own SVG files , once you have .svg file you can add it to your project by right click on app > new > Vector Asset > Local file (SVG,PSD) and use it as you normally use android:src="#drawable/ic_my_icon"
now a pathData is A path that represents the outline of a shape which can be filled or stroked. A path can also be used as a clipping path, to describe animation, or position text.
But for me what really did the job was just playing with the numbers of it and see the reaction of the actual path, what do i mean ?
Check out ShapeShifter and watch this video , drag some .svg files from the web and start playing with it , I'm sure you will get the idea .

How to create custom shape using XML as a drawable in Android?

Have a look at this Gist.
Above gist creates a drawable which looks like this:
Clearly in the gist, pathData property has been provided coordinates. How is this achieved, I mean, ofcourse someone doesn't found each point according to his needs.
My question : How is this achieved? Is there any tool present to do so?
Drawable like this reduces app size by avoiding to have an image instead. Also, there are questions for drawables in XML like this, this and many more but I think my Question is very different (and useful, probably).
You can design your SVG files using some tools such as Android Vector Asset Studio
Usually people don't write the SVG paths themselves, they use a tool or an editor where they sketch the drawing and generate the file.
What you have to do is to create a SVG image using an SVG editor such as Inkscape (the one I use)
or if you want a more professional (and expensive) editor you can use Adobe Illustrator. You can even use a tool called svg-edit which is accessible in the browser.
After creating your required art, save your SVG file.
Next goto and open Android Studio.
Right click on the res folder (any folder where drawables are usually placed)
Select New > Vector Asset. This opens the following window.
Select local file
Edit the path to direct it to the location of your svg file
Depending on the complexity of your image, errors may be thrown (not all svg elements get converted to xml. Hence, avoid using complex art in svg, use raster images in that case).
Android studio does some fixes on its own. So even if errors are thrown, if your image renders fine, then you are good to go.
Click Next > adjust a few more settings > Click Finish
Your xml vector asset has been created successfully.
Note:
To ensure backward compatibility, add the following to your build.gradle
android{
defaultConfig{
vectorDrawables.useSupportLibrary = true
}
}
dependencies{
compile 'com.android.support:appcompat-v7:23.2.0'
}
Hope this has helped you.
For more info see the official documentation.

Vector Asset Studio is importing colorful .svg file as black and white

I'm using the instructions found here, from Google to use the new option (March 2016) to import .svg files into drawable instead of .png files.
The problem I have is that the file gets imported as black and white, even though the file has many colors.
Here's an image of the .svg file, as read by Google Chrome:
This same file, is then read like this by the Vector Asset Studio:
And in the app, the image shows up as black and white.
Any idea of why this is happening?
The .svg file is getting generated on Adobe PhotoShop Creative Cloud, and I'm using Android Studio 1.5.1 on Mac.
Thank you!
EDIT: added link to .svg file I'm using for this question.
I also had the same problem but after digging internet
finally found the solution, here it is:-
After getting done with illustrator designing
1) Go to File --> Save As ---> Window Dialog box Opens--> type name of svg
--->Click Save
2) Now it shows SVG Options dialog box ---> At bottom left click More Options ---> In CSS Properties dropdown ---> Select Presentation Attributes
this setting makes svg compatible code for android import with colors !!
Sorry for making this answer elaborate ... did this for those who are beginners
Android Studio's SVG importer is somewhat limited. It only handles very simple SVG files. And I don't believe it supports the <style> element, or the class attribute. So you will need to change the CSS to XML properties.
For example:
<path class="st0" d="M163.3,10.8c2.4-0.5,6.2-0.9,10-0.9c5.5,0,9,0.9,11.6,3.1c2.2,1.6,3.5,4.1,3.5,7.5c0,4.1-2.7,7.7-7.2,9.3v0.1
c4,1,8.7,4.3,8.7,10.6c0,3.6-1.4,6.4-3.6,8.5c-3,2.7-7.7,4-14.6,4c-3.8,0-6.7-0.3-8.5-0.5V10.8z M168.8,28.1h5c5.8,0,9.2-3,9.2-7.1
c0-5-3.8-6.9-9.3-6.9c-2.5,0-4,0.2-4.8,0.4V28.1z M168.8,48.4c1.1,0.2,2.6,0.3,4.6,0.3c5.7,0,10.9-2.1,10.9-8.2
c0-5.8-5-8.2-10.9-8.2h-4.5V48.4z"/>
Will need to be changed to:
<path fill="#007299" d="M163.3,10.8c2.4-0.5,6.2-0.9,10-0.9c5.5,0,9,0.9,11.6,3.1c2.2,1.6,3.5,4.1,3.5,7.5c0,4.1-2.7,7.7-7.2,9.3v0.1
c4,1,8.7,4.3,8.7,10.6c0,3.6-1.4,6.4-3.6,8.5c-3,2.7-7.7,4-14.6,4c-3.8,0-6.7-0.3-8.5-0.5V10.8z M168.8,28.1h5c5.8,0,9.2-3,9.2-7.1
c0-5-3.8-6.9-9.3-6.9c-2.5,0-4,0.2-4.8,0.4V28.1z M168.8,48.4c1.1,0.2,2.6,0.3,4.6,0.3c5.7,0,10.9-2.1,10.9-8.2
c0-5.8-5-8.2-10.9-8.2h-4.5V48.4z"/>
Etcetera for the rest of the <path> elements.
It's so simple
you can change your color directly from your svg xml after adding image to your project .
when you added image to your project; svg color must be like this :
android:fillColor="#000000"
just need to change to this :
android:fillColor="#b3ff856d"
just looking for android:fillColor

How do I export Android XML vector drawables to another format?

I want to recreate my app for iOS and the icons in Android are XML vectors with the "android:pathData" attribute. How do I convert these to an image that I can place into Xcode and use (preserving transparency)?
I have searched for a solution and found nothing and read the article on the Android Developer site on Vector Asset Studio but found nothing about exporting these drawables to something I can import into Xcode.
I did it by creating an SVG file and copying the value of the pathData attribute of the path element in the XML file and pasting it into the d attribute of the path element in the SVG file. I then converted it to PNG using ImageMagick.
So
<path android:pathData="[Path Data]">
becomes
<path d="[Path Data]">.
Then
convert -background none image.svg image.png
in the command line.
If there is a better way of doing this please post it.
After using the trick Questioner provide, I use a browser to open the svg file, zoom-in to the size as big as possible and take a screenshot. FYI.

How to place 9patch generated images in Android Studio?

I used the freely available Simple 9patch generator available online, to generate a logo for my app's splash screen.
But how do I place it in Android Studio drawable folder?
Do I have to make separate drawable folders like ldpi, hdpi etc?
I tried that but when I try to set the image on a layout, it gives me an error saying
Cannot resolve symbol #drawable/something
You need to make sure, you have place the images in the correct folder. Here's an example,
Original post: Add 9-patch image to Android Studio
Get rid of the .9 in #drawable/splash.9. It is just #drawable/splash. .9.png is the file extension, and file extensions are not used in references to resources.

Categories

Resources