Slide 1

Slide 1 text

Databinding in Android BRYAN HERBST

Slide 2

Slide 2 text

Job Interview Q: How does an Android Activity fit in to the MVC pattern? A: Ehh…

Slide 3

Slide 3 text

MV[C|P|VM] A Model that represents your raw data. Aka POJOs. A View that dictates how your data is displayed to the user Something to get the data and display the view

Slide 4

Slide 4 text

View

{{title}}

{{body}}

Slide 5

Slide 5 text

Controller var template = Handlebars.compile(viewTemplate); var data = {title: “Hello World", body: “Templating rocks!"}; var html = template(data);

Slide 6

Slide 6 text

Let there be data binding Now we can have better separation of concerns Activities and Fragments now have more limited scope and a clearer purpose: ◦ Load data ◦ Bind data to the View ◦ Respond to events

Slide 7

Slide 7 text

Setup buildscript { dependencies { classpath 'com.android.databinding:dataBinder:1.0-rc0' } } apply plugin: 'com.android.databinding'

Slide 8

Slide 8 text

The View- step one Wrap your layout in a tag

Slide 9

Slide 9 text

The View- step two Add a tag with s for your models

Slide 10

Slide 10 text

The View- step three Bind to model with @{model.property}

Slide 11

Slide 11 text

The binding class Plugin generates binding class my_fancy_layout.xml becomes MyFancyLayoutBinding

Slide 12

Slide 12 text

The controller side @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityAnimalBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_animal); binding.setAnimal(new Elephant()); }

Slide 13

Slide 13 text

Demo time!

Slide 14

Slide 14 text

Can the views be smarter? What else can we bind?

Slide 15

Slide 15 text

Visibility- step one Import types into layouts just like Java

Slide 16

Slide 16 text

Visibility- step two android:visibility="@{note.content == null ? View.GONE : View.VISIBLE}"

Slide 17

Slide 17 text

Introducing the ViewModel Because that date format is terrible.

Slide 18

Slide 18 text

NoteViewModel public class NoteViewModel { private static final DateFormat TIMESTAMP_FORMAT = DateFormat.getDateInstance(); private Note mNote; public String getFormattedTimestamp() { return TIMESTAMP_FORMAT.format(mNote.getCreatedTime()); } }

Slide 19

Slide 19 text

Demo time!

Slide 20

Slide 20 text

Data binding: Two way street Using app namespace, bind to anything with a setter

Slide 21

Slide 21 text

Automatic updates What happens when our data changes?

Slide 22

Slide 22 text

Observables No, not RxJava

Slide 23

Slide 23 text

Observables in the ViewModel public ObservableInt numClicks; public View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View view) { numClicks.set(numClicks.get() + 1); } };

Slide 24

Slide 24 text

Observables in the layout Text now automatically updates

Slide 25

Slide 25 text

Demo time!

Slide 26

Slide 26 text

Resources https://developer.android.com/tools/data-binding/guide.html Tons of information about: ◦ s ◦ Operators in bindings ◦ Accessing arrays/lists/maps ◦ Using resources in bindings ◦ Value converters ◦ Binding custom view properties ◦ More ways to make data observable