I am using svg for icons for ImageView
it can be given using app:srcCompat
But when i want to use it for Buttons
as selector the app crashes with resource not found exception for devices with api below 21
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_enabled" android:state_enabled="true" />
<item android:drawable="@drawable/button_disabled" android:state_enabled="false" />
</selector>
Where button_enabled and button_disabled are both svg
Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.
To design the shape of an HTML button we can use SVG elements (Scalable Vector Graphics). It basically defines the vector-based graphics in XML format. Every element and every attribute in SVG files can be animated. We can use SVG to create 2-D graphics of any custom shape.
SVG Viewer - SVG Reader SVG Viewer – SVG Reader is a free Android app to view SVG files. This is another simple app with a single focus on viewing the SVG files. While opening the SVG file, it gives users an option to pick the background color. Users can select a background color of their choice and view the SVG file.
I suggest you to follow the below steps :-
Step 1:
Right click on drawable folder -> select New -> select Vaector Assests
Step 2:
Select Local file svg now select the path of your svg image click on next you will have now the svg image in your drawable folder
do the same thing for svg images you want to use in your application
Now use the below code for selector buttons selector_button.xml file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_check_circle" android:state_pressed="true" />
<item android:drawable="@drawable/ic_build" android:state_focused="true" />
<item android:drawable="@drawable/ic_build" />
</selector>
I imported two svg 1st ic_check_circle and 2nd ic_build you can replace as per your needs.
In your imageView use below line
app:srcCompat="@drawable/selector_button"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With