Slide 1

Slide 1 text

“@{dataBinding}” talk:title= @{dataBinding} @lisawrayz

Slide 2

Slide 2 text

textView.setCompoundDrawablesWithIntrinsicBounds( ContextCompat.getDrawable(this, R.drawable.cat), null, null, null); Problem: Repetitive Java boilerplate @{dataBinding} findViewById, casts setters are verbose/confusing toolbar = (Toolbar) findViewById(R.id.toolbar);

Slide 3

Slide 3 text

Problem: Repetitive Java boilerplate @{dataBinding} findViewById, casts setters are verbose/confusing XML layout is limited Manually keep track of UI updates

Slide 4

Slide 4 text

What is it? @{dataBinding} A framework to connect your model and your UI Once or persistently

Slide 5

Slide 5 text

An XML attribute for every Java setter @{dataBinding} What else is it?

Slide 6

Slide 6 text

Custom XML attributes @{dataBinding} What else is it?

Slide 7

Slide 7 text

Overwrite Android XML attributes @{dataBinding} What else is it?

Slide 8

Slide 8 text

An alternative to custom views @{dataBinding} What else is it?

Slide 9

Slide 9 text

O.K., show me!

Slide 10

Slide 10 text

Find the views Set custom font Set button colors Load and set the image Set title

Slide 11

Slide 11 text

Before: inflate and find views CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout); ImageView backdropImageView = (ImageView) findViewById(R.id.backdrop); Button upvoteButton = (Button) findViewById(R.id.upvote); Button downvoteButton = (Button) findViewById(R.id.downvote); setContentView(R.layout.activity_pet_detail); After PetDetailBinding binding = DataBindingUtil.setContentView (this, R.layout.pet_detail);

Slide 12

Slide 12 text

Before:Java if (pet != null) { appBarLayout.setTitle(pet.getName()); } After:XML

Slide 13

Slide 13 text

Before: Java Typeface lobster = FontCache.getInstance().get("LobsterTwo-Bold"); appBarLayout.setCollapsedTitleTypeface(lobster); appBarLayout.setExpandedTitleTypeface(lobster); After:XML

Slide 14

Slide 14 text

Before:Java After:XML switch(pet.vote) { case UPVOTE: upvoteButton.setSelected(true); downvoteButton.setSelected(false); break; case DOWNVOTE: …

Slide 15

Slide 15 text

Before: Java Glide.with(this).load(pet.getImageUrl()).into(backdropImageView); After:XML

Slide 16

Slide 16 text

Making the connection

Slide 17

Slide 17 text

PetDetailBinding binding = DataBindingUtil.setContentView (this, R.layout.pet_detail); Before: Java … After:XML binding.setPet(pet); PetDetailBinding binding = DataBindingUtil.setContentView (this, R.layout.pet_detail); Your old layout goes here Your model object

Slide 18

Slide 18 text

100% generated Java code Uses bitwise flags to mark ‘dirty’ One traversal to find all views How does it work? @{dataBinding}

Slide 19

Slide 19 text

layout/pet.xml generated: PetBinding.java PetBinding.image .title .upvote @{dataBinding}

Slide 20

Slide 20 text

@{ }

Slide 21

Slide 21 text

Existing attributes Pet.getName() app:title="@{pet.name}"

Slide 22

Slide 22 text

android:text android:textColor Existing attributes android:drawableLeft android:src … and many, many more

Slide 23

Slide 23 text

Attributes for every Java setter View.setSelected()

Slide 24

Slide 24 text

app:imageUrl="@{pet.imageUrl}" Custom attributes

Slide 25

Slide 25 text

@BindingAdapter({"bind:imageUrl"}) public static void loadImage(ImageView view, String url) { Glide.with(view.getContext()) .load(url) .into(view); } Bindings You pick The view you’re binding Attribute

Slide 26

Slide 26 text

app:font Custom attributes @BindingAdapter({"bind:font"}) public static void setFont(TextView textView, String fontName) { Typeface type = FontCache.getInstance().get(fontName); textView.setTypeface(type); } Bindings Tinkerbell

Slide 27

Slide 27 text

app:collapsedTitleTypeface="@{`LobsterTwo-Bold`}" app:expandedTitleTypeface="@{`LobsterTwo-Bold`}" Custom attributes @BindingAdapter("bind:expandedTitleTypeface") public static void setExpandedTitleTypeface( CollapsingToolbarLayout layout, String fontName) { Typeface type = FontCache.getInstance().get(fontName); layout.setExpandedTitleTypeface(type); } @BindingAdapter("bind:collapsedTitleTypeface") public static void setCollapsedTitleTypeface …

Slide 28

Slide 28 text

Evaluate simple expressions app:selected=“@{vote == VOTE.UPVOTE}"

Slide 29

Slide 29 text

before: if (pet != null) { appBarLayout.setTitle(pet.getName()); } after: android:text=“@{pet.name}” Avoid NPEs android:text=“@{pet.name.last}”

Slide 30

Slide 30 text

@BindingAdapter("android:indeterminateTint") public static void setIndeterminateTint( ProgressBar progressBar, int color) { Drawable toTint = progressBar.getIndeterminateDrawable().mutate(); toTint.setColorFilter(color, PorterDuff.Mode.SRC_IN); } Overwrite Android attributes

Slide 31

Slide 31 text

layout/family.xml … layout/pet.xml …

Slide 32

Slide 32 text

One way binding Model changes, view is updated Persistent binding

Slide 33

Slide 33 text

enum VOTE { UPVOTE, DOWNVOTE, NONE } public class VoteState { public final ObservableField vote = new ObservableField(); } voteState.vote.set(VOTE.DOWNVOTE) Persistent binding

Slide 34

Slide 34 text

enum VOTE { UPVOTE, DOWNVOTE, NONE } public class VoteState extends BaseObservable { private VOTE vote; @Bindable getVote(); setVote(VOTE vote) { this.vote = vote; notifyChanged(BR.vote) } } Persistent binding

Slide 35

Slide 35 text

“In beta”, you said “It’ll be fun”, you said

Slide 36

Slide 36 text

Gradle integration Syntax highlighting Code completion (2.0.0-alpha5) View and debug generated code Android Studio and You

Slide 37

Slide 37 text

Refactoring “Convert to data binding” shortcut “Clean project” required sometimes Still to come …

Slide 38

Slide 38 text

Android Studio tips tools:text=“Jane Doe” tools:ignore="UnusedAttribute" Declare namespaces in layout tag Use tools: prefix for layout preview Ignore lint warnings

Slide 39

Slide 39 text

app/build.gradle android { dataBinding { enabled = true } } How do I get started?

Slide 40

Slide 40 text

fonts: goo.gl/n6lQY7 docs: goo.gl/47cVzB @lisawrayz