Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Created October 18, 2015 00:24
Show Gist options
  • Save patrickhammond/c3569ebde0a88f93dd0f to your computer and use it in GitHub Desktop.
Save patrickhammond/c3569ebde0a88f93dd0f to your computer and use it in GitHub Desktop.
Drawable helper
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
public class DrawableHelper {
public static Drawable setupTintedIcon(Context context, @DrawableRes int iconDrawableResId, @ColorRes int iconColorResId) {
Drawable icon = ContextCompat.getDrawable(context, iconDrawableResId);
Drawable wrappedIcon = DrawableCompat.wrap(icon);
int iconColor = ContextCompat.getColor(context, iconColorResId);
DrawableCompat.setTint(wrappedIcon.mutate(), iconColor);
return wrappedIcon;
}
}
@atoennis
Copy link

To tint with a ColorStateList:

Drawable icon = ContextCompat.getDrawable(context, iconDrawableResId);
        Drawable wrappedIcon = DrawableCompat.wrap(icon);
        ColorStateList colorStateList = ContextCompat.getColorStateList(context, iconColorResId);
        DrawableCompat.setTintList(wrappedIcon.mutate(), colorStateList);
        return wrappedIcon;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment