SlideShare uma empresa Scribd logo
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

Mais conteúdo relacionado

Destaque

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
 

Destaque (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.
 

Semelhante a 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
 

Semelhante a 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
 

Último

Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAlluxio, Inc.
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)Max Lee
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Anthony Dahanne
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring SoftwareMera Monitor
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesNeo4j
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Krakówbim.edu.pl
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisNeo4j
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabbereGrabber
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdfkalichargn70th171
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzisteffenkarlsson2
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Soroosh Khodami
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024Shane Coughlan
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfDeskTrack
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfkalichargn70th171
 

Último (20)

Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring Software
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 

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