Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the tint of an ImageView using databinding

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 ?

like image 425
Jéwôm' Avatar asked Nov 29 '20 09:11

Jéwôm'


3 Answers

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

And app:tint is no more deprecated.

like image 114
Jéwôm' Avatar answered Nov 11 '22 11:11

Jéwôm'


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

like image 39
childno͡.de Avatar answered Nov 11 '22 12:11

childno͡.de


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

like image 45
divonas Avatar answered Nov 11 '22 13:11

divonas