How to Change Colors of a Drawable in Android

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

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.

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.

Android/Java: How to change programmatically the color of a Drawable in a BottomNavigationView (with no color selection at initialization)?

You can try this :)

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

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!

Drawable file wont change color in Dark Mode

To support dark mode you need a separate colors.xml file for the night version. To create colors file for night mode. Follow these steps.
Right-click on values folder
New > Value Resource File
In the Available Qualifiers search for Night mode and click on the (>>) button.
From the drop-down select Night
Name the file as colors

And define all the night mode colors here

Names of the colors will be the same as the Non-Night colors file

Another way to create colors.xml for night mode is
On the top left corner click on Android and Switch to project,
Go to folder app > src > main > res and create a new folder and name it values-night and inside that folder create an XML file named colors.xml

Change the color balance of a Drawable

You can apply a tint with the desired color to your image using mutate().setColorFilter(), and set the Drawable with the new color into your ImageView:

   ImageView imgImagen = (ImageView)findViewById(R.id.myImageView);
Drawable myImage = getResources().getDrawable( R.drawable.boy );
//Using red color.
myImage.mutate().setColorFilter(0xffff0000, PorterDuff.Mode.MULTIPLY);
imgImagen.setImageDrawable(myImage);

For example using this ImageView,

   <ImageView
android:id="@+id/myImageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/boy"
android:contentDescription="" />

you will have this image as result:

Sample Image

You can use too the color defined in your example:

 myImage.mutate().setColorFilter(Color.argb(255, 110, 150, 200), PorterDuff.Mode.MULTIPLY);


Related Topics



Leave a reply



Submit