app:srcCompat
with ImageView
allows for backward compatible use of vector drawables. But how can you use them with other View
s besides ImageView
? For example, the TextView
attributes like android:drawableLeft
.
Also using the vector drawable as an android:icon
with MenuItem
caused a crash with the following exception:
Fatal Exception: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.support.v7.view.menu.MenuItemImpl.setActionView(MenuItemImpl.java:621)
at android.support.v7.view.menu.MenuItemImpl.setActionView(MenuItemImpl.java:40)
at android.support.v4.view.MenuItemCompat.setActionView(MenuItemCompat.java:310)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:465)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at com.example.niceapp.context.main.MainActivity.onCreateOptionsMenu(MainActivity.java:101)
at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
With Support Library 23.2.0, how can this issue be addressed?
You can add Vector Drawable in TextView programmatically. Make use of VectorDrawableCompat inorder to add drawableLeft/ drawableRight /drawableTop/ drawableBottom/ drawableStart/ drawableEnd.
Steps:
i. If TextView is inside Activity:
ii. If TextView is inside Fragment:
For more information on VectorDrawableCompat Refer this link
Dadou is right. So if you would like use selector for view with VectorDrawables you need to add:
to every Activity where you want to use VectorDrawables on devices with versions below Android 5.
I'm using new support library and all I have to do is:
In Build.gradle file
Now Where ever you are using like fragment, activity or adapter use this as the first line in your class
after that use like as we do before, something.xml
Something.java
or if you have drawable id needed to be set dynamically
Update 2: They have added an option to enable it again in Support Library 23.4.0:
Update: This no longer works starting from version 23.3.0
From Android Developers Google+ post
That quote is from the official blogpost for the release of version 23.2.0 of the Support Library.
The post also mentions the following:
This translates to the steps below:
Step 1:
Create or import a vector resource which you require for the app. For example, one can create a vector drawable for the search icon and name it
ic_action_search_vector.xml
Step 2:
Create another proxy drawable resource for the vector drawable previously created. Say, for the previous
ic_action_search_vector.xml
,ic_action_search.xml
can be created as a simpleStateListDrawable
which could contain the lines below:This step can be skipped if you have referenced the vector drawable from another drawable resource which you will use with your View.
Step 3:
Use the drawable resource (here,
ic_action_search.xml
) that refers to the vector drawable (ic_action_search_vector.xml
) instead of the vector drawable directly. For a menu, it would look like:This is the solution to that problem!
Form android studio 3.0.0 android:src is not support vector image and below 21 my get exception. use app:srcCompat for vector image. Keep all vector image files inside the drawable folder.
Now you can use app:srcCompat="@drawable/ic_cart_notify" but if you try to use in android:background or android:drawableLeft then you got "Error inflating" exception. For that create a new wrapped drawable .xml file, ic_cart_notify is vector icon.
Vector drawables can be used pre-Lollipop in other places than
app:srcCompat
, but it comes with a price.I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).