The
Data Binding Library
Benoît Quenaudon
@oldergod
Slides: https://goo.gl/Pr4kjj
Slide 2
Slide 2 text
Problem with Android
Slide 3
Slide 3 text
Problem with Android
● findViewById, Cast
textView = (TextView) findViewById(R.id.text_view);
Slide 4
Slide 4 text
Problem with Android
● Setters are complicated and verbose
binding.date.setCompoundDrawablesWithIntrinsicBounds(
ContextCompat.getDrawable(this, R.drawable.ic_giants), null, null, null);
Slide 5
Slide 5 text
Problem with Android
● Manually keep track of UI updates
textView.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged() {}
@Override public void onTextChanged() {}
@Override public void afterTextChanged() {}
});
Slide 6
Slide 6 text
Problem with Android
● Not all setters in Java have XML equivalent
textView.setTypeface();
drawerLayout.setScrimColor();
Slide 7
Slide 7 text
Data Binding to the rescue
Slide 8
Slide 8 text
Data Binding Library ?
● A framework to connect your UI and model
○ Once or persistently
● One XML attribute for every Java setter
● Custom XML attributes
● Can override android:’s XML attributes
Slide 9
Slide 9 text
Data Binding Library ?
● A framework to connect your UI and model
○ Once or persistently
● One XML attribute for every Java setter
● Custom XML attributes
● Can override android:’s XML attributes
Slide 10
Slide 10 text
Data Binding Library ?
● A framework to connect your UI and model
○ Once or persistently
● One XML attribute for every Java setter
● Custom XML attributes
● Can override android:’s XML attributes
Slide 11
Slide 11 text
Data Binding Library ?
● A framework to connect your UI and model
○ Once or persistently
● One XML attribute for every Java setter
● Custom XML attributes
● Can override android:’s XML attributes
Slide 12
Slide 12 text
Data Binding Library ?
● A framework to connect your UI and model
○ Once or persistently
● One XML attribute for every Java setter
● Custom XML attributes
● Can override android:’s XML attributes
Slide 13
Slide 13 text
Before / After
github.com/oldergod/DataBindingDemo
Before: Java
showScoreView.setChecked(match.isShowScore());
After: XML
Slide 46
Slide 46 text
Before: Java
scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE);
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
scoreView.setVisibility(b ? VISIBLE : GONE);
}
After: XML
Slide 47
Slide 47 text
Before: Java
scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE);
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
scoreView.setVisibility(b ? VISIBLE : GONE);
}
After: XML
Slide 48
Slide 48 text
Before: Java
scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE);
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
scoreView.setVisibility(b ? VISIBLE : GONE);
}
After: XML
Slide 49
Slide 49 text
Before: Java
scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE);
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
scoreView.setVisibility(b ? VISIBLE : GONE);
}
After: XML
Slide 50
Slide 50 text
Before: Java
scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE);
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
scoreView.setVisibility(b ? VISIBLE : GONE);
}
After: XML
Slide 51
Slide 51 text
Before: Java
homeTeamIconView.setImageResource(match.getHomeTeam().getDrawableId());
After: XML
Slide 52
Slide 52 text
Before: Java
homeTeamIconView.setImageResource(match.getHomeTeam().getDrawableId());
After: XML
Slide 53
Slide 53 text
Callbacks
Slide 54
Slide 54 text
Before: Java
bottomNavigationView.setOnNavigationItemSelectedListener(this);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {}
After: XML
Slide 55
Slide 55 text
Before: Java
bottomNavigationView.setOnNavigationItemSelectedListener(this);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {}
After: XML
Slide 56
Slide 56 text
Before: Java
showScoreView.setOnCheckedChangeListener(this);
@Override
public void onCheckChanged(CompoundButton compoundButton, boolean b) {}
After: XML
ViewModel
public class MatchViewModel {
private Match match;
public Match getMatch() {
return match;
}
public void setMatch(Match match) {
this.match = match;
}
}
Slide 73
Slide 73 text
ViewModel
public class MatchViewModel extends BaseObservable {
private Match match;
@Bindable
public Match getMatch() {
return match;
}
public void setMatch(Match match) {
this.match = match;
notifyPropertyChanged(BR.match);
}
}
Slide 74
Slide 74 text
ViewModel
public class MatchViewModel extends BaseObservable {
private Match match;
@Bindable
public Match getMatch() {
return match;
}
public void setMatch(Match match) {
this.match = match;
notifyPropertyChanged(BR.match);
}
}
Slide 75
Slide 75 text
ViewModel
public class MatchViewModel extends BaseObservable {
private Match match;
@Bindable
public Match getMatch() {
return match;
}
public void setMatch(Match match) {
this.match = match;
notifyPropertyChanged(BR.match);
}
}
Slide 76
Slide 76 text
ViewModel
public class MatchViewModel extends BaseObservable {
private Match match;
@Bindable
public Match getMatch() {
return match;
}
public void setMatch(Match match) {
this.match = match;
notifyPropertyChanged(BR.match);
}
}
Slide 77
Slide 77 text
Two way Binding: Model ↔ View
ModelView changes ⇒ UI updates
ModelView updates ⇐ UI changes
Slide 78
Slide 78 text
Two way Binding: Model ↔ View
ModelView changes ⇒ UI updates
ModelView updates ⇐ UI changes
Slide 79
Slide 79 text
Before
Slide 80
Slide 80 text
After
Slide 81
Slide 81 text
After
Slide 82
Slide 82 text
Data Binding Library: How ?
● Zero reflection
● 100% generated Java code
● Bitwise flags to make views dirty
Slide 83
Slide 83 text
Data Binding Library: How ?
● Zero reflection
● 100% generated Java code
● Bitwise flags to make views dirty
Slide 84
Slide 84 text
Data Binding Library: How ?
● Zero reflection
● 100% generated Java code
● Bitwise flags to make views dirty
Slide 85
Slide 85 text
Data Binding Library: How ?
● Zero reflection
● 100% generated Java code
● Bitwise flags to make views dirty
Slide 86
Slide 86 text
Data Binding Library: Generated Code
● from layout/match.xml
● MatchBinding.java
● matchBinding.score
○ MatchBinding.date
○ MatchBinding.team
○ MatchBinding.showScore
○ MatchBinding.bottomNavigationView
Attributes for every java setter
class Team {
private int drawableId;
public int getDrawableId() {}
}
class ImageView > void setImageResource (int resId)
Slide 90
Slide 90 text
Attributes for every java setter
class Team {
private int drawableId;
public int getDrawableId() {}
}
class ImageView > void setImageResource (int resId)
Slide 91
Slide 91 text
Custom XML Attributes
@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String url) {
Picasso.with(view.getContext())
.load(url)
.into(view);
}
Slide 92
Slide 92 text
Custom XML Attributes
@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String url) {
Picasso.with(view.getContext())
.load(url)
.into(view);
}
Android Studio
● Gradle integration
● Syntax highlighting
● Code completion
● Can view/debug generated code
Slide 99
Slide 99 text
Android Studio
● Gradle integration
● Syntax highlighting
● Code completion
● Can view/debug generated code
Slide 100
Slide 100 text
Android Studio
● Gradle integration
● Syntax highlighting
● Code completion
● Can view/debug generated code
Slide 101
Slide 101 text
Android Studio
● Gradle integration
● Syntax highlighting
● Code completion
● Can view/debug generated code
Slide 102
Slide 102 text
Android Studio
● Gradle integration
● Syntax highlighting
● Code completion
● Can view/debug generated code
Slide 103
Slide 103 text
Android Studio Tips
Slide 104
Slide 104 text
Android Studio Tips
● Put namespaces in
Slide 105
Slide 105 text
Android Studio Tips
● Use tools: for preview
Slide 106
Slide 106 text
Easy to start
● Can be applied to a unique part of your app
● Don’t have to use every feature
○ Only view binding,
○ One way data binding,
○ Custom XML attributes...
Fin
Slides: https://goo.gl/Pr4kjj
Benoît Quenaudon
@oldergod
Slide 112
Slide 112 text
References
● DataBinding Demo App
○ https://github.com/oldergod/DataBindingDemo
● Data Bindling Library
○ https://developer.android.com/topic/libraries/data-binding/index.html
● talk:title=”@{databinding}”
○ https://www.youtube.com/watch?v=zYGVsTE_scI
● 057: Data Binding with GDE Lisa Wray
○ http://fragmentedpodcast.com/episodes/057/
● George Mount’s blog posts
○ https://medium.com/@georgemount007