Android Data Binding Part 2
We have seen basics of Data binding which covers following points:
- How to set up data binding in Android?
- How data binding makes findViewById obsolete?
- How to bind data in layout file?
If you haven’t gone through it then please check following link:
In this tutorial, we will see following points in Data Binding:
- How can we change binding class name?
- Event handling with Method references & Lambda expressions
- Expression language of Data binding
Let’s start with first one,
How can we change binding class name?
- By default, binding class is generated based on the name of the layout file and placed in a data binding package under the module package. E.g. the layout file
activity_login.xml
will generateActivityLoginBinding
. If the module package iscom.androidbytes.databindingdemo
, then it will be placed incom.androidbytes.databindingdemo.databinding
directory. - However, most of the developers prefer that generated binding name should be
LoginActivityBinding
. So, it will be more readable. - Let’s see, how we can rename binding class. All we have to do is adjusting class attribute of the data tag in layout file. Like following code:
- Now generated binding class is named
LoginActivityBinding
. - And we can also change default package of generated binding class like following code:
- It will generate binding class in
com.androidbytes.databindingdemo
package. Pretty simple, right? Let’s see event handling.
Event handling with Method references & Lambda expressions
Data binding also provides a way to bind events of view. View has some attributes which match with there event methods like View.OnClickListener
has a onClick()
method, View.OnLongClickListener
has a onLongClick()
method. So we can set bind view’s events from attributes like android:onClick, android:onLongClick
and many more.
We can bind events using Method references and Listener binding. Check following layout file:
And check LoginHandler
class which has methods which are directly bound to layout file.
Let’s see what’s going on in activity_login.xml
file.
Step 1: Here, we have import android View
class. Yes, you read it right. Data binding provides a way to import Android and Java class. So, we can use its static properties. Like, we have used in View.VISIBLE
and View.INVISIBLE
in Step 6.
Step 2: @{loginHandler::setPasswordVisible}
is an example of event binding with Method reference. We have bound a method of checkbox’s onCheckedChanged
with handler’s setPasswordVisible.
Signature of reference method will be same as signature of the listener method. Means here, setPasswordVisible
has a same signature as onCheckedChanged.
Step 3: It’s an example of event binding with Lambda expression. Whenever event is dispatched, listener evaluates the lambda expression and invoke bound method. In our case, button click will call handler’s performLogin
method. That’s it in event handling. Now see 3rd and last point Expression language.
Expression language of Data binding
Expression language of Data binding almost looks like Java expression. We have also implemented in our activity_login.xml
like:
Step 4: I know I know, you already got it. It’s ternary operator expression in layout file.
Step 5: It’s logical AND operator in layout file.
There are so many other operators which you can use in layout. There is one operator named Null Coalescing Operator ??
which I would like to share:
You can check more on operators on Android Developer.
From my personal experience, I would not recommend any type of business or presentation logic or any complex expressions inside in layout file because it make code less readable and maintainable.
I hope you liked the article. Feel free to share any feedback. And stay tuned for more on Data Binding!
Don’t forget to follow me on Medium and Twitter. If you liked the article, click the heart below so more people can see it! Thanks for reading!