3

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?

Igor
  • 320
  • 1
  • 3
  • 16

6 Answers6

9

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);
W3hri
  • 1,563
  • 2
  • 18
  • 31
4

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;
}

Fast Android asset theming with ColorFilter - Dan Lew

yiati
  • 995
  • 1
  • 12
  • 27
2

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.

Ruben2112
  • 423
  • 4
  • 18
1
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.

Haroon
  • 497
  • 4
  • 13
1

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"
FlipNovid
  • 1,191
  • 3
  • 12
  • 20
1

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);
    
}
Ferhad Konar
  • 434
  • 1
  • 7
  • 9