1

I'm making a binding adapter where a I can set color filter to a drawable.

So I have these lines of code in java class

@BindingAdapter(value = {"drawable", "filterColor"}, requireAll= true)
public static void setColorFilterToDrawable(View view, int drawableInt, @ColorRes int color){
    Drawable drawable = ContextCompat.getDrawable(view.getContext(),drawableInt);
    drawable.setColorFilter(new
            PorterDuffColorFilter(ContextCompat.getColor(view.getContext(), color), PorterDuff.Mode.SRC_IN));
    view.setBackground(drawable);
}

In XML, I use it like this

<TextView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/textCircle"
    android:drawable="@{@drawable/background_circle}"
    app:colorFilter="@{@color/primaryDark}"
    android:gravity="center"
    android:layout_alignParentRight="true"
    android:textColor="@android:color/white"
    android:textSize="14sp"
    android:text="X"
    android:textStyle="bold"
    android:layout_marginRight="5dp"
    android:onClick="@{viewModel::deletePhoto}" />
tynn
  • 38,113
  • 8
  • 108
  • 143
devadnqpnd
  • 154
  • 1
  • 3
  • 16

1 Answers1

2

android:drawable does not exist for TextView. Judging from your @BindingAdapter you're expecting a value set to app:drawable.

Additionally @drawable and @color expand to a drawable and a color integer. So your @BindingAdapter should implement the following interface instead.

@BindingAdapter(value = {"drawable", "filterColor"}, requireAll= true)
public static void setColorFilterToDrawable(View view, Drawable drawable, @ColorInt int color){
    drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    view.setBackground(drawable);
}
tynn
  • 38,113
  • 8
  • 108
  • 143