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?
For AppCompat version 23.3.0 where no work solution via selector XML (razzledazzle's accepted answer) we can do this by programmatically:
activity_main.xml
MainActivity.java
This code is equivalent for this selector xml:
UPDATE
If the vector drawable is not shown using API 23, you'll need to convert the
VectorDrawable
to a regularDrawable
first. If you want to usesetCompoundDrawablesWithIntrinsicBounds
you'll need to do this, but for StateListDrawable I didn't need to.Android 5.0 (API level 21) and higher provides vector drawable support. If your app has a minimum API level that is lower, Vector Asset Studio adds the vector drawable file to your project; also, at build time, Gradle creates PNG raster images at various resolutions. Gradle generates the PNG densities specified by the Domain Specific Language (DSL) generatedDensities property in a build.gradle file. To generate PNGs, the build system requires Android Plugin for Gradle 1.5.0 or higher.
This is not true if you include in your gradle vectorDrawables.useSupportLibrary = true
either set to false or remove the line completely and all your vectors will work as they were. But for olders versions of android they will be able to rely on the converted PNG
AppCompatTextView now supports
app:drawableLeftCompat
,app:drawableTopCompat
,app:drawableRightCompat
,app:drawableBottomCompat
,app:drawableStartCompat
andapp:drawableEndCompat
compound drawables, supporting backported drawable types such as VectorDrawableCompat.Include this in your gradle file
In your text view you can use