4

I am using DataBinding to tint vector drawable of ImageView on basic of an boolean flag. This code works well for >=21 version. But fails in <21 version.

<androidx.appcompat.widget.AppCompatImageView
    android:tint="@{model.nextEnabled ? @color/primary : @color/silver}"
    app:srcCompat="@drawable/ic_right_blue_24dp"
    />

Here ic_right_blue_24dp is a vector drawable.

After checking binding class, I could see that code for <21 version is not getting generated.

       if(getBuildSdkInt() >= 21) {

            this.mboundView1.setImageTintList(androidx.databinding.adapters.Converters.convertColorToColorStateList(modelBackEnabledMboundView1AndroidColorPrimaryMboundView1AndroidColorSilver));
        }

I have tried all things I could think, and could find.

  • AppCompatImageView
  • ImageView
  • app:srcCompat
  • android:src
  • app:tint
  • vectorDrawables.useSupportLibrary = true
  • AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

Here I must tell you that all things work using regular tint without binding.

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Test case **`app:tint`** – AskNilesh Oct 29 '18 at 12:45
  • 1
    Yes I tried already, it cause an error. `:Cannot find the setter for attribute 'app:tint' with parameter type int on androidx.appcompat.widget.AppCompatImageView.` – Khemraj Sharma Oct 29 '18 at 12:48
  • Have you tried this [v7/widget/AppCompatImageView](https://developer.android.com/reference/android/support/v7/widget/AppCompatImageView)? `` – Mykhailo Oct 29 '18 at 13:11
  • @Mykhailo No, I have migrated to `AndroidX`, I believe there is no difference in classes of `AndroidX` and `Support`. Because `AndroidX` was just an rename process. https://stackoverflow.com/q/51280090/6891563 – Khemraj Sharma Oct 29 '18 at 13:15
  • possibly related: https://stackoverflow.com/questions/11095222/android-imageview-change-tint-to-simulate-button-click/18724834#18724834 – Martin Zeitler Oct 29 '18 at 13:33
  • @MartinZeitler It is an custom view, so binding adapters are not defined for it. and it will sure not work in data binding. thanks for sharing. – Khemraj Sharma Oct 29 '18 at 13:40
  • @Khemraj added an answer. – Martin Zeitler Oct 29 '18 at 13:53
  • 1
    Does this answer your question? [Set the tint of an ImageView using databinding](https://stackoverflow.com/questions/65058461/set-the-tint-of-an-imageview-using-databinding) – childno͡.de Mar 10 '21 at 14:14
  • It is very old question, but I think it does. But it should be resolved by Google. – Khemraj Sharma Mar 11 '21 at 08:40

2 Answers2

2

there still is custom data-binding. even exactly the method, as requested:

@BindingMethods({
    @BindingMethod(
        type = "androidx.appcompat.widget.AppCompatImageView",
        attribute = "android:tint",
        method = "setImageTintList"
    )
})
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
2

This BindingAdapter will set tint programmatically...and it worked for me

@BindingAdapter("android:tint")
fun AppCompatImageView.setImageTint(@ColorInt color: Int) {
    setColorFilter(color)
}

Usage

<androidx.appcompat.widget.AppCompatImageView
    android:tint="@color/primary"
    ...
    />