29

I am working on DataBinding with BindingAdapter. Here is my custom method.

@BindingAdapter("{bind:fadevisible}")
public static void setFadeVisible(LinearLayout view, int visible) {
    Log.e("Bindings", "setFadeVisible: ");
}

And in xml file i am calling it like

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:fadevisible="@{1}"/>

But it is showing error

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'app:fadevisible' with parameter type int on android.widget.LinearLayout. file:\app\src\main\res-main\layout\activity_detail.xml loc:236:31 - 236:54 ****\ data binding error ****

I have checked this and this thread but somehow it is not helping me, as you can see i am passing int from xml and in BindingAdapter also i have mentioned LinearLayout with int value.

Even i have another method, where just parameters are different and its working fine

@BindingAdapter({"bind:image_round"}) 
public static void loadRoundImage(ImageView imageView, String url)
Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183

8 Answers8

24

Make sure in app level gradle, you have apply plugin: 'kotlin-kapt'

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
20

Your @BindingAdapter definition looks a little bit odd to me

@BindingAdapter("{bind:fadevisible}")

This is not the same like

@BindingAdapter({"bind:fadevisible"})

or

@BindingAdapter("bind:fadevisible")

which should work perfectly fine.

tynn
  • 38,113
  • 8
  • 108
  • 143
12

I had this problem with binding to ImageView and unlike your case, the definition of my binding adapter was correct but still, the IDE kept giving me this error message. After spending many hours on searching for the cause, I figured that the namespace that I use in xml layout file needs to be exactly what I declared in @BindingAdapter.

So, if my xml is like below:

<ImageView
    android:id="@+id/logo"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    app:image_url="@{item.logoUrl}"
/>

Then my binding method should be as below:

@BindingAdapter({"app:image_url"})
public static void loadImage(ImageView view, String logoUrl) {
    if (logoUrl == null) {
        view.setImageResource(R.drawable.ic_place_holder);
    } else {
        Glide.with(getContext()).load(logoUrl).crossFade().into(view);
    }
}

Note that binding method annotation indicates the namespace in it , i.e. @BindingAdapter({"app:image_url"}) exactly as it is used in layout file app:image_url="@{item.logoUrl}"

So unlike what is said in most tutorials, don't use @BindingAdapter({"bind:image_url"}) in your binding method and app:image_url="@{item.logoUrl}" in your xml file.

Akram
  • 2,158
  • 1
  • 17
  • 24
4

You try

 @BindingAdapter("bind:fadevisible")
Emre Aslan
  • 113
  • 1
  • 8
  • but i have another method `@BindingAdapter({"bind:image_round"}) public static void loadRoundImage(ImageView imageView, String url) ` and it is working perfectly without any error. – Ravi Oct 22 '16 at 06:26
  • you try @BindingAdapter("app:fadevisible") – Emre Aslan Oct 22 '16 at 06:29
  • `@BindingAdapter("bind:fadevisible")` worked, but can you please explain why `@BindingAdapter({"bind:fadevisible"})` didn't work ?? – Ravi Oct 22 '16 at 07:46
  • @RaviRupareliya i tried your code no changes and it works for me. In fact i tried the same for a button in the same layout. works fine. – Raghunandan Oct 22 '16 at 07:49
  • @Raghunandan yes that is what i am thinking, it should work, even i have tried with clean and rebuild, invalidate caches and restart but don't know what's the issue. – Ravi Oct 22 '16 at 07:51
1

I had initially set defined my customBindidingAdapter as private:

@BindingAdapter("setPriorityColor")
private static void getPriorityColor(TextView textView, int priority) {
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
edafe
  • 76
  • 1
  • 4
1

Add on to the answers if you are working on multiple modules then where you have

 @BindingAdapter("fadevisible")

That module should have the following code in the module -> build.gradle.

dataBinding {
    enabled = true
}

Enjoy Happy coding. :)

Sachin Mandhare
  • 700
  • 2
  • 7
  • 20
1

In my particular case, my BindingAdapter had two parameters, with requireAll, and I had neglected to put one of them on the element in my layout XML. So, like this: (Kotlin, I know)

@BindingAdapter("app:arg1", "app:arg2", requireAll = true)
fun MyAdapter(view: ImageView, x: String, y: Int) {
    // ...
}
<Element app:arg1="@{"foo"}"/>

The error was roughly Cannot find the setter for attribute "app:arg1" with parameter String which is perfectly true, there is no such adapter; there's only one for two args.

One hint that this was happening was that Android Studio indicated that MyAdapter was an unused function by coloring it grey.

Obviously a more eloquent error message like "there is no adapter for app:arg1 of type String but there is one for..." (when one of the attribute names matches) would be appreciated, but I won't hold my breath.

Matt Mc
  • 8,882
  • 6
  • 53
  • 89
-1

Apart from @BindingAdapter improvements (mine were working fine in one build and not in another), upgrading the Build gradle version to the latest one worked for me.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Ana
  • 1