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

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...Jim Markwith
 
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 2Sip Sipiapa
 
Ya vybirayu professiyu_v_it
Ya vybirayu professiyu_v_itYa vybirayu professiyu_v_it
Ya vybirayu professiyu_v_itsavinovama
 
интернет магазин
интернет магазининтернет магазин
интернет магазинsavinovama
 
Newsroom org models (iapa)
Newsroom org models (iapa)Newsroom org models (iapa)
Newsroom org models (iapa)Sip Sipiapa
 
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 AbandonmentMaven Infosoft Pvt Ltd.
 
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.Higor Prudencio
 

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

Effective Android Data Binding
Effective Android Data BindingEffective Android Data Binding
Effective Android Data BindingEric Maxwell
 
SharePoint Conference 2018 - APIs, APIs everywhere!
SharePoint Conference 2018 - APIs, APIs everywhere!SharePoint Conference 2018 - APIs, APIs everywhere!
SharePoint Conference 2018 - APIs, APIs everywhere!Sébastien Levert
 
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!Sébastien Levert
 
APIs, APIs Everywhere!
APIs, APIs Everywhere!APIs, APIs Everywhere!
APIs, APIs Everywhere!BIWUG
 
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?gedoplan
 
Deep dive into Android Data Binding
Deep dive into Android Data BindingDeep dive into Android Data Binding
Deep dive into Android Data BindingRadek Piekarz
 
Data binding в массы!
Data binding в массы!Data binding в массы!
Data binding в массы!Artjoker
 
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 ComponentsHassan Abid
 
Net conf BG xamarin lecture
Net conf BG xamarin lectureNet conf BG xamarin lecture
Net conf BG xamarin lectureTsvyatko Konov
 
10.Local Database & LINQ
10.Local Database & LINQ10.Local Database & LINQ
10.Local Database & LINQNguyen Tuan
 
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...Infinum
 
Test Automation for NoSQL Databases
Test Automation for NoSQL DatabasesTest Automation for NoSQL Databases
Test Automation for NoSQL DatabasesTobias Trelle
 
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 2018Hassan Abid
 
Android architecture components with cloud firestore
Android architecture components with cloud firestoreAndroid architecture components with cloud firestore
Android architecture components with cloud firestorePankaj Rai
 
Entity framework practices
Entity framework practicesEntity framework practices
Entity framework practicesMinh Ng
 

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

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
+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
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
+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...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

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