Change Drawable Color Programmatically

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.

change a drawable color programmatically in android


Drawable backgroundDrawable = DrawableCompat.wrap(view.getBackgroundDrawable()).mutate();
DrawableCompat.setTint(backgroundDrawable, color);

You can use this!!

Change drawable color programmatically for each item in list

Just need to use mutate() for drawable.

That's why every item have same color:

By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

When I use mutate() for recycler items in adapter, it produce states for every single item in list.

Usage:

override fun onBindViewHolder(holder: SomeViewHolder, position: Int) {
rarities[position].color?.let {
holder.circleView.setCircleColor(it) // custom view fun which take HEX parse it and invalidate
holder.cardInfo.background.run {
mutate()
setTint(Color.parseColor("#$it"))
}
}
<...>
}

For more information article and documentation

change a drawable color programmatically in android


Drawable backgroundDrawable = DrawableCompat.wrap(view.getBackgroundDrawable()).mutate();
DrawableCompat.setTint(backgroundDrawable, color);

You can use this!!

Setting the Color of a TextView Drawable

Try below solution

private void setTextViewDrawableColor(TextView textView, int color) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (drawable != null) {
drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
}
}
}

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


Related Topics



Leave a reply



Submit