SlideShare a Scribd company logo
HIT3328 / HIT8328 Software Development for
Mobile Devices
Dr. Rajesh Vasa, 2011
1
Twitter: @rvasa
Blog: http://rvasa.blogspot.com
Lecture 07
Dialogs, Tabs and
Lists
R. Vasa, 20122
Lecture Overview
•A short Detour into Design Patterns
•Dialogs
•Tabbed Layouts
•Enhancing Lists
R. Vasa, 20123
Design Patterns
•A general reusable solution to a commonly
occurring problem in software design
•Typically cover,
•Algorithm strategy
•Computational design
•Execution
•Implementation strategy
•Structural design
R. Vasa, 20124
API designers use these
patterns
•API designers use a number of these
patterns
•If you can recognise the patterns used -- it
will help you,
•learn the API faster & use it better
•understand the underlying problem that
motivated designers to choose the pattern
•Improve your own code by adapting the
pattern (assuming the pattern used by API
designer is good)
R. Vasa, 20125
API Design - Android Example
•Android API designers built their SDK
around the Method Chaining and Builder
pattern in many instances
•What are they? Why have they used it?
R. Vasa, 20126
Quick Concept Clarification
•Before we go into the pattern in depth
•We need to know the following:
•Mutable Vs Immutable objects
•Command-Query Separation Principle
•Fluent Interfaces
R. Vasa, 20127
Mutable vs Immutable Objects
•In Object oriented languages -- some
objects are designed to be immutable
•That is, once created -- they are constants
•In Java -- Strings are immutable
•Constant (immutable) offer better memory
allocation strategies
•However, if we change these objects too
often -- then we pay a high price
R. Vasa, 20128
Immutable Vs Mutable Objects
•In Java, if we perform a large number of
string modifications
•It is better to use StringBuffer
•StringBuffer is Mutable (can be changed)
•During design (over years) we discovered
that mutable objects should have certain
design properties
•But -- our thinking was boxed!!!!
R. Vasa, 20129
Command - Query Separation
Principle
•Good Object Oriented design should ensure
that a method is either a command or a
query
•Simply put,
•getName() -- Query -- Should not change
state
•setName() -- Command -- Can change state
•Methods that perform both are considered
poor practice
This principle boxed our thinking (to some extentThis principle boxed our thinking (to some extent)
R. Vasa, 20121
Command - Query Separation
Principle
•Consequences of this principle,
•“set” Methods should not return (i.e. void)
•This principle has influenced quite a large
pool of API code (Java, .NET and others)
•Side-effect,
•We end up writing a large number of set
methods
•Often one per line
R. Vasa, 20121
Command-Query Principle At
Work
private void makeNormalOrder(Customer customer)
{
Order o1 = new Order();
customer.addOrder(o1);
OrderLine line1 = new OrderLine(6, Product.find("TAL"));
o1.addLine(line1);
OrderLine line2 = new OrderLine(5, Product.find("HPK"));
o1.addLine(line2);
OrderLine line3 = new OrderLine(3, Product.find("LGV"));
o1.addLine(line3);
line2.setSkippable(true);
o1.setRush(true);
}
Commands and Queries are clearly separatedCommands and Queries are clearly separated
Example from:
http://martinfowler.com/bliki/FluentInterface.html
R. Vasa, 20121
Command-Query Principle At
Work
private void makeNormalOrder(Customer customer)
{
Order o1 = new Order();
customer.addOrder(o1);
OrderLine line1 = new OrderLine(6, Product.find("TAL"));
o1.addLine(line1);
OrderLine line2 = new OrderLine(5, Product.find("HPK"));
o1.addLine(line2);
OrderLine line3 = new OrderLine(3, Product.find("LGV"));
o1.addLine(line3);
line2.setSkippable(true);
o1.setRush(true);
}
Example from:
http://martinfowler.com/bliki/FluentInterface.html
Code is needlessly messy, long and convolutedCode is needlessly messy, long and convoluted
R. Vasa, 20121
Fluent Interface
•The concept is that we should write code
that is better to read
•That is, it communicates the intent much
more clearly
R. Vasa, 20121
Fluent Interfaces Example
private void makeFluentOrder(Customer customer)
{
customer.newOrder()
.with(6, "TAL")
.with(5, "HPK").skippable()
.with(3, "LGV")
.priorityRush();
}
Example from:
http://martinfowler.com/bliki/FluentInterface.html
Easier to read -- but is C-Q still followed?Easier to read -- but is C-Q still followed?
Commands change state and return --
but intent of the principle is not really
violated
Commands change state and return --
but intent of the principle is not really
violated
R. Vasa, 20121
Fluent Interfaces & C-Q
Principle
private void makeFluentOrder(Customer customer)
{
customer.newOrder()
.with(6, "TAL")
.with(5, "HPK").skippable()
.with(3, "LGV")
.priorityRush();
}
Example from:
http://martinfowler.com/bliki/FluentInterface.html
with() methods [commands]
update and return the order
object
with() methods [commands]
update and return the order
object
public Order with(int i, String string)public Order with(int i, String string)
R. Vasa, 20121
Method Chaining Pattern
private void makeFluentOrder(Customer customer)
{
customer.newOrder()
.with(6, "TAL")
.with(5, "HPK").skippable()
.with(3, "LGV")
.priorityRush();
}
Method Chaining Pattern - Object
is mutated and returned back
Method Chaining Pattern - Object
is mutated and returned back
R. Vasa, 20121
Back to Android API Design
•Android SDK designers are influenced by
these ideas around fluent interfaces and
method chaining
•They combine it with the Builder Pattern to
help construct layouts and views
•This pattern is highlighted in the examples
that follow ....
R. Vasa, 20121
Lecture Roadmap - Where are
we?
•A short Detour into Design Patterns
•Dialogs
•Tabbed Layouts
•Enhancing Lists
R. Vasa, 20121
Dialogs
• How long have I been alive?
Date Dialog Alert Dialog
R. Vasa, 20122
Dialogs are flexible
Date Dialog
(SDK Provided)
Custom Alert
Dialog
Title and Icon
R. Vasa, 20122
Dialogs are flexible
Date Dialog
(SDK Provided)
Custom Alert
Dialog
Title and Icon
Message
R. Vasa, 20122
Dialogs are flexible
Date Dialog
(SDK Provided)
Custom Alert
Dialog
Title and Icon
Message
Control
Buttons
R. Vasa, 20122
Dialogs are useful for error
messages
Custom Alert
Dialog
Ideal for simple error messages
R. Vasa, 20122
Date Picker Dialog
private MutableDateTime mdt = new
MutableDateTime(1990,1,1,10,10,0,0);
Mutable Date Time from Joda Time library (see
sample code)
We can set a default date value for the dialog
R. Vasa, 20122
Short Problem - Usability
Question
•In this context, what should be the default
date shown?
20 years ago Current
Should it be something else? -- Is it important?
R. Vasa, 20122
Constructing Custom Alter
Dialogs
This code uses a built-in icon,
but can be any appropriate
drawable
This code uses a built-in icon,
but can be any appropriate
drawable
See
http://androiddrawableexplorer.appspot.com/
for a full list of built-in drawable resources
and their names
R. Vasa, 20122
Constructing Custom Alter
Dialogs
We can set buttons
(positive/negative/neutral)
We can set buttons
(positive/negative/neutral)
R. Vasa, 20122
Constructing Custom Alter
Dialogs
Button Click
implementatio
n
Button Click
implementatio
n
R. Vasa, 20122
Deconstructing the Patterns in
Use
Dialog Builder - Inner ClassDialog Builder - Inner Class
Method Chaining PatternMethod Chaining Pattern
R. Vasa, 20123
Showing a Dialog
public void onBackPressed()
{
showDialog(EXIT_DIALOG);
}
This is a method
provided by the
Activity class
This is a method
provided by the
Activity class
We have to provide
these constants for
the call-back
We have to provide
these constants for
the call-back
R. Vasa, 20123
Dialogs are Constructed via
Call-Backs
public void onBackPressed()
{
showDialog(EXIT_DIALOG);
}
Triggers
the call
back
This is
displayed to
the user
This is
displayed to
the user
R. Vasa, 20123
Why go through so much
trouble?public void onBackPressed()
{
showDialog(EXIT_DIALOG);
}
Because dialogs needs a
parent Activity for life cycle
management
Because dialogs needs a
parent Activity for life cycle
management
Offers
separation of
concerns
Offers
separation of
concerns
R. Vasa, 20123
Lecture Roadmap - Where are
we?
•A short Detour into Design Patterns
•Dialogs
•Tabbed Layouts
•Enhancing Lists
R. Vasa, 20123
Tabbed Layouts
R. Vasa, 20123
Tabbed Layouts - Concepts
Tab Host (container)
R. Vasa, 20123
Tabbed Layouts - Concepts
Tab Widget
Tab Host (container)
R. Vasa, 20123
Tabbed Layouts - Concepts
Tab Host (container)
Tab Widget
Frame Layout is used to
switch the visible content
for a tab
R. Vasa, 20123
Layout of a Tabbed App.
bHost contains the TabWidget and the FrameLayo
R. Vasa, 20123
Layout of a Tabbed App.
Two views in
the frame
layout to
switch around
Two views in
the frame
layout to
switch around
R. Vasa, 20124
Wiring up the Activity
Method chaining usedMethod chaining used
R. Vasa, 20124
Wiring up the Activity
TabSpec
populates the tab
widget
TabSpec
populates the tab
widget
R. Vasa, 20124
Tabs need multiple icon
resources
Selected Icon Unselected Icon
We have to provide both icons to improve look & feelWe have to provide both icons to improve look & feel
R. Vasa, 20124
Working with Multiple Icons
State information is provided as a drawable resourceState information is provided as a drawable resource
Note: XML file is
placed in the
drawable folder
R. Vasa, 20124
Using Icon State Information
Icon selection is managed
by SDK (via convention)
Icon selection is managed
by SDK (via convention)
R. Vasa, 20124
Tabs can load different
activities
•If a tab needs to start a new activity,
•We need to start it with Intents
R. Vasa, 20124
Tabs can load different
activities
Intent is reused in this example --
because this method copies the
value internally
Intent is reused in this example --
because this method copies the
value internally
Method Chaining used hereMethod Chaining used here
R. Vasa, 20124
Lecture Roadmap - Where are
we?
•A short Detour into Design Patterns
•Dialogs
•Tabbed Layouts
•Enhancing Lists
R. Vasa, 20124
Enhancing Lists
Latitude / Longitude
Example
Drop down
showing
matching options
Drop down
showing
matching options
R. Vasa, 20124
Recall: Lists get data via an
Adapter
ListView
Data Source
Adapter
R. Vasa, 20125
We can tie an adapter to Edit
Text View
Data Adapter set for AutoCompleteTextView
R. Vasa, 20125
Listening to Selection
cityView.addTextChangedListener(this);
public class LatLongActivity extends Activity implements TextWatcher
R. Vasa, 20125
Listening to Selection
cityView.addTextChangedListener(this);
public class LatLongActivity extends Activity implements TextWatcher
R. Vasa, 20125
Lecture Roadmap - Where are
we?
•A short Detour into Design Patterns
•Dialogs
•Tabbed Layouts
•Enhancing Lists (with images)
R. Vasa, 20125
Lists with Images
Latitude / Longitude
Example
Melbourne & Sydney have
special images. Rest use
same icon
Melbourne & Sydney have
special images. Rest use
same icon
R. Vasa, 20125
Custom List Row
We can specify a
custom layout for rows
in a list
We can specify a
custom layout for rows
in a list
R. Vasa, 20125
Custom List Row
Layout provides the
default icon to show for
each row item
Layout provides the
default icon to show for
each row item
R. Vasa, 20125
We provide a custom row
adapter
•The standard adapter will display using a
pre-defined (SDK provided) layout
•We have to override this behaviour with our
own “row” rendering functionality
// use custom row adapter
setListAdapter(new RowIconAdapter());
public class LatLongActivity extends ListActivity
Note: See Sample Code for details
R. Vasa, 20125
Custom Row Adapter
class RowIconAdapter extends ArrayAdapter<String>
{
public RowIconAdapter()
{super(LatLongActivity.this, R.layout.listrow, R.id.row_label, cityNames);}
public View getView(int pos, View cView, ViewGroup parent)
{
View row = super.getView(pos, cView, parent); // DEFAULT ROW
ImageView icon = (ImageView) row.findViewById(R.id.row_icon);
String city = cityNames[pos];
if (city.startsWith("Melb"))
icon.setImageResource(R.drawable.melbourne);
else if (city.startsWith("Syd"))
icon.setImageResource(R.drawable.sydney);
else
icon.setImageResource(R.drawable.location);
return row;
}}
R. Vasa, 20125
A Few List View Related Issues
As we scroll down, we get more
cities
R. Vasa, 20126
A Few List View Related Issues
View objects on Rows not
shown are destroyed as we
scroll up/down
View objects on Rows not
shown are destroyed as we
scroll up/down
Only 7
rows
(views) are
kept in
RAM
R. Vasa, 20126
A Few List View Related Issues
View objects on Rows not shown
are destroyed as we scroll
up/down
View objects on Rows not shown
are destroyed as we scroll
up/down
Design Tip: Make sure that rows
are constructed as quickly as
possible
Design Tip: Make sure that rows
are constructed as quickly as
possible
R. Vasa, 20126
Lecture Roadmap - Where are
we?
•A short Detour into Design Patterns
•Dialogs
•Tabbed Layouts
•Enhancing Lists

More Related Content

PDF
HIT3328 - Chapter0602 - Sketching Apps
PDF
HIT3328 - Chapter0601 - Menus and Lists
PDF
HIT3328 - Chapter0702 - Navigation Flow and Design Approach
PPT
Frames for shot list
PPTX
CLOVER: PROPERTY GRAPH BASED METADATA MANAGEMENT SERVICE
PDF
CSC1100 - Chapter06 - Operating System & Utility Programs
PDF
CSC1100 - Chapter03 - Input
PDF
HIT3328 - Chapter05 - Working with Forms
HIT3328 - Chapter0602 - Sketching Apps
HIT3328 - Chapter0601 - Menus and Lists
HIT3328 - Chapter0702 - Navigation Flow and Design Approach
Frames for shot list
CLOVER: PROPERTY GRAPH BASED METADATA MANAGEMENT SERVICE
CSC1100 - Chapter06 - Operating System & Utility Programs
CSC1100 - Chapter03 - Input
HIT3328 - Chapter05 - Working with Forms

Similar to HIT3328 - Chapter0701 - Dialogs, Tabs and Lists (20)

PPTX
Android application componenets for android app development
PDF
ApiDesign
PDF
API design
PPT
"Android" mobilių programėlių kūrimo įvadas #2
PDF
HIT3328 - Chapter03 - Simple Interactive Apps
PPTX
Android Study Jams - Session 2
PDF
Api design
PPTX
Android Study Jams Session 4
PDF
Thinking clearly about API design
PPTX
Android Apps Development Basic
PPTX
Android design patterns in mobile application development presentation
PPTX
Android App Development (Basics)
PPTX
2 Day Android Workshop
PPTX
hema ppt (2).pptx
PPTX
Wi phug windows phone development from z to a
PDF
Designing JS APis
PPTX
Advanced MVVM in Windows 8
PDF
FluentInterfaceHandsOn
PDF
The Good, the Bad and the Ugly things to do with android
PDF
Windows Store App Development C And Xaml 1st Edition Pete Brown
Android application componenets for android app development
ApiDesign
API design
"Android" mobilių programėlių kūrimo įvadas #2
HIT3328 - Chapter03 - Simple Interactive Apps
Android Study Jams - Session 2
Api design
Android Study Jams Session 4
Thinking clearly about API design
Android Apps Development Basic
Android design patterns in mobile application development presentation
Android App Development (Basics)
2 Day Android Workshop
hema ppt (2).pptx
Wi phug windows phone development from z to a
Designing JS APis
Advanced MVVM in Windows 8
FluentInterfaceHandsOn
The Good, the Bad and the Ugly things to do with android
Windows Store App Development C And Xaml 1st Edition Pete Brown
Ad

More from Yhal Htet Aung (13)

PDF
HIT3328 - Chapter02 - Foundation and Tools
PDF
HIT3328 - Chapter04 - Complex Interactions
PDF
HIT3328 - Chapter01 - Platforms and Devices
PDF
CSC1100 - Chapter11 - Programming Languages and Program Development
PDF
CSC1100 - Chapter10 - Information System
PDF
CSC1100 - Chapter09 - Computer Security, Ethics and Privacy
PDF
CSC1100 - Chapter08 - Database Management
PDF
CSC1100 - Chapter07 - Communications & Networks
PDF
CSC1100 - Chapter05 - Storage
PDF
CSC1100 - Chapter04 - Output
PDF
CSC1100 - Chapter02 - Components of the System Unit
PDF
CSC1100 - Chapter01 - Overview of Using Computers
PDF
CSC1100 - Chapter12 - Flow Charts
HIT3328 - Chapter02 - Foundation and Tools
HIT3328 - Chapter04 - Complex Interactions
HIT3328 - Chapter01 - Platforms and Devices
CSC1100 - Chapter11 - Programming Languages and Program Development
CSC1100 - Chapter10 - Information System
CSC1100 - Chapter09 - Computer Security, Ethics and Privacy
CSC1100 - Chapter08 - Database Management
CSC1100 - Chapter07 - Communications & Networks
CSC1100 - Chapter05 - Storage
CSC1100 - Chapter04 - Output
CSC1100 - Chapter02 - Components of the System Unit
CSC1100 - Chapter01 - Overview of Using Computers
CSC1100 - Chapter12 - Flow Charts
Ad

Recently uploaded (20)

PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
1. Introduction to Computer Programming.pptx
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Reach Out and Touch Someone: Haptics and Empathic Computing
A comparative analysis of optical character recognition models for extracting...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
TLE Review Electricity (Electricity).pptx
Programs and apps: productivity, graphics, security and other tools
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
cloud_computing_Infrastucture_as_cloud_p
Diabetes mellitus diagnosis method based random forest with bat algorithm
1. Introduction to Computer Programming.pptx
SOPHOS-XG Firewall Administrator PPT.pptx
Heart disease approach using modified random forest and particle swarm optimi...
Tartificialntelligence_presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

HIT3328 - Chapter0701 - Dialogs, Tabs and Lists

  • 1. HIT3328 / HIT8328 Software Development for Mobile Devices Dr. Rajesh Vasa, 2011 1 Twitter: @rvasa Blog: http://rvasa.blogspot.com Lecture 07 Dialogs, Tabs and Lists
  • 2. R. Vasa, 20122 Lecture Overview •A short Detour into Design Patterns •Dialogs •Tabbed Layouts •Enhancing Lists
  • 3. R. Vasa, 20123 Design Patterns •A general reusable solution to a commonly occurring problem in software design •Typically cover, •Algorithm strategy •Computational design •Execution •Implementation strategy •Structural design
  • 4. R. Vasa, 20124 API designers use these patterns •API designers use a number of these patterns •If you can recognise the patterns used -- it will help you, •learn the API faster & use it better •understand the underlying problem that motivated designers to choose the pattern •Improve your own code by adapting the pattern (assuming the pattern used by API designer is good)
  • 5. R. Vasa, 20125 API Design - Android Example •Android API designers built their SDK around the Method Chaining and Builder pattern in many instances •What are they? Why have they used it?
  • 6. R. Vasa, 20126 Quick Concept Clarification •Before we go into the pattern in depth •We need to know the following: •Mutable Vs Immutable objects •Command-Query Separation Principle •Fluent Interfaces
  • 7. R. Vasa, 20127 Mutable vs Immutable Objects •In Object oriented languages -- some objects are designed to be immutable •That is, once created -- they are constants •In Java -- Strings are immutable •Constant (immutable) offer better memory allocation strategies •However, if we change these objects too often -- then we pay a high price
  • 8. R. Vasa, 20128 Immutable Vs Mutable Objects •In Java, if we perform a large number of string modifications •It is better to use StringBuffer •StringBuffer is Mutable (can be changed) •During design (over years) we discovered that mutable objects should have certain design properties •But -- our thinking was boxed!!!!
  • 9. R. Vasa, 20129 Command - Query Separation Principle •Good Object Oriented design should ensure that a method is either a command or a query •Simply put, •getName() -- Query -- Should not change state •setName() -- Command -- Can change state •Methods that perform both are considered poor practice This principle boxed our thinking (to some extentThis principle boxed our thinking (to some extent)
  • 10. R. Vasa, 20121 Command - Query Separation Principle •Consequences of this principle, •“set” Methods should not return (i.e. void) •This principle has influenced quite a large pool of API code (Java, .NET and others) •Side-effect, •We end up writing a large number of set methods •Often one per line
  • 11. R. Vasa, 20121 Command-Query Principle At Work private void makeNormalOrder(Customer customer) { Order o1 = new Order(); customer.addOrder(o1); OrderLine line1 = new OrderLine(6, Product.find("TAL")); o1.addLine(line1); OrderLine line2 = new OrderLine(5, Product.find("HPK")); o1.addLine(line2); OrderLine line3 = new OrderLine(3, Product.find("LGV")); o1.addLine(line3); line2.setSkippable(true); o1.setRush(true); } Commands and Queries are clearly separatedCommands and Queries are clearly separated Example from: http://martinfowler.com/bliki/FluentInterface.html
  • 12. R. Vasa, 20121 Command-Query Principle At Work private void makeNormalOrder(Customer customer) { Order o1 = new Order(); customer.addOrder(o1); OrderLine line1 = new OrderLine(6, Product.find("TAL")); o1.addLine(line1); OrderLine line2 = new OrderLine(5, Product.find("HPK")); o1.addLine(line2); OrderLine line3 = new OrderLine(3, Product.find("LGV")); o1.addLine(line3); line2.setSkippable(true); o1.setRush(true); } Example from: http://martinfowler.com/bliki/FluentInterface.html Code is needlessly messy, long and convolutedCode is needlessly messy, long and convoluted
  • 13. R. Vasa, 20121 Fluent Interface •The concept is that we should write code that is better to read •That is, it communicates the intent much more clearly
  • 14. R. Vasa, 20121 Fluent Interfaces Example private void makeFluentOrder(Customer customer) { customer.newOrder() .with(6, "TAL") .with(5, "HPK").skippable() .with(3, "LGV") .priorityRush(); } Example from: http://martinfowler.com/bliki/FluentInterface.html Easier to read -- but is C-Q still followed?Easier to read -- but is C-Q still followed? Commands change state and return -- but intent of the principle is not really violated Commands change state and return -- but intent of the principle is not really violated
  • 15. R. Vasa, 20121 Fluent Interfaces & C-Q Principle private void makeFluentOrder(Customer customer) { customer.newOrder() .with(6, "TAL") .with(5, "HPK").skippable() .with(3, "LGV") .priorityRush(); } Example from: http://martinfowler.com/bliki/FluentInterface.html with() methods [commands] update and return the order object with() methods [commands] update and return the order object public Order with(int i, String string)public Order with(int i, String string)
  • 16. R. Vasa, 20121 Method Chaining Pattern private void makeFluentOrder(Customer customer) { customer.newOrder() .with(6, "TAL") .with(5, "HPK").skippable() .with(3, "LGV") .priorityRush(); } Method Chaining Pattern - Object is mutated and returned back Method Chaining Pattern - Object is mutated and returned back
  • 17. R. Vasa, 20121 Back to Android API Design •Android SDK designers are influenced by these ideas around fluent interfaces and method chaining •They combine it with the Builder Pattern to help construct layouts and views •This pattern is highlighted in the examples that follow ....
  • 18. R. Vasa, 20121 Lecture Roadmap - Where are we? •A short Detour into Design Patterns •Dialogs •Tabbed Layouts •Enhancing Lists
  • 19. R. Vasa, 20121 Dialogs • How long have I been alive? Date Dialog Alert Dialog
  • 20. R. Vasa, 20122 Dialogs are flexible Date Dialog (SDK Provided) Custom Alert Dialog Title and Icon
  • 21. R. Vasa, 20122 Dialogs are flexible Date Dialog (SDK Provided) Custom Alert Dialog Title and Icon Message
  • 22. R. Vasa, 20122 Dialogs are flexible Date Dialog (SDK Provided) Custom Alert Dialog Title and Icon Message Control Buttons
  • 23. R. Vasa, 20122 Dialogs are useful for error messages Custom Alert Dialog Ideal for simple error messages
  • 24. R. Vasa, 20122 Date Picker Dialog private MutableDateTime mdt = new MutableDateTime(1990,1,1,10,10,0,0); Mutable Date Time from Joda Time library (see sample code) We can set a default date value for the dialog
  • 25. R. Vasa, 20122 Short Problem - Usability Question •In this context, what should be the default date shown? 20 years ago Current Should it be something else? -- Is it important?
  • 26. R. Vasa, 20122 Constructing Custom Alter Dialogs This code uses a built-in icon, but can be any appropriate drawable This code uses a built-in icon, but can be any appropriate drawable See http://androiddrawableexplorer.appspot.com/ for a full list of built-in drawable resources and their names
  • 27. R. Vasa, 20122 Constructing Custom Alter Dialogs We can set buttons (positive/negative/neutral) We can set buttons (positive/negative/neutral)
  • 28. R. Vasa, 20122 Constructing Custom Alter Dialogs Button Click implementatio n Button Click implementatio n
  • 29. R. Vasa, 20122 Deconstructing the Patterns in Use Dialog Builder - Inner ClassDialog Builder - Inner Class Method Chaining PatternMethod Chaining Pattern
  • 30. R. Vasa, 20123 Showing a Dialog public void onBackPressed() { showDialog(EXIT_DIALOG); } This is a method provided by the Activity class This is a method provided by the Activity class We have to provide these constants for the call-back We have to provide these constants for the call-back
  • 31. R. Vasa, 20123 Dialogs are Constructed via Call-Backs public void onBackPressed() { showDialog(EXIT_DIALOG); } Triggers the call back This is displayed to the user This is displayed to the user
  • 32. R. Vasa, 20123 Why go through so much trouble?public void onBackPressed() { showDialog(EXIT_DIALOG); } Because dialogs needs a parent Activity for life cycle management Because dialogs needs a parent Activity for life cycle management Offers separation of concerns Offers separation of concerns
  • 33. R. Vasa, 20123 Lecture Roadmap - Where are we? •A short Detour into Design Patterns •Dialogs •Tabbed Layouts •Enhancing Lists
  • 35. R. Vasa, 20123 Tabbed Layouts - Concepts Tab Host (container)
  • 36. R. Vasa, 20123 Tabbed Layouts - Concepts Tab Widget Tab Host (container)
  • 37. R. Vasa, 20123 Tabbed Layouts - Concepts Tab Host (container) Tab Widget Frame Layout is used to switch the visible content for a tab
  • 38. R. Vasa, 20123 Layout of a Tabbed App. bHost contains the TabWidget and the FrameLayo
  • 39. R. Vasa, 20123 Layout of a Tabbed App. Two views in the frame layout to switch around Two views in the frame layout to switch around
  • 40. R. Vasa, 20124 Wiring up the Activity Method chaining usedMethod chaining used
  • 41. R. Vasa, 20124 Wiring up the Activity TabSpec populates the tab widget TabSpec populates the tab widget
  • 42. R. Vasa, 20124 Tabs need multiple icon resources Selected Icon Unselected Icon We have to provide both icons to improve look & feelWe have to provide both icons to improve look & feel
  • 43. R. Vasa, 20124 Working with Multiple Icons State information is provided as a drawable resourceState information is provided as a drawable resource Note: XML file is placed in the drawable folder
  • 44. R. Vasa, 20124 Using Icon State Information Icon selection is managed by SDK (via convention) Icon selection is managed by SDK (via convention)
  • 45. R. Vasa, 20124 Tabs can load different activities •If a tab needs to start a new activity, •We need to start it with Intents
  • 46. R. Vasa, 20124 Tabs can load different activities Intent is reused in this example -- because this method copies the value internally Intent is reused in this example -- because this method copies the value internally Method Chaining used hereMethod Chaining used here
  • 47. R. Vasa, 20124 Lecture Roadmap - Where are we? •A short Detour into Design Patterns •Dialogs •Tabbed Layouts •Enhancing Lists
  • 48. R. Vasa, 20124 Enhancing Lists Latitude / Longitude Example Drop down showing matching options Drop down showing matching options
  • 49. R. Vasa, 20124 Recall: Lists get data via an Adapter ListView Data Source Adapter
  • 50. R. Vasa, 20125 We can tie an adapter to Edit Text View Data Adapter set for AutoCompleteTextView
  • 51. R. Vasa, 20125 Listening to Selection cityView.addTextChangedListener(this); public class LatLongActivity extends Activity implements TextWatcher
  • 52. R. Vasa, 20125 Listening to Selection cityView.addTextChangedListener(this); public class LatLongActivity extends Activity implements TextWatcher
  • 53. R. Vasa, 20125 Lecture Roadmap - Where are we? •A short Detour into Design Patterns •Dialogs •Tabbed Layouts •Enhancing Lists (with images)
  • 54. R. Vasa, 20125 Lists with Images Latitude / Longitude Example Melbourne & Sydney have special images. Rest use same icon Melbourne & Sydney have special images. Rest use same icon
  • 55. R. Vasa, 20125 Custom List Row We can specify a custom layout for rows in a list We can specify a custom layout for rows in a list
  • 56. R. Vasa, 20125 Custom List Row Layout provides the default icon to show for each row item Layout provides the default icon to show for each row item
  • 57. R. Vasa, 20125 We provide a custom row adapter •The standard adapter will display using a pre-defined (SDK provided) layout •We have to override this behaviour with our own “row” rendering functionality // use custom row adapter setListAdapter(new RowIconAdapter()); public class LatLongActivity extends ListActivity Note: See Sample Code for details
  • 58. R. Vasa, 20125 Custom Row Adapter class RowIconAdapter extends ArrayAdapter<String> { public RowIconAdapter() {super(LatLongActivity.this, R.layout.listrow, R.id.row_label, cityNames);} public View getView(int pos, View cView, ViewGroup parent) { View row = super.getView(pos, cView, parent); // DEFAULT ROW ImageView icon = (ImageView) row.findViewById(R.id.row_icon); String city = cityNames[pos]; if (city.startsWith("Melb")) icon.setImageResource(R.drawable.melbourne); else if (city.startsWith("Syd")) icon.setImageResource(R.drawable.sydney); else icon.setImageResource(R.drawable.location); return row; }}
  • 59. R. Vasa, 20125 A Few List View Related Issues As we scroll down, we get more cities
  • 60. R. Vasa, 20126 A Few List View Related Issues View objects on Rows not shown are destroyed as we scroll up/down View objects on Rows not shown are destroyed as we scroll up/down Only 7 rows (views) are kept in RAM
  • 61. R. Vasa, 20126 A Few List View Related Issues View objects on Rows not shown are destroyed as we scroll up/down View objects on Rows not shown are destroyed as we scroll up/down Design Tip: Make sure that rows are constructed as quickly as possible Design Tip: Make sure that rows are constructed as quickly as possible
  • 62. R. Vasa, 20126 Lecture Roadmap - Where are we? •A short Detour into Design Patterns •Dialogs •Tabbed Layouts •Enhancing Lists