1

Before I decided to ask it, I search some Remove Tint Color Programmatically, but it marked as duplicate so I didn't know if this is also a duplicate or not? because it didn't answer my question, I think.

Because I am asking also if is it possible to RESET or REMOVE the tint of ImageView after changing the drawable.

This is the normal image

enter image description here

This is the selected image

enter image description here

and this is the normal image again after deselecting the image

enter image description here

and this is my code.

if (isSelected) {
    // Reset the ImageView to normal
    isSelected = false
    imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_border_24))
} else {
    // Tint ImageView to Red
    isSelected = true
    imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_24))
    imgHeart.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(itemView.context, R.color.colorRedHeart))
}

I also try the imgHeart.imageTintList = null but the image is turning into white or disappear. I also try the imgHeart.colorFilter = null but the image is do nothing and turning into the last image.

I am using the Material Design Icon.

What I want to bring back the image into normal drawable as you see in the first image.

Thank you.

dotGitignore
  • 1,597
  • 2
  • 15
  • 34
  • I tried the code and call `imgHeart.imageTintList = null` after `imgHeart.setImageDrawable` exactly cleared the tint, I think there must be some problems elsewhere. – Hong Duan Jul 17 '18 at 01:23
  • Do you need all 3 icons, or only the last 2? –  Jul 17 '18 at 05:43

1 Answers1

0

The selected drawable is a completely different drawable than the other 2. You can't tint baseline_favorite_border_24 to look like the selected one, you can tint only the borders and can't fill it with color. By the way imageTintList requires API 21+.
So use 2 drawables(you don't need to download them, they exist in Android Studio), name the selected one as baseline_favorite_filled_24 and toggle between them.

if (isSelected) {
    isSelected = false
    imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_border_24))
} else {
    isSelected = true
    imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_filled_24))
}