SlideShare a Scribd company logo
Data Binding
Who we are?
Leonid Olevsky
leonid.olevsky@gmail.com
Yonatan Levin
levinyon@gmail.com
Android
Academy
Data binding
Today
findViewById
Coordinates between the model and the views
Create untestable code
And more...
What is Data Binding?
Model View ViewModel (MVVM)
Ongoing link between an element in the user interface
and a value
When a variable’s value is updated, the display on the
user’s screen changes automatically
Dependencies
It's a support library - Android 2.1 (API level
7+)
Gradle 1.3.0-beta4 or higher is required
Beta release
Dependencies
In a top-level build.gradle add
In each module
Simple usage - Binding Data
Simple usage - Binding Data
Data binding
Simple usage - Binding Data
The magic
Other option
Simple usage - Binding Events
More options
Use imports
Alies
More options
Referencing static fields and methods in
expressions
More options - include
Expression Language
Mathematical + - / * %
String concatenation +
Logical && ||
Binary & | ^
Unary + - ! ~
Shift >> >>> <<
Comparison == > < >= <=
instanceof
Grouping ()
Literals - character, String,
numeric, null
Cast
Method calls
Field access
Array access []
Ternary operator ?:
Expression Language
Not supported
this
super
new
Explicit generic invocation
Observable
Observable Objects
listen for changes of all properties on that object
ObservableFields
listen for changes of a field of that object
Observable Collections
listen for changes on dynamic structures to hold data
Observable Objects
ObservableFields
Observable Collections
ObservableArrayMap ObservableArrayList
Further Reading
developer.android.com/tools/data-binding/guide.html
Our app
Architecture
StarwarsActivity
CrawlLoader OpeningCrawl
mCrawlLoader.startLoading()
new OpeningCrawl
onLoadFinishListener.onLoadFinish(openingCrawl)
Every ~5 seconds
findViewById(R.id.asw_tv_episode);
findViewById(R.id.asw_tv_crawl);
mEpisodeTextView.setText(...);
mCrawlTextView.setText(...);
mCrawlTextView.startAnimation();
Data Binding could be the mess
Downside
- Define stuff in XML
- Not debuggable
- XML not unit tested
- Run-time errors instead compile time
- Bad IDE support
MVP - The Best Simple Friend
Why MVP?
- Decoupled layers
- Easy to debug
- Fully testable business logic, data model
MVP vs DataBinding
Presenter react on what happen in the view,
retrieves data, formats and display back in the
view
Data Binding will take over the main
responsibilities of the presenter (react and
display). Rest - will go to the enhanced Model -
ViewModel
MVVM
https://en.wikipedia.org/wiki/Model_View_ViewModel
MVVM Architecture
StarwarsActivity
CrawlLoader OpeningCrawl
mCrawlLoader.startLoading()
new OpeningCrawl
onLoadFinishListener.onLoadFinish(openingCrawl)
Every ~5 seconds
StarWarsViewModel
Code
https://github.com/parahall/StarWarsDataBinding.gi
t
Layout - ActivityStarWars.xml
Layout - ActivityStarWars.xml
StarWarsActivity
StarWarsActivity - no leak
StarWarsActivity - callbacks
StarWarsViewModel
StarWarsViewModel - react
StarWarsViewModel - retrieve
StarWarsViewModel - decide
* runOnUiThread - might be avoided using OttoBus event
CrawlLoader
So what we got?
Clean Debuggable code
Decoupled layer
Fully testable UI logic
Fun :)
Q?
Thank you!
Leonid Olevsky
leonid.olevsky@gmail.com
Yonatan Levin
levinyon@gmail.com
Android
Academy

More Related Content

Data binding

Editor's Notes

  1. The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view. The thing is that Data Binding framework will take over the main responsibilities of the Presenter (“acting upon the model and the view”), while the remainder is left to the enhanced Model – the ViewModel (“retreiving data from repositories and formatting”).
  2. The ViewModel is a standard Java class whose sole responsibility is to represent the data behind a single View. It can merge data from multiple sources (Models) and prepare that data for presentation.
  3. All the binding and updating of data to the view is done through the Data Binding Framework. The ObservableField class allow the View to react on changes to the model, and the XML references to fields allow the framework to push changes back to the ViewModel when the user acts upon the View. You can also programmatically subscribe to changes in fields. One great advantage of representing the visual state of the View in a standard Java class like this is clear: You can easily unit test the visual behaviour.