Introduction
I have posted basic blogs about data binding in Android, you can check them first to get started:
- Working with data binding in Android
- Image loading with data binding in Android
- Two-way data binding in Android
- 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).
- public class MyEventListener
- {
- public void onClickButton1(View view) { ... }
- public void onClickButton2(View view) { ... }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android">
- <data>
- <variable name="handler" type="com.androidgig.MyEventListener"/>
- </data>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{user.firstName}"
- android:onClick="@{handler.onClickButton1}"/>
- </LinearLayout>
- </layout>
- binding.setHandler(this);
- public interface MyClickListener
- {
- public void onButtonClick1(User user);
- }
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android">
- <data>
- <variable
- name="handler"
- type="com.androidgig.MyClickListener"/>
- <variable
- name="user"
- type="com.androidgig.advancedatabinding.User"/>
- </data>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="@{() -> handler.onButtonClick1(user)}" />
- </RelativeLayout>
- </layout>
- <data>
- <import type="android.view.View"/>
- </data>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{user.firstName}"
- android:visibility="@{user.firstName.length()>0 ? View.VISIBLE : View.GONE}"/>
Expression Language
- Mathematical + – / * %
- String concatenation +
- Logical && ||
- Binary & | ^
- Unary + – ! ~
- Shift >> >>> <<
- Comparison == > < >= <=
- instanceof
- Grouping ()
- Literals – character, String, numeric, null
- Cast
- Method calls
- Field access
- Array access [ ]
- Ternary operator ? :
- android:text="@{`Hello ` + user.firstName}"
- android:visibility="@{user.gender==0 ? View.VISIBLE : View.GONE}"
- android:text="@{user.age > 18 ? `Adult` : `Child`}"
- android:text="@{user.displayName ?? (user.firstName + ` ` + user.lastName)}"
- android:text="@{user.displayName != null ? user.displayName : (user.firstName + ` ` + user.lastName)}"
- <Button android:id="@+id/btn_submit"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="@{user.displayName!=null ? View.VISIBLE : View.GONE}"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="@{btnSubmit.visibility}"/>
Referencing Resources
- android:padding="@{user.age > 18 ? @dimen/adultPadding : @dimen/childPadding}"
- android:hint="@{@string/firstName}"

Ravi KandelPosted Jul 14, 2016, 12:10 PM
Thanks for sharing.
Santhakumar MunuswamyPosted Jun 25, 2016, 1:04 AM
Thank you for nice article
GokulPosted Jun 23, 2016, 3:13 AM
Thanks for sharing
Kuppurasu NagarajPosted Jun 21, 2016, 12:40 PM
Nice Sharing..
Debasis SahaPosted Jun 21, 2016, 12:47 AM
Good One..
kalu singh raoPosted Jun 20, 2016, 2:00 PM
Nice...