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:
You can use too the color defined in your example:
myImage.mutate().setColorFilter(Color.argb(255, 110, 150, 200), PorterDuff.Mode.MULTIPLY);
Related Topics
Runtimeexception: Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
Self-Signed Ssl Acceptance on Android
How to Include *.So Library in Android Studio
Integrate Mupdf Reader in an App
How to Create a Closed (Circular) Listview
The Setmobiledataenabled Method Is No Longer Callable as of Android L and Later
List of Android Permissions Normal Permissions and Dangerous Permissions in API 23
How to Make an Alert Dialog Fill 90% of Screen Size
Singleton Object Becomes Null After App Is Resumed
How to Use Custom Font in a Project Written in Android Studio
Push Notifications on Content Change
Clicking Urls Opens Default Browser
Soft Keyboard Open and Close Listener in an Activity in Android
How to Solve Java.Lang.Outofmemoryerror Trouble in Android
Runtimeexception: Unable to Instantiate Application
Using Intent in an Android Application to Show Another Activity
Handling Registration Id Changes in Google Cloud Messaging on Android