SlideShare a Scribd company logo
1 of 28
Download to read offline
Android Data
Binding
2
About Me
11
binding.setPresenter(
new Presenter(
“Ajit Singh”,
“Developer”,
“github.com/ajitsing”,
“www.singhajit.com”
)
);
Agenda
11
• What is Data Binding
• Why Data Binding?
• Live Coding / Demo
• Take Aways
10
What data binding do? How
would it help me?
Whats the problem with below code
11
Task task = tasks.get(position);



TextView taskTitle = (TextView) taskView.findViewById(R.id.task_title);

TextView taskDescription = (TextView) taskView.findViewById(R.id.task_description);

TextView creationDate = (TextView) taskView.findViewById(R.id.creation_date);
TextView reminderTime = (TextView) taskView.findViewById(R.id.reminder_timer);



taskTitle.setText(task.getTitle());

taskDescription.setText(task.getDescription());

creationDate.setText(task.getCreationDate());
reminderTime.setText(task.getReminderTime());
Again..
11
Button addTaskButton = (Button) findViewById(R.id.add_task);



addTaskButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

presenter.onAddTask();

}

});

}
10
How to eliminate this boiler plate
code??
Eliminate Boiler Plate Code
11
Task task = tasks.get(position);



binding.taskTitle.setText(task.getTitle());

binding.taskDescription.setText(task.getDescription());

binding.creationDate.setText(task.getCreationDate());
binding.reminderTime.setText(task.getReminderTime());
10
Is it a viewHolder??
Eliminate Boiler Plate Code
11
<Button

android:id="@+id/add_task"

android:onClick="@{presenter.onAddTask}"

……
/>
binding.setTask(task);
Data Binding
11
• Developed by Google
• Built in inside gradle plugin
• No external dependency needed
Enable Data Binding
11
android {
……
dataBinding {
enabled = true
}
}
Data Binding Layout Files
11
<layout>

<data class="UserBinding">

<import type=“com.ajit.singh.demo.util.AppUtil"/>

<variable

name="viewModel"

type="com.ajit.singh.demo.viewmodel.UserViewModel"/>

</data>
<TextView

android:text=“@{viewModel.name}"

android:layout_width="wrap_content"

android:layout_height=“wrap_content"/>
</layout>

User Event Handling
11
<Button

android:id="@+id/add_task"

android:onClick="@{(view) -> handler.onAddTask(viewModel)}"

android:layout_width=“wrap_content"

android:layout_height="wrap_content"/>
Binding Expressions
11
• android:text="@{String.valueOf(index + 1)}”
• android:text="@{user.displayName ?? user.lastName}”
• android:text="@{user.displayName != null ?
user.displayName : user.lastName}"
• android:padding="@{large? @dimen/largePadding :
@dimen/smallPadding}”
• android:visibility="@{user.isAdult ? View.VISIBLE :
View.GONE}"
Keywords Not Supported In Binding
11
• new
• this
• super
Inflating Layout
11
UserBinding binding = DataBindingUtil.setContentView(this,
R.layout.user_layout);
OR
DataBindingUtil.inflate(inflator, R.layout.user_layout, parent, false);
binding.setUser(new User(“Ajit Singh”));
TextView userName = (TextView) findViewById(R.id.user_name);
userName.setText(“Ajit Singh”);
ViewModel
11
public class User extends BaseObservable {
@Bindable

public String getName() {

return name;

}
public void setName(String name) {

this.name = name;

notifyPropertyChanged(BR.name);

}
}
10
Demo / Hands On
Demo App Architecture
11
Activity
Presenter/
Controller
DataBase
Layout
DataBase
execute some logic
Update UI
ReadWrite
Demo
11
One way binding with
static data
What we did?
11
• Wrap Parent ViewGroup in <layout> tag
• Move all namespaces to <layout> tag
• Define meta data of binding in <data> tag
• Inflate layout using DataBindingUtil
• set the variables defined in <data> tag in binding
Demo
11
Two way binding
What we did?
11
• Created a viewModel to hold view state
• Extend viewModel with BaseObservable
• Create setters for the fields
• Notify when data changes in viewModel
• @={viewModel.title}
Demo
11
Event Handling
Custom Binding
11
@BindingAdapter("setAdapter")

public static void setAdapter(AdapterView view, BaseAdapter adapter) {

view.setAdapter(adapter);

}
<ListView

android:id="@+id/tasks_list"

app:setAdapter="@{TasksAdapter.newInstance(viewModel.tasks)}"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>
Take Aways…
11
• Use one way binding to update data on UI
• Two way binding to update view model from UI
• Don’t extend viewModel with BaseObservable

for static data
• Use custom binding to avoid duplication and make code
readable in layout
• Do not write too much logic in layout files
THANK YOU
For questions or suggestions:
Ajit Singh
ajitsing@thoughtworks.com

More Related Content

Viewers also liked (10)

Santa Clara Law - Entrepreneur’s Law Clinic - IP, Data Contracts and HIPAA.PO...
Santa Clara Law - Entrepreneur’s Law Clinic - IP, Data Contracts and HIPAA.PO...Santa Clara Law - Entrepreneur’s Law Clinic - IP, Data Contracts and HIPAA.PO...
Santa Clara Law - Entrepreneur’s Law Clinic - IP, Data Contracts and HIPAA.PO...
 
Reservation
ReservationReservation
Reservation
 
491
491491
491
 
Saul Leal CAPTURING COMMUNITIES OF INTEREST 2
Saul Leal CAPTURING COMMUNITIES OF INTEREST 2Saul Leal CAPTURING COMMUNITIES OF INTEREST 2
Saul Leal CAPTURING COMMUNITIES OF INTEREST 2
 
Ya vybirayu professiyu_v_it
Ya vybirayu professiyu_v_itYa vybirayu professiyu_v_it
Ya vybirayu professiyu_v_it
 
интернет магазин
интернет магазининтернет магазин
интернет магазин
 
Winet v23
Winet v23Winet v23
Winet v23
 
Newsroom org models (iapa)
Newsroom org models (iapa)Newsroom org models (iapa)
Newsroom org models (iapa)
 
5 Amazing Tips To Prevent Shopping Cart Abandonment
5 Amazing Tips To Prevent Shopping Cart Abandonment5 Amazing Tips To Prevent Shopping Cart Abandonment
5 Amazing Tips To Prevent Shopping Cart Abandonment
 
TRABALHO DE GESTÃO DE CAMPANHAS DE MARKETING DIGITAL.
TRABALHO DE GESTÃO DE CAMPANHAS DE MARKETING DIGITAL.TRABALHO DE GESTÃO DE CAMPANHAS DE MARKETING DIGITAL.
TRABALHO DE GESTÃO DE CAMPANHAS DE MARKETING DIGITAL.
 

Similar to Android data binding

Similar to Android data binding (20)

Effective Android Data Binding
Effective Android Data BindingEffective Android Data Binding
Effective Android Data Binding
 
Data Binding
Data BindingData Binding
Data Binding
 
SharePoint Conference 2018 - APIs, APIs everywhere!
SharePoint Conference 2018 - APIs, APIs everywhere!SharePoint Conference 2018 - APIs, APIs everywhere!
SharePoint Conference 2018 - APIs, APIs everywhere!
 
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
 
APIs, APIs Everywhere!
APIs, APIs Everywhere!APIs, APIs Everywhere!
APIs, APIs Everywhere!
 
Vaadin7
Vaadin7Vaadin7
Vaadin7
 
What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?
 
Deep dive into Android Data Binding
Deep dive into Android Data BindingDeep dive into Android Data Binding
Deep dive into Android Data Binding
 
Android development
Android developmentAndroid development
Android development
 
Data binding в массы!
Data binding в массы!Data binding в массы!
Data binding в массы!
 
Android data binding
Android data bindingAndroid data binding
Android data binding
 
Building Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsBuilding Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture Components
 
Net conf BG xamarin lecture
Net conf BG xamarin lectureNet conf BG xamarin lecture
Net conf BG xamarin lecture
 
10.Local Database & LINQ
10.Local Database & LINQ10.Local Database & LINQ
10.Local Database & LINQ
 
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
 
Green dao
Green daoGreen dao
Green dao
 
Test Automation for NoSQL Databases
Test Automation for NoSQL DatabasesTest Automation for NoSQL Databases
Test Automation for NoSQL Databases
 
Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018
 
Android architecture components with cloud firestore
Android architecture components with cloud firestoreAndroid architecture components with cloud firestore
Android architecture components with cloud firestore
 
Entity framework practices
Entity framework practicesEntity framework practices
Entity framework practices
 

Recently uploaded

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 

Android data binding

  • 2. About Me 11 binding.setPresenter( new Presenter( “Ajit Singh”, “Developer”, “github.com/ajitsing”, “www.singhajit.com” ) );
  • 3. Agenda 11 • What is Data Binding • Why Data Binding? • Live Coding / Demo • Take Aways
  • 4. 10 What data binding do? How would it help me?
  • 5. Whats the problem with below code 11 Task task = tasks.get(position);
 
 TextView taskTitle = (TextView) taskView.findViewById(R.id.task_title);
 TextView taskDescription = (TextView) taskView.findViewById(R.id.task_description);
 TextView creationDate = (TextView) taskView.findViewById(R.id.creation_date); TextView reminderTime = (TextView) taskView.findViewById(R.id.reminder_timer);
 
 taskTitle.setText(task.getTitle());
 taskDescription.setText(task.getDescription());
 creationDate.setText(task.getCreationDate()); reminderTime.setText(task.getReminderTime());
  • 6. Again.. 11 Button addTaskButton = (Button) findViewById(R.id.add_task);
 
 addTaskButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 presenter.onAddTask();
 }
 });
 }
  • 7. 10 How to eliminate this boiler plate code??
  • 8. Eliminate Boiler Plate Code 11 Task task = tasks.get(position);
 
 binding.taskTitle.setText(task.getTitle());
 binding.taskDescription.setText(task.getDescription());
 binding.creationDate.setText(task.getCreationDate()); binding.reminderTime.setText(task.getReminderTime());
  • 9. 10 Is it a viewHolder??
  • 10. Eliminate Boiler Plate Code 11 <Button
 android:id="@+id/add_task"
 android:onClick="@{presenter.onAddTask}"
 …… /> binding.setTask(task);
  • 11. Data Binding 11 • Developed by Google • Built in inside gradle plugin • No external dependency needed
  • 12. Enable Data Binding 11 android { …… dataBinding { enabled = true } }
  • 13. Data Binding Layout Files 11 <layout>
 <data class="UserBinding">
 <import type=“com.ajit.singh.demo.util.AppUtil"/>
 <variable
 name="viewModel"
 type="com.ajit.singh.demo.viewmodel.UserViewModel"/>
 </data> <TextView
 android:text=“@{viewModel.name}"
 android:layout_width="wrap_content"
 android:layout_height=“wrap_content"/> </layout>

  • 14. User Event Handling 11 <Button
 android:id="@+id/add_task"
 android:onClick="@{(view) -> handler.onAddTask(viewModel)}"
 android:layout_width=“wrap_content"
 android:layout_height="wrap_content"/>
  • 15. Binding Expressions 11 • android:text="@{String.valueOf(index + 1)}” • android:text="@{user.displayName ?? user.lastName}” • android:text="@{user.displayName != null ? user.displayName : user.lastName}" • android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}” • android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"
  • 16. Keywords Not Supported In Binding 11 • new • this • super
  • 17. Inflating Layout 11 UserBinding binding = DataBindingUtil.setContentView(this, R.layout.user_layout); OR DataBindingUtil.inflate(inflator, R.layout.user_layout, parent, false); binding.setUser(new User(“Ajit Singh”)); TextView userName = (TextView) findViewById(R.id.user_name); userName.setText(“Ajit Singh”);
  • 18. ViewModel 11 public class User extends BaseObservable { @Bindable
 public String getName() {
 return name;
 } public void setName(String name) {
 this.name = name;
 notifyPropertyChanged(BR.name);
 } }
  • 21. Demo 11 One way binding with static data
  • 22. What we did? 11 • Wrap Parent ViewGroup in <layout> tag • Move all namespaces to <layout> tag • Define meta data of binding in <data> tag • Inflate layout using DataBindingUtil • set the variables defined in <data> tag in binding
  • 24. What we did? 11 • Created a viewModel to hold view state • Extend viewModel with BaseObservable • Create setters for the fields • Notify when data changes in viewModel • @={viewModel.title}
  • 26. Custom Binding 11 @BindingAdapter("setAdapter")
 public static void setAdapter(AdapterView view, BaseAdapter adapter) {
 view.setAdapter(adapter);
 } <ListView
 android:id="@+id/tasks_list"
 app:setAdapter="@{TasksAdapter.newInstance(viewModel.tasks)}"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
  • 27. Take Aways… 11 • Use one way binding to update data on UI • Two way binding to update view model from UI • Don’t extend viewModel with BaseObservable
 for static data • Use custom binding to avoid duplication and make code readable in layout • Do not write too much logic in layout files
  • 28. THANK YOU For questions or suggestions: Ajit Singh ajitsing@thoughtworks.com