Skip to content

Instantly share code, notes, and snippets.

@dptsolutions
Last active December 6, 2017 12:18
Show Gist options
  • Save dptsolutions/754e64fae939aa46ea93ccdc3b738d29 to your computer and use it in GitHub Desktop.
Save dptsolutions/754e64fae939aa46ea93ccdc3b738d29 to your computer and use it in GitHub Desktop.
Vector Drawables used as menu icons at all API Levels
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_logout"
android:title="@string/action_logout"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
Toolbar toolbar;
//Call initToolBarMenuIcons after you've inflated the layout into your Activity/Fragment that holds your Toolbar.
@MainThread
@SuppressLint("NewApi") //suppress warning, since using DrawableCompat to set tint
public void initToolBarMenuIcons() {
toolbar.inflateMenu(R.menu.main);
//Manually adding icon since it's a vector drawable and we can't currently inflate from XML into menuitems
Drawable wrappedLogoutIcon = DrawableCompat.wrap(AppCompatResources.getDrawable(getContext(), R.drawable.ic_logout_black_24dp));
//Tint it too
DrawableCompat.setTint(wrappedLogoutIcon, Color.WHITE);
//Set the tinted vector drawable to the item
toolbar.getMenu().findItem(R.id.action_logout).setIcon(wrappedLogoutIcon);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment