Last active
December 6, 2017 12:18
-
-
Save dptsolutions/754e64fae939aa46ea93ccdc3b738d29 to your computer and use it in GitHub Desktop.
Vector Drawables used as menu icons at all API Levels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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