0

I write a custom attribute for load image url like this:

@BindingAdapter("srcCircleUrl")
fun loadCircleImage(view: ImageView, imageUrl: String) {
    loadImage(view.context, imageUrl, view, options = circleCropTransform())
}

when I want to set a raw string in xml, it gives me srcCircleUrl attribute not found error.

for example if I write something like this, it does not work:

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    app:srcCircleUrl="https://66.media.tumblr.com/97bcd9782631f8bef87bb30e830344a6/tumblr_owxu10zbPB1tl4ciuo4_250.png"
    android:scaleType="centerCrop"
    tools:srcCompat="@drawable/flag_iran" />

so, the question is, how can I give a raw string as input to a custom databinding attribute?


I also test these ways:

app:srcCircleUrl="@{https://66.media.tumblr.com/97bcd9782631f8bef87bb30e830344a6/tumblr_owxu10zbPB1tl4ciuo4_250.png}"

app:srcCircleUrl="@{`https://66.media.tumblr.com/97bcd9782631f8bef87bb30e830344a6/tumblr_owxu10zbPB1tl4ciuo4_250.png`}"
Amir Hossein Ghasemi
  • 20,623
  • 10
  • 57
  • 53

2 Answers2

7

You need to surround your string with single quotes and curly brackets like so app:something='@{"my string"}'.

I think this should work for you:

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    app:srcCircleUrl='@{"https://66.media.tumblr.com/image.png"}'
    android:scaleType="centerCrop"
    tools:srcCompat="@drawable/flag_iran" />
gookman
  • 2,527
  • 2
  • 20
  • 28
0

I Think U need returns with url format from data bind resource u cannot directly pass the https url to app:srcUrl

@BindingAdapter("imageUrl")
    public static void setImageUrl(ImageView view, String imageUrl) {
       Picasso.with(view.getContext())
                .load(imageUrl)
                .placeholder(R.drawable.placeholder)
                .into(view); 0));
    }

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="prod" type="com.webkul.example.Product"/>
    </data>
<ImageView
  android:id="@+id/image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  app:imageUrl="@{prod.img_url}"/>
</layout>
Aravind V
  • 358
  • 2
  • 10