I'm trying to set an OnClickListener
for an <include>
d layout, but receive a data binding error at compile time stating that data binding "Cannot find the setter for attribute 'android:onClick' with parameter type android.view.View.OnClickListener".
Context here is that I'm using data binding to inflate the included layout, so that I can pass values into it from a viewModel
that I've bound to the including layout.
I've tried various syntax for the data binding expression:
@{viewModel::onClickFunction}
@{viewModel.onClickFunction}
@{() -> viewModel.onClickFunction()}
@{(view) -> viewModel.onClickFunction()}
@{_ -> viewModel.onClickFunction()}
I've tried all of the above with onClickFunction
as a function, and also as an OnClickListener
object.
Other related questions on Stack Overflow seem to solve this issue by cleaning the project to regenerate the databinding files, but that hasn't worked for me.
Relevant code snippets below:
viewModel
class MyViewModel() {
val onClickFunctionAsAListener: OnClickListener = object:OnClickListener{
override fun onClick(v: View?) {
//Do something
}
}
fun onClickFunction() {
//Do something
}
}
Including layout
<layout>
<data>
<variable name="viewModel" type="full.package.name.MyViewModel"/>
</data>
<LinearLayout>
<include
layout="@layout/included_layout"
android:onClick="@{viewModel.onClickListener}"
app:customAttribute="@{`someText`}/>
</LinearLayout>
</layout>
Included layout
<layout>
<data>
<variable name="customAttribute" type="String"/>
</data>
<TextView
layout="@layout/included_layout"
android:text="@{customAttribute}"/>
</layout>