Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vector Drawable Compat

I am making an android app with a few fragments. In one of these fragments, I have a toolbar with a back arrow as an image button.
In the XML File, I have the "app:srcCompat" attribute, but I get an error when using this attribute saying this: "To use VectorDrawableCompat, you need to set 'android.defaultConfig.vectorDrawables.useSupportLibrary = true'

like image 629
Guilherme R Avatar asked Dec 19 '16 15:12

Guilherme R


People also ask

What is use of vector drawable in android?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.

Can vector Drawables be scaled?

In Android 5.0 (API Level 21) and above, you can define vector drawables, which scale without losing definition. Changing the width and height in the definition of the vector drawable to 200dp significantly improves the situation at the 400dp rendered size.

What is a vectordrawable?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.

How to use vector drawables instead of images in srccompat?

Also, you need to add this block of code in every activity or fragment where you're referencing drawables instead of images in srcCompat: Show activity on this post. You have to add vectorDrawables.useSupportLibrary = true this line of code in you app level build.gradle inside the defaultConfig tag

What can I do with vectordrawablecompat from androidx?

For example, using VectorDrawableCompat from AndroidX enables: Both nonZero and evenOdd path fillTypes —the two common ways of defining the inside of a shape, often used in SVGs ( evenOdd added to platform impl in API 24) Gradient & ColorStateList fills/strokes (added to platform impl in API 24)

How to define a vector drawable Android?

VectorDrawableCompat can be defined in an XML file with the <vector> element. The VectorDrawableCompat has the following elements: <vector> Used to define a vector drawable android:name Defines the name of this vector drawable. android:width Used to define the intrinsic width of the drawable. This support all the dimension units, normally ...


2 Answers

In your module build.gradle file, you need to add this line:

apply plugin: 'com.android.application'  android {     ...      defaultConfig {         ...          vectorDrawables.useSupportLibrary = true // This line here     }     ... }  ... 
like image 169
Farbod Salamat-Zadeh Avatar answered Sep 27 '22 21:09

Farbod Salamat-Zadeh


add this line to your Gradle file under defaultConfig block:

vectorDrawables.useSupportLibrary = true 

Also, you need to add this block of code in every activity or fragment where you're referencing drawables instead of images in srcCompat:

static {         AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);     } 
like image 38
Marzieh Bahri Avatar answered Sep 27 '22 21:09

Marzieh Bahri