Slide 1

Slide 1 text

Data Binding An Introduction to MVVM approach By Chintan Soni

Slide 2

Slide 2 text

Agenda ● Introduction ● Components of Data Binding ● Hands On: Simple Data Binding Demo with / without Event Binding ● Hands On: RecyclerView Demo with / without Event Binding ● Hands On: ToDo App

Slide 3

Slide 3 text

Introduction

Slide 4

Slide 4 text

You hate boilerplate code. Just confirming it... ● Assigning ids in layouts ● Finding views and casting it to relevant views ● Just for the sake of setting a text..!! android:id="@+id/textview" TextView textView = (TextView)findViewById(R.id.textview); textView.setText("Do i need to do it everytime..??");

Slide 5

Slide 5 text

Thanks to Jake Wharton for writing ButterKnife... ● This made life more simpler and easier: ● @Bind(R.id.textview) TextView textView; textView.setText("Feeling better now.."); But still you are dealing with ids.. Should it be mandatory ?

Slide 6

Slide 6 text

Any other way out ? Think of a some kind of mechanism that works like; You have the modelled data to display; Then you merely pass it to the layout; And layout will take care of WHERE to display WHAT.. "Are you kidding me..??"..

Slide 7

Slide 7 text

What If I say You can achieve most of the things from the layout itself ● Setting data, ● Handling events ● and much more..

Slide 8

Slide 8 text

Welcome to, Data Binding: An MVVM Approach

Slide 9

Slide 9 text

Data Binding ● Introduced along with Android Studio 1.3 at Google IO 2015 ● Allows to write declarative layouts ● Minimizes a huge amount of boilerplate code ● Binds your application logic directly to your layout

Slide 10

Slide 10 text

Components

Slide 11

Slide 11 text

Layout ● Every layout file that is declared for data binding starts with ● is place where we define tags carrying the model ● tag defines the type of the data and its alias name to use in layout . . . Basic Data Binding Layout Template

Slide 12

Slide 12 text

Sample Data Binding Layout ● To use this variable “user”, we have to write Expression @{...} ● This way we can bind the relevant properties of Model class to views, DIRECTLY...

Slide 13

Slide 13 text

DataBindingUtil ● It generates Binding class ● Binding class wires up layout properties to layout views ● Binding class is generated based on the name of layout file, starting it with upper-case, removing underscores ( _ ) and capitalizing the following letter and then suffixing “Binding” ● After generating the class, you can simply set the data with setter method of Binding class. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity); User user = new User("Test", "User"); binding.setUser(user); }

Slide 14

Slide 14 text

That’s it !!! Let’s Move On to Hands On Experience with Data Binding...

Slide 15

Slide 15 text

GDG Developers Conclave Reach Me At: @chintansoni202 +ChintanSoni201188 [email protected]