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.

9ab0b3b080e75e0c03a0c643333f8b93?s=128

Segun Famisa

July 23, 2016
Tweet

Transcript

  1. Faster Android development with Data Binding Android Fortnightly Series, July

    2016
  2. Segun Famisa Software Engineer at Konga,KongaPay @segunfamisa segunfamisa.com

  3. What is data binding?

  4. 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 ()`
  5. What is data binding? Regular Way of accessing views

  6. What is data binding? So much boilerplate code Data binding

    helps bring an end to that. findViewById()
  7. None
  8. Data binding in your Android app

  9. Data binding in Android ButterKnife Holdr Data binding library

  10. Data binding in Android ButterKnife Holdr Data binding library

  11. Data binding in Android ButterKnife Holdr Data binding library Holdr

    has been deprecated
  12. Data binding in Android ButterKnife Holdr Data binding library

  13. Why use data binding?

  14. 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.
  15. 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
  16. Why use data binding? Avoid boilerplate code Efficiency No Performance

    issues Data binding does not use reflection. Every processing happens at compile time. #PerfMatters
  17. How to use data binding in Android

  18. 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.
  19. 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 }
  20. Writing your first data binding code

  21. Your first data binding code Make <layout> outer tag Use

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

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

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

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

    DataBindingUtil a. Activity b. Fragment
  26. Yasss, we did it. Let’s take it further!

  27. Using variables

  28. Using variables 1. Add <variable> tag 2. Set variable

  29. Using variables 1. Add <variable> tag 2. Set variable The

    user variable within data describes a property that may be used within this layout.
  30. Using variables 1. Add <variable> tag 2. Set variable

  31. Let’s take it further, let’s handle events!

  32. Add handler class & method Add handler variable to <data>

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

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

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

    tag Bind handler to view in code Handling events - Method references
  36. Layout details

  37. Layout details Imports Expressions Includes Null coalescing

  38. Layout details Imports Expressions Includes Null coalescing • Mathematical +

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

    Grouping () • Literals - character, String, numeric, null • Cast • Method calls • Field access • Array access [] • Ternary operator ?:
  40. Layout details Imports Expressions Includes Null coalescing Expressions that aren’t

    supported: “new” “this” “super”
  41. 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
  42. Layout details Imports Expressions Includes Null coalescing

  43. Layout details Imports Expressions Includes Null coalescing The expression resolves

    to user.nickName if user. name is null and user. name otherwise
  44. Under the hood How data binding works?

  45. 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
  46. How data binding in Android works Begin Compilation Process layout

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

    custom attributes to static methods. TL;DR you can create your own attributes.
  49. Attribute setters Custom setters. Using @BindingAdapter annotation, you can bind

    custom attributes to static methods.
  50. Attribute setters Custom setters. Using @BindingAdapter annotation, you can bind

    custom attributes to static methods.
  51. Resources & References • https://developer.android.com/topic/libraries/data- binding/index.html • https://www.youtube.com/watch?v=NBbeQMOcnZ0 • https://www.youtube.com/watch?v=DAmMN7m3wLU

    • https://realm.io/news/data-binding-android-boyar-mount/ • https://goo.gl/jwc921
  52. We could go on and on and on..

  53. Questions? @segunfamisa segunfamisa.com Thank you for listening.