1

I have a separate layout that I want to call onClick and update a field on callback

<include
    android:onClick="@{() -> viewModel.changeItem(2)}"
    layout="@layout/item"
    app:attr="@{viewModel.title}"
    app:desc="@{viewModel.description}"
    app:active="@{viewModel.isSelected}"
/>

But it returns the following binding error:

Cannot find the setter for attribute 'android:onClick' with parameter type lambda on com.X.databinding.ItemBinding.

But I can binding on other views

<TextView
    android:onClick="@{() -> viewModel.changeItem(1)}"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>
Iñigo
  • 1,877
  • 7
  • 25
  • 55
nima moradi
  • 2,300
  • 21
  • 34
  • Have you checked [this](https://stackoverflow.com/questions/47827685/cannot-find-the-setter-for-attribute-androidonclick-with-parameter-type-lambd)? – Piyush Jul 25 '19 at 12:49
  • i did, it's working completely if set in textView or button and etc – nima moradi Jul 25 '19 at 12:51
  • I think the problem is solved here: [enter link description here](https://stackoverflow.com/questions/52898833/databinding-and-included-layouts-cannot-find-setter-attribute-for-onclick) – ReneUhrin Aug 21 '19 at 17:30

1 Answers1

0

What I suggest you is to call the onClick directly from code.

Setup an ID for your view in your .xml, then set something like this :

val item = findViewById(R.id.your_id) etc.
item.onClick { functionYouWantToCall() }

However, I do not know if this is possible to set an ID or an onClickListener on an include layout.
If you can not do it, simply put your include inside a LinearLayout, then set your onClick on it.

I used to set onClicks in the .xml too, but I think it is much more efficient to set it in the code.
Moreover, I do not know if you can set arguments in a function you call from the xml.

Mathieu
  • 1,435
  • 3
  • 16
  • 35