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) { ... }
- }
Assign an event to a view with binding method, as shown below:
- <?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>
This will not work until you set the handler in the binding class in your activity or fragment.
- binding.setHandler(this);
Pass custom arguments with onClick, using lambda expressions.
- public interface MyClickListener
- {
- public void onButtonClick1(User user);
- }
Pass the whole model class from your view onClick.
- <?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>
It will pass the whole model class with the values to the listener method.
Import pre-defined class
- <data>
- <import type="android.view.View"/>
- </data>
Use it in your layout to hide/show view, as shown below,
- <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
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 ? :
- android:text="@{`Hello ` + user.firstName}"
- android:visibility="@{user.gender==0 ? View.VISIBLE : View.GONE}"
- android:text="@{user.age > 18 ? `Adult` : `Child`}"
Null Coalescing Operator
- 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,
- android:text="@{user.displayName != null ? user.displayName : (user.firstName + ` ` + user.lastName)}"
Referencing other view’s property
- <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
You can access resources also, it can be anything like a string, drawable, or dimen:
- android:padding="@{user.age > 18 ? @dimen/adultPadding : @dimen/childPadding}"
- android:hint="@{@string/firstName}"
This article was originally posted by me
here.