Slide 1

Slide 1 text

Binding Data with Android Databinding +Riccardo Ciovati

Slide 2

Slide 2 text

Whoami ● I have been building Android apps for more than 5 years ● Android Developer at Subito ● GDG Milano manager since April 2015

Slide 3

Slide 3 text

Data Binding is a way to get your data from your data model into your UI using (almost) just markup. What’s Data Binding?

Slide 4

Slide 4 text

How we did it so far?

Slide 5

Slide 5 text

How we did it so far? public class MainActivity extends AppCompatActivity { private Ad mAd = new Ad("iPhone for sale"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView subject = (TextView) findViewById(R.id.ad_subject); subject.setText(mAd.getSubject()); } }

Slide 6

Slide 6 text

What’s wrong with this? ● Could crash at runtime ○ Missing ID ○ Wrong cast ○ Multiple layout configurations can have different elements ● Each findViewById requires a layout traversal ● Only the main thread can access those elements ● It’s boilerplate code ○ A few libraries to the rescue (eg Butterknife)

Slide 7

Slide 7 text

Hello Data Binding

Slide 8

Slide 8 text

Hello Data Binding public class MainActivity extends AppCompatActivity { private Ad mAd = new Ad("iPhone for sale"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setAd(mAd); } }

Slide 9

Slide 9 text

Hello Data Binding ● ● ● ● ● ● ● ● Code generator + small runtime dependency ● Works with API level 7+ apply plugin: 'com.android.databinding' buildscript { repositories { jcenter() } dependencies { classpath "com.android.databinding:dataBinder:1.0-rc2" } }

Slide 10

Slide 10 text

● Most Java expressions are supported: ○ String concatenation: + ○ Logical operator: &&, || ○ Comparision: ==, =>, <=, ect ● Method invocation and field access ● Built-in null-safety and null-coalescing operator: ○ android:text="@{ad.subject ?? @string/no_subject}" ● Expressions autocomplete in AS is not supported (yet) ● Syntax checks at compile-time Full List: https://goo.gl/9FVASg Expressions - Basics

Slide 11

Slide 11 text

Expressions - Method invocation

Slide 12

Slide 12 text

Expressions - Arrays and Maps android:text="@{list[index]}" android:text="@{sparse[index]}" android:text="@{map[key]}"

Slide 13

Slide 13 text

Expressions - Considerations ● Expression type matters! ● Avoid complex expressions ○ Consider use ViewModel

Slide 14

Slide 14 text

Model-View-ViewModel is an architectural pattern which allows to to separate and mediate the communication between model and views. ● Separation of presentation logic and business logic ● Testability Source: https://goo.gl/d0oxj7 MVVM MVVM picture

Slide 15

Slide 15 text

One way: model values are automatically assigned to the UI placeholder elements specified through the data binding notation, but the UI elements don't change the values in the model. Changes in the model are not propagated to the view. Source: https://goo.gl/fo3797 Databinding models One-Way Data Binding

Slide 16

Slide 16 text

Two way: model values are automatically assigned to the UI placeholder elements specified through the data binding notation. Any changes to the view are immediately reflected in the model, and any changes in the model are propagated to the view. Source: https://goo.gl/fo3797 Databinding models Two-Way Data Binding

Slide 17

Slide 17 text

One and a half way databinding The Data Binding Library provides a mechanism to implement an hybrid approch between one-way and two-way databinding in wich model changes are reflected to the UI. To implement that we have two options: ● Using Observable Objects ● Using Observable Fields

Slide 18

Slide 18 text

Observable Objects public class Ad extends BaseObservable { private String mSubject; public void setSubject(String subject) { mSubject = subject; notifyPropertyChanged(BR.subject); } @Bindable public String getSubject() { return mSubject; } }

Slide 19

Slide 19 text

Observable Objects ● We need to annotate each getter with the @Bindable annotation provided by the Data Binding Library and notify the changes in the setter. ● For each method annotated the compiler will generate a corrisponding field in the BR class. If case we don’t want or we can’t extend from BaseObservable we can implement the Observable interface.

Slide 20

Slide 20 text

Observable Fields ● Built-in Observable types: ObservableField, ObservableBoolean, ObservableByte, ObservableChar, ObservableShort, ObservableInt, ObservableLong, ObservableFloat, ObservableDouble, and ObservableParcelable. public class Ad { public final ObservableField subject = new ObservableField<>(); } //Setting the value will refresh the components observing this field mAd.subject.set("iPhone for sale");

Slide 21

Slide 21 text

Observable Collections ● ObservableList ○ ObservableArrayList is the implementation ○ Notify whether items are added, changed, moved or removed ● ObservableMap ○ ObservableArrayMap is the implementation ○ Notify whether a key is added, removed or changed.

Slide 22

Slide 22 text

● You don’t have to take care to set Observables fields from the main thread, the Data Binding Library will dispatch notifications in the main thread for you. Observable Fields

Slide 23

Slide 23 text

Binding Adapter ● ● ● ● ● ● ●

Slide 24

Slide 24 text

Binding Adapter ● ● ● ● ● ● ●

Slide 25

Slide 25 text

Binding Adapter ● Namespace is actually not taken into account ● Cool examples: ○ Setting custom typeface: https://goo.gl/H9v1vA ○ Binding collection to ListView/RecyclerView: https://goo.gl/zQl94z @BindingAdapter("bind:displayedChild") public static void setDisplayedChild(ViewAnimator viewAnimator, int index) { viewAnimator.setDisplayedChild(index); }

Slide 26

Slide 26 text

Binding Adapter ● You read parameters from multiple attributes at once @BindingAdapter({"bind:imageUrl", "bind:error"}) public static void loadImage(ImageView view, String url, Drawable error) { Picasso.with(view.getContext()).load(url).error(error).into(view); }

Slide 27

Slide 27 text

Summary ● Databinding allows you to rethink your app architecture in a more robust and testable way ● Keep away your business logic from you views → MVVM ● Tools are currently under development so expect big enhacements in the future

Slide 28

Slide 28 text

Thanks! We are hiring! - jobs.subito.it

Slide 29

Slide 29 text

References and further reading ● Databinding Guide: https://goo.gl/2s693c ● Building Apps Faster with Android Data Binding: https://goo.gl/zkyNwA ● Data Binding Techniques @ Droidcon NYC, 2015: https://goo.gl/WsmIYc ● Android Databinding: goodbye Presenter, hello ViewModel: http://goo. gl/xnqDWt ● MVVM on Android using the Data Binding Library: http://goo.gl/qCXFRc