Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

databinding button onclick not working

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

like image 890
Eugene Avatar asked Jan 30 '17 14:01

Eugene


People also ask

How do I use the databinding library in Android?

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:

Where does the data come from in data binding?

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.

Do I need to enable data binding in jetpack?

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.

How do I enable data binding in Gradle?

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.


1 Answers

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);

like image 76
Chisko Avatar answered Sep 18 '22 14:09

Chisko