spectacularbob
spectacularbob

Reputation: 3218

Android Image Button tint only one color

I am writing an app where I have an icon on an image button that looks like a paintbrush with paint on it. When ever the user changes colors, I want the paint on the brush to change to the chosen color.

I have tried making the icon's paint white and tinting the image button, but this just tints the whole paintbrush image. I have also tried setColorFilter() without success. Anyone know how I might be able to accomplish this without creating a drawable resource for every color?

Edit: Perhaps I should clarify,

If I have an icon that looks like this paintbrush plain

Then when the user selects red, it should look like this red brush

Thanks

Upvotes: 3

Views: 3306

Answers (2)

Anitha Manikandan
Anitha Manikandan

Reputation: 1170

Yeah. Is possible with a white image. The image should be designed in such a way that the color area in the brush icon alone should be in white color. Then you can apply colorFilter on the image to change the white color alone to any other color(probably the user selected color)

imageBtn.getDrawable().setColorFilter(0xffff0000, PorterDuff.Mode.MULTIPLY);

And this one below that apply to background

imageBtn.getBackground().setColorFilter(0xffff0000, PorterDuff.Mode.MULTIPLY);

I tried it with chessboard image which has white and black squares, I could change the white squares to any color with the colorFilter.

It will definitely help.

Upvotes: 2

arodriguezdonaire
arodriguezdonaire

Reputation: 5562

This should work:

ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton11); // image button
            imgBtn.setColorFilter(getResources().getColor(android.R.color.darker_gray), Mode.SRC_ATOP);

Upvotes: 2

Related Questions