Skip to content

Instantly share code, notes, and snippets.

@VassilisPallas
Created May 9, 2016 18:58
Show Gist options
  • Save VassilisPallas/f6fb6e65f667efe4b717b797f2be1c0a to your computer and use it in GitHub Desktop.
Save VassilisPallas/f6fb6e65f667efe4b717b797f2be1c0a to your computer and use it in GitHub Desktop.
set custom color to any drawable image
/**
* change color on a drawable image
*
* @param context the context
* @param imageId the id from the image
* @param color the color hex
* @return
*/
public Drawable setCustomDrawableColor(Context context, int imageId, int color) {
try {
Drawable image;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
image = context.getResources().getDrawable(imageId, context.getTheme());
} else {
image = context.getResources().getDrawable(imageId);
}
if (image != null)
image.setColorFilter(color, PorterDuff.Mode.SRC_IN);
return image;
} catch (IllegalStateException e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment