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:
class MyViewModel() {
val onClickFunctionAsAListener: OnClickListener = object:OnClickListener{
override fun onClick(v: View?) {
//Do something
}
}
fun onClickFunction() {
//Do something
}
}
<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>
<layout>
<data>
<variable name="customAttribute" type="String"/>
</data>
<TextView
layout="@layout/included_layout"
android:text="@{customAttribute}"/>
</layout>
It seems that you can't actually assign an OnClick handler to an <include>
tag directly. I managed to get it to work by adding another variable to IncludedLayout
s data binding class, and then assigning the OnClickListener to IncudedLayout
s root view in XML.
After the changes, my files looked like this:
class MyViewModel() {
val onClickFunction: OnClickListener = object:OnClickListener{
override fun onClick(v: View?) {
//Do something
}
}
}
<layout>
<data>
<variable name="viewModel" type="full.package.name.MyViewModel"/>
</data>
<LinearLayout>
<include
layout="@layout/included_layout"
app:onClick="@{viewModel.onClickListener}"
app:customAttribute="@{`someText`}/>
</LinearLayout>
</layout>
<layout>
<data>
<variable name="customAttribute" type="String"/>
<variable name="onClick" type="android.view.View.OnClickListener"/>
</data>
<TextView
layout="@layout/included_layout"
android:text="@{customAttribute}"
android:onClick="@{onClick}"/>
</layout>