Slide 1

Slide 1 text

Data Binding and You Siena Aguayo Software Engineer, Indiegogo @sienatime

Slide 2

Slide 2 text

@sienatime What is data binding?

Slide 3

Slide 3 text

@sienatime Attaches model data to your view public class Pikachu { public String name = "Pikachu"; public String genus = "Mouse"; public String type = "Electric"; public int imageResource = R.drawable.sprite_25; }

Slide 4

Slide 4 text

@sienatime One- and Two-Way Binding One-Way Binding Two-Way Binding Model View Model View

Slide 5

Slide 5 text

@sienatime Why is data binding useful?

Slide 6

Slide 6 text

@sienatime Eliminate boilerplate! TextView pokeName = (TextView) findViewById(R.id.poke_name); TextView pokeGenus = (TextView) findViewById(R.id.poke_genus); TextView pokeType = (TextView) findViewById(R.id.poke_type); pokeName.setText(pikachu.name); pokeGenus.setText(String.format(R.string.genus_format, pikachu.genus)); pokeType.setText(pikachu.type) PikachuActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.pikachu_activity); binding.setPikachu(new Pikachu());

Slide 7

Slide 7 text

@sienatime How do I set it up?

Slide 8

Slide 8 text

@sienatime Usage in build.gradle android { dataBinding { enabled = true } } dependencies { compile "com.android.support:appcompat-v7:23.4.0" }

Slide 9

Slide 9 text

@sienatime Usage in XML

Slide 10

Slide 10 text

@sienatime Custom @BindingAdapter XML ImageViewBindings.java @BindingAdapter("android:imageResource") public static void setImage(ImageView view, int resourceId) { view.setImageDrawable(view.getContext().getDrawable(resourceId)); } Pikachu.java public int imageResource = R.drawable.sprite_25;

Slide 11

Slide 11 text

@sienatime Data binding works back to API 7! compile "com.android.support:appcompat-v7:23.4.0"

Slide 12

Slide 12 text

@sienatime Further Reading ● Code used in this presentation https://github.com/sienatime/pikachu-data- binding ● Official documentation https://developer.android.com/topic/libraries/data- binding/index.html ● “Data Binding Techniques” by Jacob Tabak’s (video from Droidcon NYC 2015) https://www.youtube.com/watch?v=WdUbXWztKNY ● “Marshmallow Brings Data Bindings to Android” by Yiğit Boyar and George Mount (video and transcription from Bay Area Android Dev Group, Oct. 2015) https://realm.io/news/data-binding-android-boyar-mount/ ● “Advanced Data Binding” by Yiğit Boyar (video from Google I/O 2016) https: //www.youtube.com/watch?v=DAmMN7m3wLU