Modifying The Color of an Android Drawable

How to change colors of a Drawable in Android?

I was able to do this with the following code, which is taken from an activity (the layout is a very simple one, just containing an ImageView, and is not posted here).

private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test_colors);

ImageView iv = (ImageView) findViewById(R.id.img);
Drawable d = getResources().getDrawable(RES);
iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
int to = Color.RED;

//Need to copy to ensure that the bitmap is mutable.
Bitmap src = ((BitmapDrawable) d).getBitmap();
Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
for(int x = 0;x < bitmap.getWidth();x++)
for(int y = 0;y < bitmap.getHeight();y++)
if(match(bitmap.getPixel(x, y)))
bitmap.setPixel(x, y, to);

return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
//There may be a better way to match, but I wanted to do a comparison ignoring
//transparency, so I couldn't just do a direct integer compare.
return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

How to achieve partially color changing of drawable?

You can split icon into 2 parts (same sized), for example we can take "format color text" icon from material icons

2

and then simple combine two drawables into one using <layer-list> (LayerDrawable)

ic_format_color_text.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_format_color_text_black_part1_24dp" />
<item android:drawable="@drawable/ic_format_color_text_black_part2_24dp" />
</layer-list>

Now, this drawable can be used as an icon for your button (MaterialButtonToggleGroup + MaterialButton). To tint "bottom shape" part only:

val dr = (colorTextBtn.icon as? LayerDrawable)?.getDrawable(0 /* colored shape layer index*/)
dr?.let { DrawableCompat.setTint(dr, /* color */) }

Sample Image

Change drawable color programmatically

Try this:

Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable); 
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);

Using DrawableCompat is important because it provides backwards compatibility and bug fixes on API 22 devices and earlier.

android drawables are colored automatically with color primary and it will not change

First, you are using MaterialComponents Theme and I suggest that you use Material Components too. Instead of Button, you can use:

com.google.android.material.button.MaterialButton

More about MaterialComponents and Material Themes you can find here: https://material.io/components?platform=android

To change the color of your Button or MaterialButton you need to use:

app:backgroundTint="@color/somecolor"

instead of:

android:backgroundTint="@color/somecolor"

Let me know if it works!

How to change color of drawable set as android:background?

Try:

yourImage.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

This literally gets background (without src) from ImageView and covers its shape with color. The way the drawable and given color are combining is defined by mode of Porter Duff - here you have more about it.

How to change the background color of a View Drawable without changing its body in recyclerView?

Add the below utility method in the adapter

public static void setDrawableColor(Context context, Drawable drawable, int color) {
Drawable drawableWrap = DrawableCompat.wrap(drawable).mutate();
DrawableCompat.setTint(drawableWrap, ContextCompat.getColor(context, color));
}

And use it as follows in onBindViewHolder

@Override
public void onBindViewHolder(coresHolder holder, int position) {
cores cores=list.get(position);
holder.vincule(cores);

// Changing background drawable
setDrawableColor(holder.item.getContext(), holder.item.getBackground(), R.color.purple_700);

}

Demo:

Sample Image

Android - Change color of Drawables

You can do it programmatically:

public static void colorDrawable(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
if (wrappedDrawable != null) {
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
setBackgroundDrawable(view, wrappedDrawable);
}
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackgroundDrawable(View view, Drawable drawable) {

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}


Related Topics



Leave a reply



Submit