Skip to content

Instantly share code, notes, and snippets.

@oli-g-sk
Last active April 7, 2021 19:05
Show Gist options
  • Save oli-g-sk/e66365ca5e36edcad22f1cc191fac3d6 to your computer and use it in GitHub Desktop.
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
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