Introduction

I have posted basic blogs about data binding in Android, you can check them first to get started:
  1. Working with data binding in Android
  2. Image loading with data binding in Android
  3. Two-way data binding in Android
  4. Setting custom font through XML with data binding

Binding Events

Events will be bound to the handler methods directly, as we used to do it with Android:onClick. The only difference here will be is that we have to take a reference of our interface(handler).
  1. public class MyEventListener
  2. {
  3. public void onClickButton1(View view) { ... }
  4. public void onClickButton2(View view) { ... }
  5. }
Assign an event to a view with binding method, as shown below:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android">
  3. <data>
  4. <variable name="handler" type="com.androidgig.MyEventListener"/>
  5. </data>
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <Button android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="@{user.firstName}"
  13. android:onClick="@{handler.onClickButton1}"/>
  14. </LinearLayout>
  15. </layout>
This will not work until you set the handler in the binding class in your activity or fragment.
  1. binding.setHandler(this);
Pass custom arguments with onClick, using lambda expressions.
  1. public interface MyClickListener
  2. {
  3. public void onButtonClick1(User user);
  4. }
Pass the whole model class from your view onClick.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android">
  3. <data>
  4. <variable
  5. name="handler"
  6. type="com.androidgig.MyClickListener"/>
  7. <variable
  8. name="user"
  9. type="com.androidgig.advancedatabinding.User"/>
  10. </data>
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent">
  14. <Button
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:onClick="@{() -> handler.onButtonClick1(user)}" />
  18. </RelativeLayout>
  19. </layout>
It will pass the whole model class with the values to the listener method.
Import pre-defined class
  1. <data>
  2. <import type="android.view.View"/>
  3. </data>
Use it in your layout to hide/show view, as shown below,
  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="@{user.firstName}"
  5. android:visibility="@{user.firstName.length()>0 ? View.VISIBLE : View.GONE}"/>

Expression Language

The expression language looks a lot like a Java expression. These are the same, as shown below:
Example
  1. android:text="@{`Hello ` + user.firstName}"
  2. android:visibility="@{user.gender==0 ? View.VISIBLE : View.GONE}"
  3. android:text="@{user.age > 18 ? `Adult` : `Child`}"
Null Coalescing Operator
  1. android:text="@{user.displayName ?? (user.firstName + ` ` + user.lastName)}"
If user.displayName is null, it will set firstName and lastName as the text, else displayName it is equivalent to, as shown below,
  1. android:text="@{user.displayName != null ? user.displayName : (user.firstName + ` ` + user.lastName)}"
Referencing other view’s property
  1. <Button android:id="@+id/btn_submit"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:visibility="@{user.displayName!=null ? View.VISIBLE : View.GONE}"/>
  5. <TextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:visibility="@{btnSubmit.visibility}"/>

Referencing Resources

You can access resources also, it can be anything like a string, drawable, or dimen:
  1. android:padding="@{user.age > 18 ? @dimen/adultPadding : @dimen/childPadding}"
  2. android:hint="@{@string/firstName}"
This article was originally posted by me here.