Binding Adapter not working properly

12.9k views Asked by At

I have a hard time making @BindingAdapter to work in my project.

@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
    Log.d("TEST","URL: " + url);
}

Above code shows how it is implemented in my ViewModel. Nothing special.

    <ImageView
        android:id="@+id/image_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:layout_below="@id/profile_container"
        app:imageUrl="@{item.imageUrl}"
        tools:src="@drawable/placeholder_image"/>

This does not work. namespace app is unbound. So what am i missing. I tried following https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwooh and see how they set bindingAdapter. But there is something i have missed

7

There are 7 answers

2
Sergei Bubenshchikov On

I suggest to use "bind" namespace for bindable attributes and use same names for adapter parameter and layout attribute.

Adapter:

@BindingAdapter("bind:imageUrl")
public static void setImageUrl(ImageView imageView, String imageUrl) {
     Log.d("TEST","URL: " + imageUrl);
}

Layout:

<ImageView
    android:id="@+id/image_holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:layout_below="@id/profile_container"

    bind:imageUrl="@{item.imageUrl}"

    tools:src="@drawable/placeholder_image"/>

where namespace "app" was replaced to "bind". On your Layout root:

 xmlns:bind="http://schemas.android.com/apk/res-auto"
0
Abdo Driowya On

You are using app:imageUrl="@{item.imageUrl}" in the xml, make sure you have a String called imageUrl in your model.

You must pass the url link of the image and not the binderAdapter's name.

Syntax :

app:BinderAdapterName="@{item.imagelink}"
0
Milad Ahmadi On

Add the following line to your app's build.Gradle file:

apply plugin: 'kotlin-kapt'

and enable data-binding in the android block in the app's build.Gradle file:

android {
.
.
buildFeatures {
    dataBinding true
}
.
.
}

create BindingAdapters.kt file and add @BindingAdapter with below syntax:

@BindingAdapter("imageUrl")
  fun ImageView.bindImage(imgUrl: String){
  Glide.with(context)
  .load(imgUrl)
  .into(this)
  }

in view's xml use imageUrl='@{"YourImageUrl"}' syntax in the ImageView:

 <ImageView
 android:id="@+id/imageView3"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:background="@drawable/img"
 imageUrl='@{"YourImageUrl"}'/>

This works well ;)

My project Gradle version: 7.0.0

2
Swapnil On

I encountered the same problem, I missed to bind layout using:

DataBindingUtil.setContentView(activity, layoutResId);
0
Sunil Kumar S C On

I had same issue and none of the other solutions worked for me. Then this worked.

In activity

dataBinding.lifecycleOwner = this
0
kosiara - Bartosz Kosarzycki On

Remember to add the following line to your app's build.gradle file:

apply plugin: 'kotlin-kapt'

and check @BindingAdapter syntax:

@BindingAdapter("visibleOnScreen")
fun View.setVisibility(isVisible: ObservableBoolean) {
    if (isVisible.get())
        this.visibility = View.VISIBLE
    else
        this.visibility = View.GONE
}

in view's xml:

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:visibleOnScreen="@{viewModel.errorOccurred}" />

viewModel:

var errorOccurred = androidx.databinding.ObservableBoolean(false)
0
Ravi Kumar On

I just placed buildFeatures block after compileOptions and kotlinOptions in android block of build.gradle(:app). It worked

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
buildFeatures {
 databinding true
}