Last active
April 7, 2021 19:05
-
-
Save oli-g-sk/e66365ca5e36edcad22f1cc191fac3d6 to your computer and use it in GitHub Desktop.
Load and tint a Drawable, with vector support on older 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
public static class Util { | |
public Drawable getTintedDrawable(Context context, @DrawableRes int id, @ColorRes int colorId) { | |
// use AppCompatResources to get Drawable for pre-Lollipop vector compatibility; | |
Drawable drawable = AppCompatResources.getDrawable(context, id); | |
if (drawable == null) { | |
return null; | |
} | |
// mutate and wrap in DrawableCompat | |
Drawable wrapped = DrawableCompat.wrap(drawable.mutate()); | |
// apply tint | |
int tint = ContextCompat.getColor(context, colorId); | |
DrawableCompat.setTintMode(wrapped, PorterDuff.Mode.SRC_IN); | |
DrawableCompat.setTint(wrapped, tint); | |
return wrapped; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment