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 ()`
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.
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
Why use data binding? Avoid boilerplate code Efficiency No Performance issues Data binding does not use reflection. Every processing happens at compile time. #PerfMatters
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.
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 }
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 tag cannot be the direct child of a tag
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
How data binding in Android works Begin Compilation Process layout files Parse Expressions Resolve dependencies Find setters Write binders Java Compilation
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
Attribute setters Custom setters. Using @BindingAdapter annotation, you can bind custom attributes to static methods. TL;DR you can create your own attributes.