ahmad aghazadeh ahmad aghazadeh - 4 months ago 105
Android Question

How to use android xml (SVG) in drawableTop, drawableLeft,...?

When use

drawableTop=svg drawable
textview occured error. how fix this bug?

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableTop="@drawable/ic_cached_black_24dp" // <--XML(SVG) error
/>

<ImageView
android:id="@+id/imgToolPic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
app:srcCompat="@drawable/ic_photo_black_24px" // <--XML(SVG) dont error
/>


build.gradle :

android {

defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}


Error :


android.view.InflateException: Binary XML file line #43: Error
inflating class TextView

Answer

In activity add this method

static
    {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

In onCreate ues this code :

Drawable img = getContext().getResources().getDrawable( R.drawable.ic_cached_black_24dp);
extView.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
Comments