Set the tint of an ImageView using databinding

Viewed 3552

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 ?

4 Answers

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

And app:tint is no more deprecated.

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

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
Related