0

I'm trying to use drawable in textView like this

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/svg_ic_profile_logout"
    android:drawablePadding="20dp"
    android:drawableStart="@drawable/svg_ic_profile_logout"
    android:gravity="center_vertical"
    android:onClick="@{viewModel.onLogoutClick}"
    android:text="@string/profile_logout"/>

but the icon I using is SVG, it works perfectly fine for API > 21 but got a crash for API level lower than 21.

islamdidarmd
  • 725
  • 5
  • 19

2 Answers2

0

Add vectorDrawables.useSupportLibrary = true in you app level build.gradle as follows:

  //For Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

Refer to this guide.

islamdidarmd
  • 725
  • 5
  • 19
0
  1. If your activity extends FragmentActivity, extend AppCompatActivity.

  2. If you have an exception: android.view.InflateException: Binary XML file line #100: Error inflating class TextView having TextView with android:drawableLeft="@drawable/ic_svg_image", then follow https://stackoverflow.com/a/48146570/2914140.

Set SVG support as described there and wrap your SVG image in selector or layer-list:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_svg_image" />
</selector>

Then change to android:drawableLeft="@drawable/svg_image_selector", where svg_image_selector is an XML created above.

CoolMind
  • 26,736
  • 15
  • 188
  • 224