Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android drawable resources: Dynamically change an icon color/"background"?

I have a png file that is transparent outside which is used as an icon image somewhere in my UI; I'd like for the user to be able to specify a different color for it at runtime (as in the image on the left below, assuming I have the android icon); or a different "background" image (Image on the right below, suppose the icon is a shape combining a circle and a triangle). I read this post which used ColorFilter to set all the white color pixels to another color in code; but it doesn't allow me to change the "background" of the icon. I guess I probably can do this with a ClipDrawable but I can't figure out how.

A: Suppose I have the android icon B: Suppose the icon is a shape combining a circle and a triangle)

like image 594
mobileTofu Avatar asked Feb 14 '26 06:02

mobileTofu


1 Answers

If you use ImageView, you can set also the source of image (android:src) but also background for the image (android:background). So if your image has a transparent region, or region with alpha channel, the background color will be used for these pixels.

The background color then can be changed dynamically. Can you use this approach?

    imageView.setBackgroundColor(0x00FF0000); // red
like image 55
peter.bartos Avatar answered Feb 16 '26 19:02

peter.bartos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!