Vishwajit Palankar
Vishwajit Palankar

Reputation: 3083

Using SVG in android selector for buttons

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

Upvotes: 6

Views: 13717

Answers (4)

OBX
OBX

Reputation: 6114

SVG rendering in Android was targeted for API level 21 and +. ie from Lollipop onward. Since the crash occurs in a lower version, what you require is backward compatibility for SVG .

Ideally there are two solutions here:

  1. Use the Victor library, which supports backward compatibility. Available [ here ] . Also check this detailed medium article on Vector usage in Android. [ here ]
  2. From Android support library v23.2 onward, SVG usage has been made backward compatible till API v 7. The detailed guide is available [ here ]. Also see this Answer as well. [ here ]

Upvotes: 2

Vishwajit Palankar
Vishwajit Palankar

Reputation: 3083

I have the answer to my own question. In your activity just add this static block

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

Upvotes: 5

Pranav Darji
Pranav Darji

Reputation: 752

Use below selector_checkbox.xml

<?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_checked="true" android:state_focused="true" />
    <item android:drawable="@drawable/ic_uncheck_circle" android:state_checked="false" android:state_focused="true" />
    <item android:drawable="@drawable/ic_uncheck_circle" android:state_checked="false" />
    <item android:drawable="@drawable/ic_check_circle" android:state_checked="true" />
</selector>

Where ic_check_circle and ic_uncheck_circle both are svg file imported from local file as i already mentioned in above steps

And in your xml file use below code for CheckBox and put selector_checkbox.xml file as a button and you are done

<android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/appCompatCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/selector_checkbox" />

Upvotes: 1

Pranav Darji
Pranav Darji

Reputation: 752

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" 

Upvotes: 5

Related Questions