Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to make click event in android data binding

View.Java

package com.espresso.mvvmtestproject;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.espresso.mvvmtestproject.databinding.ActivityMainBinding;


public class View extends AppCompatActivity implements ViewContract.requiredMethods{


    ViewModel mModel;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Setting the layout
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mModel=new ViewModel(getContext());
        binding.setUser(mModel);
        binding.setHandlers(new MyHandlers());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public Context getContext() {
        return View.this;
    }


}

ViewContract.java

public interface ViewContract {
    interface requiredMethods{
        Context getContext();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="user"
            type="com.espresso.mvvmtestproject.ViewModel"/>
        <variable
            name="handlers"
            type="com.espresso.mvvmtestproject.MyHandlers"/>
    </data>




    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center">
        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:text="First Text"/>
        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:text="Second Text"/>

        <Button
            android:text="Show Toast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{handlers::onShowToast}"/>
    </LinearLayout>

</layout>

MyHandlers.java

public class MyHandlers {

    public void onShowToast(View view) {
        Toast.makeText(view,"Clicked",Toast.LENGTH_SHORT).show();
    }

}

ViewModel.java

public class ViewModel {


    private Context mActivity;

    public ViewModel(Context context) {
        this.mActivity=context;
    }

    public void onShowToast(View view) {
        Toast.makeText(mActivity,"Clicked",Toast.LENGTH_SHORT).show();
    }

}

ErrorLog

Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources, :app:assembleDebug]
D:\Projects\Trp\MvvmTestProject\app\src\main\java\com\espresso\mvvmtestproject\View.java
Error:(8, 48) error: package com.espresso.mvvmtestproject.databinding does not exist
Error:(15, 5) error: cannot find symbol class ActivityMainBinding
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Listener class android.view.View.OnClickListener with method onClick did not match signature of any method handlers::onShowToast
file:D:\Projects\Trp\MvvmTestProject\app\src\main\res\layout\activity_main.xml
loc:40:31 - 40:51
****\ data binding error ****
Information:BUILD FAILED
Information:Total time: 4.396 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console
like image 249
Devrath Avatar asked Nov 08 '16 13:11

Devrath


People also ask

Which is better view Binding or data binding in Android?

ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.

How check data binding is null in Android?

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 .


2 Answers

In later versions of the DataBinding library you can just bind to your click event like this:

android:onClick="@{() -> viewModel.save()}"

Then, in your viewmodel you can just add a listener like this:

public void save(){
 ...
 }
like image 134
exkoria Avatar answered Sep 21 '22 10:09

exkoria


Make your MyHandlers interface from class.

public class MyHandlers {
    public void onShowToast(View view);
} 

Implement it in your Activity or Fragment, in your case it will be as follows

public class View extends AppCompatActivity implements ViewContract.requiredMethods, MyHandlers{

    ViewModel mModel;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Setting the layout
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mModel=new ViewModel(getContext());
        binding.setUser(mModel);
        binding.setHandlers(this);

    }        

    @Override
    public void onShowToast(View view) {
        Toast.makeText(view,"Clicked",Toast.LENGTH_SHORT).show();
    }
}

Override your interface method onShowToast and set the handler for your binding, and that's it, you are done with click events

like image 20
Ravi Avatar answered Sep 18 '22 10:09

Ravi