Using SVG in android selector for buttons

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" 

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

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

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 ]

Tags:

Android

Svg