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

Faster Android Development with Data Binding

Faster Android Development with Data Binding

Session at the Android Fortnightly Series by GDG Lagos.

A talk about how to use data binding in Android to improve development speed, performance and prevent redundant code.

Segun Famisa

July 23, 2016
Tweet

More Decks by Segun Famisa

Other Decks in Programming

Transcript

  1. 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 ()`
  2. What is data binding? So much boilerplate code Data binding

    helps bring an end to that. findViewById()
  3. 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.
  4. 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
  5. Why use data binding? Avoid boilerplate code Efficiency No Performance

    issues Data binding does not use reflection. Every processing happens at compile time. #PerfMatters
  6. 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.
  7. 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 }
  8. Your first data binding code Make <layout> outer tag Use

    DataBindingUtil a. Activity b. Fragment
  9. Your first data binding code Make <layout> outer tag Use

    DataBindingUtil a. Activity b. Fragment
  10. Your first data binding code Make <layout> outer tag Use

    DataBindingUtil a. Activity b. Fragment
  11. Your first data binding code Make <layout> outer tag Use

    DataBindingUtil a. Activity b. Fragment
  12. Your first data binding code Make <layout> outer tag Use

    DataBindingUtil a. Activity b. Fragment
  13. Using variables 1. Add <variable> tag 2. Set variable The

    user variable within data describes a property that may be used within this layout.
  14. Add handler class & method Add handler variable to <data>

    tag Bind handler to view in code Handling events - Method references
  15. Add handler class & method Add handler variable to <data>

    tag Bind handler to view in code Handling events - Method references
  16. Add handler class & method Add handler variable to <data>

    tag Bind handler to view in code Handling events - Method references
  17. Add handler class & method Add handler variable to <data>

    tag Bind handler to view in code Handling events - Method references
  18. Layout details Imports Expressions Includes Null coalescing • Mathematical +

    - / * % • String concatenation + • Logical && || • Binary & | ^ • Unary + - ! ~ • Shift >> >>> << • Comparison == > < >= <=
  19. Layout details Imports Expressions Includes Null coalescing • instanceof •

    Grouping () • Literals - character, String, numeric, null • Cast • Method calls • Field access • Array access [] • Ternary operator ?:
  20. 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 <include> tag cannot be the direct child of a <merge> tag
  21. Layout details Imports Expressions Includes Null coalescing The expression resolves

    to user.nickName if user. name is null and user. name otherwise
  22. 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
  23. How data binding in Android works Begin Compilation Process layout

    files Parse Expressions Resolve dependencies Find setters Write binders Java Compilation
  24. 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
  25. Attribute setters Custom setters. Using @BindingAdapter annotation, you can bind

    custom attributes to static methods. TL;DR you can create your own attributes.