Advanced Data Binding In Android

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.   
  11.        <Button android:layout_width="wrap_content"  
  12.            android:layout_height="wrap_content"  
  13.            android:text="@{user.firstName}"  
  14.            android:onClick="@{handler.onClickButton1}"/>  
  15.         
  16.    </LinearLayout>  
  17. </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.   
  4.     <data>  
  5.         <variable  
  6.             name="handler"  
  7.             type="com.androidgig.MyClickListener"/>  
  8.         <variable  
  9.             name="user"  
  10.             type="com.androidgig.advancedatabinding.User"/>  
  11.     </data>  
  12.   
  13.     <RelativeLayout  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="match_parent">  
  16.   
  17.         <Button  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:onClick="@{() -> handler.onButtonClick1(user)}" />  
  21.     </RelativeLayout>  
  22. </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:  
  • Mathematical + – / * %
  • String concatenation +
  • Logical && ||
  • Binary & | ^
  • Unary + – ! ~
  • Shift >> >>> <<
  • Comparison == > < >= <=
  • instanceof
  • Grouping ()
  • Literals – character, String, numeric, null
  • Cast
  • Method calls
  • Field access
  • Array access [ ]
  • Ternary operator ? :
 
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.   
  6. <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         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.


Similar Articles