Databinding and included layouts: Cannot find setter attribute for onClick - android

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>

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 IncludedLayouts data binding class, and then assigning the OnClickListener to IncudedLayouts root view in XML.
After the changes, my files looked like this:
viewModel
class MyViewModel() {
val onClickFunction: OnClickListener = object:OnClickListener{
override fun onClick(v: View?) {
//Do something
}
}
}
Including layout
<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>
Included 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>

include tag does not support onClick method directly. While the selected answer is correct, instead of passing onClickLister to include layout (or having custom #BindingAdapter, which is also another solution), I would just wrap my include inside a ViewGroup and onClick on ViewGroup.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{()-> viewModel.yourFunction()}">
<include layout="#layout/custom_layout"/>
It's a workaround, but works as charm.

Related

Android MVVM ClickListener best practice

As I read sunflower and many projects we have the multi clean ways for implementing a ClickListener
Binding clicklistener in view (Activity/fragment)
Creating a separated variable for clickListener on XML and call it on the constructor
Creating a static method and calling it from XML
Creating a viewModel with two uses (model and method) and passing the ViewModel class directly to XML and calling the method on our object
1
binding.addPlant.setOnClickListener {
navigateToPlantListPage()
}
2
<data>
<variable
name="clickListener"
type="android.view.View.OnClickListener"/>
<variable
name="plant"
type="com.google.samples.apps.sunflower.data.Plant"/>
</data>
then
init {
binding.setClickListener { view ->
binding.viewModel?.plantId?.let { plantId ->
navigateToPlant(plantId, view)
}
}
}
3
companion object{
#JvmStatic
#BindingAdapter("bind:setSubjectText")
fun setSubjectText(textView: TextView, string: String){
val mTxt = "Subject: ${string}"
textView.text = mTxt
}
}
4
<data>
<variable
name="viewmodel"
type="com.android.example.livedatabuilder.LiveDataViewModel" />
</data>
as a model
<TextView
android:id="#+id/current_weather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{viewmodel.currentWeather}"
tools:text="Tokyo" />
in the same XML as a ViewModel method
<Button
android:id="#+id/refresh_button"
android:layout_width="match_parent"
android:layout_height="56dp"
android:onClick="#{() -> viewmodel.onRefresh()}"
android:text="#string/refresh_label" />
As you see each of them has some pros and cons. For instance for each theme:
Messy view
When we want to call multi-object in XML, the XML will be messy
In the large-scale program, we will engage with many static methods
Messy ViewModel
Question: which of them is the best practice for implementing clicklistener, especially in large-scale programs with some fragments by many clickable objects?
In my opinion, none of the methods you mentioned were not good anymore, and any new project could use Jetpack Compose. In this fashion, you do not need fragments, XMLs, binding. etc

NotFoundException: String resource ID #0x0 when binding string resource

I'm currently using bindings to dynamically set the texts of various text views using the android view models. At the moment the view models look something like this:
class MyViewModel(
resources: Resources,
remoteClientModel: Model = Model()
) : ObservableViewModel() {
init {
observe(remoteClientModel.liveData) {
notifyChange()
}
fun getTextViewTitle(): String = when {
someComplicatedExpression -> resources.getString(R.string.some_string, null)
else -> resources.getString(R.string.some_other_string)
}
}
And the xml layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<variable
name="viewModel"
type="my.app.signature.MyViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{viewModel.textViewTitle}"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
However I would like to remove the "resources: Resources" that is injected into the view model, since the resources are coupled with the Activity. The code now simply returns the string resource id instead:
fun getTextViewTitle(): Int = when {
someComplicatedExpression -> R.string.some_string
else -> R.string.some_other_string
}
Hence I've removed the activity dependency. The compiler thinks this is fine but it crashes in runtime with the following exception: android.content.res.Resources$NotFoundException: String resource ID #0x0.
This happens when trying to attach the lifeCycleOwner to the binding using:
override fun onActivityCreated(savedInstanceState: Bundle?) {
// Some more code....
binding.lifecycleOwner = activity
// Some more code....
I'm not sure how to remove the resource dependency from the view model without having it crash in runtime.
EDIT:
For clarification: The ObservableViewModel in my example is the very same one as the one found here:
https://developer.android.com/topic/libraries/data-binding/architecture
Used to perform notifyChange.
The issue here is the code is trying to call textView.setText(0) which results in an error since there is no string resource with id 0x0. This is happening because getTextViewTitle() return an Int and the view binding functionality will make it default as 0 (when initializing).
https://developer.android.com/topic/libraries/data-binding/expressions#property_reference
From the docs
Avoiding null pointer exceptions
Generated data binding code automatically checks for null values and avoid null pointer exceptions. For example, in the expression #{user.name}, if user is null, user.name is assigned its default value of null. If you reference user.age, where age is of type int, then data binding uses the default value of 0.
Maybe something like this could work,
android:text='#{viewModel.textViewTitle == 0 ? "" : #string{viewModel.textViewTitle}}'
or
android:text='#{viewModel.textViewTitle, default=""}'
To solve this simply make a context available in the view, so that you can call context.getString(...) in your view.
<data>
<import type="androidx.core.content.ContextCompat" />
<variable
name="viewModel"
type="my.application.path.SomeViewModel" />
</data>
<....
....
android:text="#{context.getString(viewModel.textResource)}"
...
/>
Just convert your int value to String to avoid this crush
android:text='#{String.valueOf(viewModel.profile.walletBalance)}'
In some cases your binding variable itself can be null
<data>
<variable
name="viewModel"
type="SomeViewModel"
/>
</data>
<TextView
android:text="#{viewModel == null ? "" : viewModel.textViewTitle}"
/>

Data Binding Compiler cannot find custom binding adapter provided

I have made a binding adapter available statically inside my Fragment which basically change my button appearance from "Stop" to "Play" and vice-versa.
companion object {
#BindingAdapter("playState")
fun Button.setPlayState(item: UIState) {
item.let {
if (it.isPlaying) {
setText("Stop")
setBackgroundColor(ContextCompat.getColor(context, R.color.colorStop))
} else {
setText("Play")
setBackgroundColor(ContextCompat.getColor(context, R.color.colorPlay))
}
}
}
}
Here is my layout file. I have provided a data class for it.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<!-- stuff here -->
<variable
name="viewmodel"
type="com.mypackage.ui.ViewModel"/>
<variable
name="uistate"
type="com.mypackage.ui.UIState" />
</data>
<!-- layout, buttons, and more stuff here. Just pay attention to this following button -->
<Button
android:id="#+id/play_button"
android:layout_width="150sp"
android:layout_height="75sp"
android:layout_marginTop="20sp"
android:onClick="#{() -> viewmodel.onPlayClicked()}"
android:text="#string/play_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/minus_layout"
app:layout_constraintVertical_bias="0.026"
app:playState="#{uistate}"/>
</layout>
UIState itself is pretty self-explanatory.
data class UIState(var isPlaying: Boolean)
and the () -> viewmodel.onPlayClicked() flips the Boolean at UIState.
After compiling, Data Binding Compiler throws this error:
Cannot find a setter for <android.widget.Button app:playState>
that accepts parameter type 'com.mypackage.ui.UIState'
I have tried:
Rebuilding the project by removing .gradle folder
Looking for answer here and here.
Removed #JvmStatic annotation at the extension function
Moved the extension function to top level instead of Fragment's companion object.
I think you missed to add kotlin plugin in your gradle
apply plugin: 'kotlin-kapt'
You don't have to use #JvmStatic because you are using Kotlin extension feature.
You need to add the view reference as a paramater to your BindingAdapter method.
#BindingAdapter("playState")
fun setPlayState(button:Button,item: UIState) {
//do your work here
}
Your namespace
xmlns:app="http://schemas.android.com/apk/res-auto"
is wrong for custom binding adapters. Please use the namespace
xmlns:app="http://schemas.android.com/tools"
since app:playState is not in the namespace you have given its not working properly

Cannot define setOnRefreshListener for SwipeRefreshLayout

I'm trying to assign a behavior different than the standard one when my swipelayout is refreshed:
This is my code:
binding.refreshed = binding.refreshOtrosRequestList
binding.refreshOtrosRequestList!!.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener {
[Change behavior]
})
These are the elements I'm using defined in the corresponding XML file:
<data>
<variable name="refreshed" type="androidx.swiperefreshlayout.widget.SwipeRefreshLayout"/>
</data>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refreshOtrosRequestList"
app:onRefreshListener="#{() -> model.onRefresh(refreshed)}"
android:layout_width="wrap_content"
android:layout_height="match_parent">
But the defined setOnRefreshListener never fires:
What am I doing wrong?
PD: this is request, also in data in XML file:
<data>
<variable name="model" type="es.nscontrol.controlpresencial.viewmodels.NonWorkingDateViewModel"/>
<variable name="refreshed" type="androidx.swiperefreshlayout.widget.SwipeRefreshLayout"/>
</data>
What is "model"?
model.onRefresh(refreshed)
Is it somewhere defined?
Additionally, it looks strange when you use databiding to store view from this layout in a variable.
Edit:
I hope you also binded this "model" to the actual variable as you did with
binding.refreshed = binding.refreshOtrosRequestList
And invoke this after you binded these variables:
binding.executePendingBindings()

How to use kotlin.Int in databinding?

I wrote a layout xml like below.
But the Kotlin compiler says Cannot resolve symbol 'Int'
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout ...>
<data>
<import type="androidx.databinding.ObservableArrayMap" />
<variable
name="myList"
type="ObservableArrayMap<Int,String>" />
</data>
<!-- ...... -->
</layout>
Is it possible to use kotlin builtins in android databinding xml?
Use java Integer instead of kotlin Int.
You can not use characters <,> etc in XML. So use HTML entities.
like
ObservableArrayMap<Integer,String>
You can add data binding simple way.
Configure your app to use the data binding:
Open the app/build.gradle, Then you have to add these line of code inside the android tags in gradle and sync project
dataBinding {
enabled = true
}
Layout and Binding Expression in Data Binding:
Open the layout (XML) file activity_main and replace the root with layout tag and place all tags inside to layout tags.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout>
<layout>
Replace the traditional setContentView() to DataBindingUtil.setContentView() in Activity:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
}
Let’s us see how to data bind with Views and Widget using data binding
binding.tvName.setText("Monika Sharma");
binding.tvAddress.setText("251 mansarovar Jaipur | India ");
binding.tvFollowers.setText("240K");
binding.tvfollowing.setText("324K");

Categories

Resources