Created
May 9, 2016 18:58
-
-
Save VassilisPallas/f6fb6e65f667efe4b717b797f2be1c0a to your computer and use it in GitHub Desktop.
set custom color to any drawable image
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
/** | |
* 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