11

I use databinding to set the tint of my ImageView. And this is working well :

android:tint="@{plantEntity.isFavorite ? @color/favorite : @color/favorite_none}" />

The problem is android:tint is deprecated. When I try to use app:tint instead, I have this error :

Cannot find a setter for <android.widget.ImageView app:color> that accepts parameter type 'int'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

Why and what I have to do ? Create a BindingAdapter ?

Jéwôm'
  • 3,753
  • 5
  • 40
  • 73

5 Answers5

15

similar to what has been discussed on Tint does not work <21 version in DataBinding

add a binding adapter:

@JvmStatic
@BindingAdapter("app:tint")
fun ImageView.setImageTint(@ColorInt color: Int) {
  setColorFilter(color)
}

and you don't have to use any compat if your minSdk is > 21 (which is a good deal anyway in 2021 you should not support anything below 26)

anyhow .. this seems to be a fug on the androidx.databinding expression https://issuetracker.google.com/issues/152953070

user924
  • 8,146
  • 7
  • 57
  • 139
childno͡.de
  • 4,679
  • 4
  • 31
  • 57
9

It's working and using androidx.appcompat.widget.AppCompatImageView.

And app:tint is no more deprecated.

Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
4

Use androidx.appcompat.widget.AppCompatImageView with android:tint.

divonas
  • 992
  • 1
  • 10
  • 11
1

Just to complement @Jéwôm's answer with a clear example, use AppCompatImageView with android:tint

<androidx.appcompat.widget.AppCompatImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tint="@{data.yourImageTintColorRes}"
    android:src="@drawable/yourDrawable"/>
Mike
  • 21
  • 4
0

Add variable

<variable name="color" type="Integer" />

and...

android:background="@{color == 0 ? @color/black : color == 1 ? @color/red: @color/purple_200 }"

On the code, set the value

 binding.color = 0
dma123
  • 79
  • 4