Faster Android development
with Data Binding
Android Fortnightly Series, July 2016
Slide 2
Slide 2 text
Segun Famisa
Software Engineer at Konga,KongaPay
@segunfamisa
segunfamisa.com
Slide 3
Slide 3 text
What is data binding?
Slide 4
Slide 4 text
What is data binding?
● Simply put: binding data, to your layouts.
● Changes in data are propagated to the
views, and vice versa.
● Does not necessarily use the `findViewById
()`
Slide 5
Slide 5 text
What is data binding?
Regular Way of accessing views
Slide 6
Slide 6 text
What is data binding?
So much boilerplate code
Data binding helps bring an end to that.
findViewById()
Slide 7
Slide 7 text
No content
Slide 8
Slide 8 text
Data binding in your Android
app
Slide 9
Slide 9 text
Data binding in Android
ButterKnife
Holdr
Data binding library
Slide 10
Slide 10 text
Data binding in Android
ButterKnife
Holdr
Data binding library
Slide 11
Slide 11 text
Data binding in Android
ButterKnife
Holdr
Data binding library
Holdr has been deprecated
Slide 12
Slide 12 text
Data binding in Android
ButterKnife
Holdr
Data binding library
Slide 13
Slide 13 text
Why use data binding?
Slide 14
Slide 14 text
Why use data binding?
Avoid boilerplate code
Efficiency
No Performance issues
findViewById()
Data binding makes the view id’s
not useful in code, so, no need to
findViewById.
Avoid those ugly looking
findViewById calls.
Slide 15
Slide 15 text
Why use data binding?
Avoid boilerplate code
Efficiency
No Performance issues
findViewById is an expensive call.
It traverses the UI hierarchy only
once.
Data binding improves efficiency of
by preventing numerous findViewById
calls
Slide 16
Slide 16 text
Why use data binding?
Avoid boilerplate code
Efficiency
No Performance issues
Data binding does not use
reflection.
Every processing happens at compile
time.
#PerfMatters
Slide 17
Slide 17 text
How to use data binding in
Android
Slide 18
Slide 18 text
How to use data binding in Android?
What do I need to use data binding?
● Android Studio 1.3+
● Gradle 1.5.0-alpha1 and above
● Download the library from the Support repository in the
Android SDK manager.
Slide 19
Slide 19 text
How to use data binding in Android?
Enable data binding in the android block of your app’s build.gradle
file
android {
...
dataBinding.enabled = true
}
Slide 20
Slide 20 text
Writing your first data
binding code
Slide 21
Slide 21 text
Your first data binding code
Make outer tag
Use DataBindingUtil
a. Activity
b. Fragment
Slide 22
Slide 22 text
Your first data binding code
Make outer tag
Use DataBindingUtil
a. Activity
b. Fragment
Slide 23
Slide 23 text
Your first data binding code
Make outer tag
Use DataBindingUtil
a. Activity
b. Fragment
Slide 24
Slide 24 text
Your first data binding code
Make outer tag
Use DataBindingUtil
a. Activity
b. Fragment
Slide 25
Slide 25 text
Your first data binding code
Make outer tag
Use DataBindingUtil
a. Activity
b. Fragment
Slide 26
Slide 26 text
Yasss, we did it. Let’s take it further!
Slide 27
Slide 27 text
Using variables
Slide 28
Slide 28 text
Using variables
1. Add tag
2. Set variable
Slide 29
Slide 29 text
Using variables
1. Add tag
2. Set variable
The user variable within data
describes a property that may be
used within this layout.
Slide 30
Slide 30 text
Using variables
1. Add tag
2. Set variable
Slide 31
Slide 31 text
Let’s take it further, let’s handle
events!
Slide 32
Slide 32 text
Add handler class &
method
Add handler variable to
tag
Bind handler to view in
code
Handling events - Method references
Slide 33
Slide 33 text
Add handler class &
method
Add handler variable to
tag
Bind handler to view in
code
Handling events - Method references
Slide 34
Slide 34 text
Add handler class &
method
Add handler variable to
tag
Bind handler to view in
code
Handling events - Method references
Slide 35
Slide 35 text
Add handler class &
method
Add handler variable to
tag
Bind handler to view in
code
Handling events - Method references
Slide 36
Slide 36 text
Layout details
Slide 37
Slide 37 text
Layout details
Imports
Expressions
Includes
Null coalescing
Layout details
Imports
Expressions
Includes
Null coalescing
Expressions that aren’t supported:
“new”
“this”
“super”
Slide 41
Slide 41 text
Layout details
Imports
Expressions
Includes
Null coalescing
● You can embed include tags within
your layout.
● Each include must have an id for
you to be able to access it from
code.
● An tag cannot be the
direct child of a tag
Slide 42
Slide 42 text
Layout details
Imports
Expressions
Includes
Null coalescing
Slide 43
Slide 43 text
Layout details
Imports
Expressions
Includes
Null coalescing
The expression resolves
to user.nickName if user.
name is null and user.
name otherwise
Slide 44
Slide 44 text
Under the hood
How data binding works?
Slide 45
Slide 45 text
How data binding in Android works
● Begin compilation
● Process layout files
○ Removing every data
binding-related stuff in
the layout.
● Parse expressions
● During compile, resolve
dependencies
● Find setters, and set
attributes
● Write data binding`
Begin Compilation
Process layout files
Parse Expressions
Resolve dependencies
Find setters
Write binders
Java Compilation
Slide 46
Slide 46 text
How data binding in Android works
Begin Compilation
Process layout files
Parse Expressions
Resolve dependencies
Find setters
Write binders
Java Compilation
Slide 47
Slide 47 text
How data binding in Android works
Begin Compilation
Process layout files
Parse Expressions
Resolve dependencies
Find setters
Write binders
Java Compilation
Removes all data binding-related
code in the layout
Slide 48
Slide 48 text
Attribute setters
Custom setters.
Using @BindingAdapter annotation, you can bind custom
attributes to static methods. TL;DR you can create your own
attributes.
Slide 49
Slide 49 text
Attribute setters
Custom setters.
Using @BindingAdapter annotation, you can bind custom
attributes to static methods.
Slide 50
Slide 50 text
Attribute setters
Custom setters.
Using @BindingAdapter annotation, you can bind custom
attributes to static methods.