Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advanced Android DataBinding

André Diermann
November 24, 2016
130

Advanced Android DataBinding

Talk at GDG Düsseldorf November MeetUp (2016-11-24).

Among Android Developers, the introduction of DataBinding is considered as one of „the most significant changes“ to the Android framework. It impacts the whole way how developers develop and design their apps. The basic usage of DataBinding is pretty straightforward. However, in advanced use cases the usage could be tricky. How to generically bind data in RecyclerViews? How to test DataBinding? What are BindingAdapters for? Which architectural style to choose for DataBinding? ...

This talk tries to answer these kind of questions by providing a closer look to this new tool. It addresses advanced challenges developers will stumble upon when applying DataBinding in real world projects. I'm using DataBinding since day one and already gained some practical experiences which might be interesting for other Android enthusiast as well. However, while one objective of the talk is to share my experience I would also like to facilitate a discussion among the attendees and also learn from their experiences with DataBinding.

André Diermann

November 24, 2016
Tweet

Transcript

  1. What? DataBinding is a tool which drastically reduces the amount

    of boilerplate code. It helps you to do more with less code.
  2. How? <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="…"> <data> <variable name="user" type="…"/>

    </data> <TextView … android:text="@{user.name}"/> </layout> main_activity.xml
  3. How? <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="…"> <data> <variable name="user" type="…"/>

    </data> <TextView … android:text="@{user.name}"/> </layout> main_activity.xml
  4. How? <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="…"> <data> <variable name="user" type="…"/>

    </data> <TextView … android:text="@{user.name}"/> </layout> main_activity.xml
  5. How? <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="…"> <data> <variable name="user" type="…"/>

    </data> <TextView … android:text="@{user.name}"/> </layout> main_activity.xml
  6. How? @Override protected void onCreate(…) { … MainActivityBinding binding =

    DataBindingUtil.setContentView( this, R.layout.main_activity); binding.setUser(user); } ActivityMain.java
  7. How? @Override protected void onCreate(…) { … MainActivityBinding binding =

    DataBindingUtil.setContentView( this, R.layout.main_activity); binding.setUser(user); } ActivityMain.java
  8. How? @Override protected void onCreate(…) { … MainActivityBinding binding =

    DataBindingUtil.setContentView( this, R.layout.main_activity); binding.setUser(user); } ActivityMain.java
  9. Features • Code generation • View references • Variables •

    Custom setters • Custom conversions • Event handling • Observability • Two-way binding • Expression language • …
  10. Data changes public class MyViewModel implements ViewModel { public final

    ObservableField<String> string; public final ObservableBoolean boolean; public final ObservableList<ListViewModel> list; … } Dynamic data Further reading: https://medium.com/google-developers/android-data-binding-observability-9de4ff3fe038
  11. Migration Button button = (Button) findViewById( R.id.button); button.setOnClickListener(…); Before 1.

    Eliminate findViewById()* * or any third party library binding.button. setOnClickListener(…); After
  12. Migration button.setOnClickListener( new OnClickListener(){ @Override onClick(…){ handler.onClick(…); } } );

    Before 2. Remove event listeners <Button android:onClick= “@{handler::onClick}“ /> After
  13. Migration User user = loadUser(); tvUserName.setText(user.getName()); if (user.getPicture() == null)

    ivUserPic.setVisibility(…); … Before 3. Bind data to UI binding.setUser(user); <TextView android:text=“@{user.name}“ /> <ImageView android:visibility=“@{user.picture}“ /> After
  14. 1. Activity or Fragment • Load Binding • Create ViewModel

    • Inject Interactors into ViewModel • Inject ViewModel into Binding VIEW 2. ViewModel • Load Model via Interactor • Bind Model data to ViewModel fields VIEW MODEL MODEL (Business Logic) 3. There is no step 3 ;-)
  15. Testing • Don‘t test the generated code • Test your

    ViewModels • Verify UI with Espresso
  16. Conclusion • DataBinding has great potential for faster and cleaner

    app dev. • Using its full potential requires architecture changes • It’s not meant for everything! • Some controversy voices exist