How to put a svg file in your app? - android

I would like to insert a svg picture in my app so I looked for in Youtube ans I follow this tutorial :
https://www.youtube.com/watch?v=S65WtImJOvI
I create a directory by the name of raw, I download svg-android.jar and I added the following lines in my MainActivity.java :
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(Color.WHITE);
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
imageView.setImageDrawable(svg.createPictureDrawable());
setContentView(imageView);
The svg picture is android.svg. But when I tried to compile and execute with an emulator it crashes ! The app does not open !
So, May I have to do something in the activity_main.xml ?
What do you think about this ?
Thank you very much !

Easy,
Add it like this, and use inside of ImageView
app:srcCompat="#drawable/ic_person_outline_black_24dp"

Default Android ImageView does not support SVG files. You can include a custom library to perform this operation.
This is a wonderfull library I've used so far : https://github.com/Pixplicity/sharp

The easier way is to transform your SVG into a XML vector image (using this website for example).
Then you can use it almost like a standard resource drawable.
For example in your xml layout, on an ImageView, you'll have to use app:srcCompat instead of android:src.
You may have to add vectorDrawables.useSupportLibrary = true in your gradle file too.

Related

Using SVG as converted to vector into imageview

After design this below text in Inkscape application I would like to use it in Android into special ImageView
I am a beginner to use this feature in Android IFAIG I convert that on this link as:
http://a-student.github.io/SvgToVectorDrawableConverter.Web/
Now, after converting that I copy that into drawable folder and I assign that into ImageView I don't have this font structure in ImageView and that like with this image:
How can I resolve this problem?
The main problem is that your SVG file depends on a particular font being installed. Whether you convert the SVG using the online tool or whether you use it directly as proposed by GianhTran, it's better to remove this dependency.
So in Inkscape:
Select the text
Apply Path > Object to Path from the menu
Your text has now been converted into a path. It no longer depends on the font being present.
If you have svg resource file, you can be using for ImageView,
Add below config into your gradle
defaultConfig {
....
vectorDrawables.useSupportLibrary = true
...
}
And use srcCompat in ImageView
app:srcCompat="your_svg"

Set ImageView resource from Java

How to set image from android drawables at ImageView?
Not from R.drawable but from defaults... I.E. a star.
I know how to do it in xml, now I want to do the same from Java.
If by defaults you mean drawables that are bundled with android you set them in the same way as normal drawable eg imageView.setImageResource(android.R.drawable.stat_notify_sync), you just prefix R with android
First of all, to work with images at the Android environment, you have to put the images in the drawable directory, and the only way to use them in your application is using the class R. The example to set a image on android programmatically in Java is below:
YourImageView.setImageResource(R.drawable.name_of_your_image);

how do you use svg in drawable folder?

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.

how to load xml in imageview using glide

I have created an gradient xml in drawable folder of my project. I want to load that xml in an ImageView using Glide. How can I do that ?
Simple loading of image in ImageView using Glide:
Glide.with(LoginActivity.this).load(R.drawable.a270).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).into(backgroundImage);
But if I use the same code above replacing R.drawable.a270 with R.drawable.gradient_background with a drawable xml its not working.
Please try this -
Glide.with(LoginActivity.this).load("").error(R.drawable.gradient_background).diskCacheStrategy(DiskCacheStrategy.RESULT).into(backgroundImage);
OR
Glide.with(LoginActivity.this).load(null).fallback(R.drawable.gradient_background).diskCacheStrategy(DiskCacheStrategy.RESULT).into(backgroundImage);
I doubt you are using VectorDrawable or animated drawable. Please refer this and this for more information on this.

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.

Categories

Resources