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

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
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
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
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
 

Recently uploaded (20)

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
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
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
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...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
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
 

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