I have an icon in png in drawable. It's black with the transparent background. How can I change the icon color without adding another drawable?
-
Go paint and inverse it – Haroon May 11 '16 at 09:24
-
Without paint ))))) Only in the android – Igor May 11 '16 at 09:25
-
Refer http://stackoverflow.com/a/5837189/6097062 – Saurabh Vardani May 11 '16 at 09:26
-
Possible duplicate of [How to change colors of a Drawable in Android?](http://stackoverflow.com/questions/1309629/how-to-change-colors-of-a-drawable-in-android) – Jonathan Darryl May 11 '16 at 09:27
6 Answers
You can use a ColorFilter to change the icons color at runtime.
Try something like this:
Drawable mIcon= ContextCompat.getDrawable(getActivity(), R.drawable.your_icon);
mIcon.setColorFilter(ContextCompat.getColor(getActivity(), R.color.new_color), PorterDuff.Mode.MULTIPLY);
mImageView.setImageDrawable(mIcon);

- 1,563
- 2
- 18
- 31
A good helper function for this is,
public Drawable getTintedDrawable(Resources res,
@DrawableRes int drawableResId, @ColorRes int colorResId) {
Drawable drawable = res.getDrawable(drawableResId);
int color = res.getColor(colorResId);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
return drawable;
}

- 995
- 1
- 12
- 27
Try using this static method:
public static Drawable changeDrawableColor(Drawable drawable, int color) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, color);
return drawable;
}
The color parameter could be a color from your resources.

- 423
- 4
- 18
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN));
Try the above You can Play with PorterDuffColorFilter(0xffff00,PorterDuff.Mode.LIGHTEN) You can use Black etc.

- 497
- 4
- 13
PorterDuff.Mode.SRC_IN
Using this property you change the color of the icon with the exact color chosen.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Drawable mIcon= ContextCompat.getDrawable(this, R.drawable.icon_send);
mIcon.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
ibSendMessage.setBackground(mIcon);
}
In newer versions of Android you can do this in XML
android:backgroundTint="@color/colorAccent"

- 1,191
- 3
- 12
- 20
-
`backgroundTint` didn't work for me, currently using Android Studio 4.0 – syed dastagir Nov 20 '20 at 13:28
If you change "Drawable" other 'ImageView's that use this "Drawable resource" will also change, it is better to apply the filter to single "ImageView".
Using these two functions, you can enter the color you want as an ID (like: R.color.white) or color code (like: #efec0c).
public void ChangePngIconColor(String Target_Color, ImageView Target_ImageView){
/*
* Sample:
* Target_Color = "#efec0c"; OR Target_Color = "efec0c";
*
*/
Target_Color = (Target_Color.startsWith("#")) ? Target_Color : "#"+Target_Color;
Target_ImageView.setColorFilter(Color.parseColor(Target_Color), PorterDuff.Mode.SRC_IN);
}
public void ChangePngIconColor(int Target_Color_ID, ImageView Target_ImageView){
/*
* Sample: Target_Color = R.color.white;
*
*/
Target_ImageView.setColorFilter(ContextCompat.getColor(context,Target_Color_ID), PorterDuff.Mode.SRC_IN);
}

- 434
- 1
- 7
- 9