Reputation: 59
I have one white image and in app I need to show that image in many different colors.
Is there a way to change color of that white image programmatically so that I don't need to have separate image for every color?
For now I have this:
button.setBackgroundResource(getResources().getIdentifier("image_white", "drawable", getPackageName()));
Upvotes: 0
Views: 5925
Reputation: 2209
You can use :
Drawable myDrawable = ContextCompat.getDrawable(context,R.drawable.drawable_id);
myDrawable.setTint(ContextCompat.getColor(context, R.color.color_id);
Sorry for my english. I hope it's help.
Upvotes: 1
Reputation: 15615
Yes, it is simple. You can programmatically change colors of any image using Color Filters.
Here is a simple example on how you can use it.
ImageView imageView = (ImageView) findViewById(R.id.imageV);
imageView.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
The color of the image will be changed to red. You can use any color of your choice, but the main magic lies in using the proper mode for PorterDuff
. You can also give it a try using PorterDuff.Mode.SRC_ATOP
if you are not satisfied with the results.
Upvotes: 6
Reputation: 1800
You can change color if you are using material design icons.
android:backgroundTint="@color/yourColor"
android:tint="@color/yourColor"
Here you can find material design icons.
Otherwise you can use custom xml resource file to change background, but i think i won't fit your need.
Upvotes: -1