Presentation from the Toronto TechHub user group on Android development. Introducing basic development concepts with Eclipse and then switching to Xamarin.Android and Visual Studio.
6. What Makes an Android
Application?
•
•
•
•
Presentation layer
Derive from Activity
Use Views
Similar to forms in the
desktop world
• Worker processes in
application
• Invisible
• Responsible for
updating data
sources, activities,
notifications
• Shareable data store
• Preferred way to
share data across
applications
Activities
Services
Content
Providers
• Message-passing
framework
• Broadcast messages
to target
activity/service
• Broadcast consumers
• Filtered by criteria
• Listen to intents that
match the criteria
• Enables notifications
without interrupting
the current activity
• Device notification
area
Intents
Broadcast
Receivers
Notifications
7. Resources
Resources are non-code application parts
Android resources include
images, strings, simple
values, animations, themes, etc.
Best to keep separated/external from code
External resources are easier to
maintain, upgrade, and manage (…and localize!)
Created under the res folder
8. Layout Resources
Layouts specify the UI
Decouple presentation layer from code
Enable designing UI in XML
Can be referenced as any other resource from other
layouts
Usually, each layout XML file = view
9. Code and User Interface
Separation
Strive to define most of the UI in XML files, and
write only code in Java files
Clean code/UI separation provides flexibility and
easy maintenance
Makes it easier to adjust for various types of
hardware devices (similar to resource localization)
UI elements can be manipulated from code
Use findViewById to get UI element instance from
code
11. Localization
Resources make localization easy
Create a language-specific folder structure alongside
the main folder structure
Folder name includes qualifiers
+ res
+ values
+ strings.xml
+ values-fr
+ strings.xml
+ values-fr-rCA
+ strings.xml
12. What Is an Activity?
An activity represents a screen
Using Views to provide UI
Extends the Activity class
To navigate screens, start a new activity using an intent
By default, activities occupy the entire screen
Can create semi-transparent/floating activities
Last-in-first-out activity stack
New activity pushes foreground activity down into stack
Navigating back or finishing an activity pops from the
stack the previous activity
13. Creating Activities
Extend the Activity class
The base class presents an empty screen
Encapsulates window display handling functionality
User interfaces are created using Views
Can create UI from layout or from View-derived
instances
//Using a layout resource identifier:
setContentView(R.layout.main);
//Using a View-derived instance:
TextView text = new TextView(this);
text.setText("New text!");
setContentView(text);
14. Multiple Activities
All activities must be declared in the application
manifest
<activity android:name=".SecondaryActivity"/>
Span a sub-activity using an Intent
Intent launch = new Intent(this, SecondaryActivity.class);
startActivity(launch);
15. Layouts
Most commonly used layouts
Layout
Description
FrameLayout
Pins child views to the top left corner. Adding multiple
children stacks each new child on top of the previous,
with each new view obscuring the last.
LinearLayout
Adds each child view in a straight line, either vertically or
horizontally.
RelativeLayout
Enables defining the positions of each of the child views
relative to each other and the screen boundaries.
TableLayout
Lay out views using a grid of rows and columns.
Multiple layouts can be mixed together
16. Selectors and Lists
ListView provides a convenient UI
for value selection from a long list
Presents multiple items on screen
Spinner provides UI for value
selection
Presents only a single value at a time
Drop-down overlay of selectable items
18. Xamarin: C# on 3 Billion Devices
Xamarin provides a .NET runtime for iOS and
Android development in C#
Proprietary IDE: Xamarin Studio
Full Visual Studio integration
Folders structure:Src – hold all the source filesGen – holds auto-generated filesRes – holds the resources of the application. Subfolders (like drawable-*) holds graphics optimized for different resolutionsRes/layout – holds the layout definition XML filesRes/values – holds XML files with predefined values, such as string resource dictionaries, constants, etc.Android (2.3.3) – holds a reference to JAR file with Android APIsReference Libraries/”MyLibrary” holds a reference to 3rd party libraries and JARs referenced in the projectAsses – holds other static application’s assets which will be deployed onto deviceAndroidManifest.xml – application manifest file describing the application being built and what components—activities, services, etc.—are being supplied by that application Res folder can hold additional directories such as menu, raw, xml with XML-based specifications of various parts of the application.