Android Data Binding pass arguments to onClick method

Multiple Ways:

Solution 1 (When you have Presenter)

You have a presenter or handler class like below

public class Presenter {
    public void onSaveClick(View view, Task task){}
}

Now take variable of type Presenter in your layout. and set click like below.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="task" type="com.android.example.Task" />
        <variable name="presenter" type="com.android.example.Presenter" />
    </data>
    <LinearLayout ...>
        <Button 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:onClick="@{(view) -> presenter.onSaveClick(view, task)}"
           />
    </LinearLayout>
</layout>

You can add any more arguments in this.

Please note that, you have to set these data variables in the onCreate() method. Like below

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setTask(task);
binding.setPresenter(presenter);

Solution 2 (When you don't have Presenter)

Create a variable type of your Activity / Fragment

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="task" type="com.android.example.Task" />
        <variable name="activity" type="com.example.MainActivity" />
    </data>
    <LinearLayout ...>
        <Button 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:onClick="@{(view) -> activity.onSaveClick(view, task)}"
           />
    </LinearLayout>
</layout>

and make method in your activity which you want to call from binding layout. and don't forget to call setActivity(YourActivity.this) from Activity.

public class MainActivity extends AppCompatActivity {
    public void onSaveClick(View view, Task task) {
        System.out.println("MainActivity.onSaveClick");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setActivity(this);
        binding.setTask(task);
    }
}

Further reading Android DataBinding documentation

There are many other ways to set click, check this answer.


You can use a lambda expressions and pass the view in as a parameter.

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

If you need the view, you can pass that as well with:

 android:onClick="@{(view) -> callback.onCategoryClick(view, viewModel)}"