Advance DataBinding in Android
In earlier posts we have already seen
- Working with DataBinding Android
- Image Loading with DataBinding in Android
- Two-way DataBinding in Android
- Setting custom font through XML with DataBinding
Today we will discuss more about Advance DataBinding
Binding Events :
Events will be bound to handler methods directly, like we used to do it with android:onClick. Only change will be we have to take reference of our interface(handler).
1 2 3 4 |
public class MyEventListener { public void onClickButton1(View view) { ... } public void onClickButton2(View view) { ... } } |
Assign event to a view with binding method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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 handler in binding class in your Activity or Fragment.
1 |
binding.setHandler(this); |
Passing custom arguments with onClick using lambda expression.
1 2 3 |
public interface MyClickListener { public void onButtonClick1(User user); } |
Pass whole model class from your view onClick.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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 whole model class with values to listener method.
Import pre-defined class
1 2 3 |
<data> <import type="android.view.View"/> </data> |
use it in your layout to hide/show view
1 2 3 4 5 |
<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:
- Mathematical + – / * %
- String concatenation +
- Logical && ||
- Binary & | ^
- Unary + – ! ~
- Shift >> >>> <<
- Comparison == > < >= <=
- instanceof
- Grouping ()
- Literals – character, String, numeric, null
- Cast
- Method calls
- Field access
- Array access [ ]
- Ternary operator ? :
you can refer this for more details on this.
Example
1 2 3 |
android:text="@{`Hello ` + user.firstName}" android:visibility="@{user.gender==0 ? View.VISIBLE : View.GONE}" 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 text, else displayName, it is equivalent to
1 |
android:text="@{user.displayName != null ? user.displayName : (user.firstName + ` ` + user.lastName)}" |
Referencing other view’s property
1 2 3 4 5 6 7 8 9 |
<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 string, drawable, dimen…
1 2 |
android:padding="@{user.age > 18 ? @dimen/adultPadding : @dimen/childPadding}" android:hint="@{@string/firstName}" |
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019