Skip to content Skip to sidebar Skip to footer

How Can I Use Vectordrawable With The Android Toolbar?

What is the proper method to use the new VectorDrawable in the toolbar? I tried to use the app:srcCompat element as illustrated below, but nothing showed up.

Solution 1:

Turns out it's quite easy. Say, you have vector drawable vd_trash_24dp.

Describing MenuItem one cannot address VectorDrawable directly with android:icon. It seems ignoring app:srcCompat also.

But. As all we know ;)

AppCompat does support loading vector drawables when they are referenced in another drawable container such as a StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable

Let's try it, should we?

Create StateListDrawable vd_test_vd

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

than

<menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><itemandroid:id="@+id/menu_action_filter"android:title="@string/menu_action_filter"android:icon="@drawable/vd_test_vd"android:orderInCategory="100"app:showAsAction="always"/></menu>

street magic indeed.

Yes, one could try and set drawable at runtime with MenuItem.setIcon(). But who need that %)

Post a Comment for "How Can I Use Vectordrawable With The Android Toolbar?"