activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="callback"
type="com.buscom.ActionCallBack" />
</data>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_oml"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_50"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{(v) -> callback.onClick(v)}"
android:text="Menu" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
</layout>
ActionCallBack.java
This is the interface I implement in MainActivity
public interface ActionCallback {
void onClick(View view);
}
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
actionCallBack = new ActionCallBack() {
@Override
public void onClick(View view) {
System.out.println("Call onclick method *****");
}
}
}
When i click on button onClick() method is not evoked, noting is shown in output or no action performed. But is works in the traditional way with onClickListener
The databinding library is bundled with the Android Gradle plugin. You do not need to declare a dependency on the library, but you must enable it. To enable data binding, set the dataBinding build option to true in your module's build.gradle file, as shown below:
The data is provided by a ViewModel. Model-View-ViewModel is a presentation layer pattern that works very well with Data Binding. Here's a diagram: If you're not yet familiar with the ViewModel class from the Architecture Components libraries, you can check out the official documentation.
Note: You must enable data binding for all modules that depend on libraries that use data binding, even if the module doesn't directly use data binding. For more information about data binding, see the guide to the data binding library , and the Android Studio release notes Your feedback helps make Jetpack better.
To enable data binding, set the dataBinding build option to true in your module's build.gradle file, as shown below: Note: You must enable data binding for all modules that depend on libraries that use data binding, even if the module doesn't directly use data binding.
I think there is a mistake in your Activity declaration. Anyhow, you are still not setting your callback, as such:
binding.setCallback(this);
or
binding.setCallback(actionCallback);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With