Advance DataBinding in Android

In earlier posts we have already seen

  1. Working with DataBinding Android
  2. Image Loading with DataBinding in Android
  3. Two-way DataBinding in Android
  4. 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).

Assign event to a view with binding method

This will not work until you set handler in binding class in your Activity or Fragment.

Passing custom arguments with onClick using lambda expression.

Pass whole model class from your view onClick.

It will pass whole model class with values to listener method.

Import pre-defined class

use it in your layout to hide/show view

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.


Null Coalescing Operator

If user.displayName is null it will set firstName and lastName as text, else displayName, it is equivalent to

Referencing other view’s property

Referencing Resources

You can access resources also, it can be anything like string, drawable, dimen…

Follow me

Ravi Rupareliya

He is author of Android Gig. He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.
Ravi Rupareliya
Follow me
Live Templates in Android Studio
RecyclerView With Android DataBinding