Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SVG in android selector for buttons

Tags:

android

svg

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

like image 201
Vishwajit Palankar Avatar asked Jan 23 '17 08:01

Vishwajit Palankar


People also ask

Can SVG be used in Android?

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.

Can you make an SVG a button?

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.

How do I open an SVG file on Android?

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.


1 Answers

I suggest you to follow the below steps :-

Step 1:

select svg from local folder

Right click on drawable folder -> select New -> select Vaector Assests

Step 2:

click on next

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" 
like image 167
Pranav Darji Avatar answered Sep 19 '22 22:09

Pranav Darji