android: gui development - university of waterloocs349/s16/slides/8.1-android.pdf · d) • android...
TRANSCRIPT
Android: GUI Development
A brief introduction and getting-started guide.
Ove
rvie
w • What is Android?– Introduction– Architecture– Activities, Views, Intents
• Android Developer Tools– Installing the Android SDK– Setting up an AVD
• Demo– Creating a project, project structure– Building and deploying to an AVD– Using MVC
• Wrap-up– Tips & Tricks
2
Res
ourc
es
3
http://developer.android.com
http://developer.android.com/
Wha
t is
And
roid
? • Pervasive Mobile Platform– Runs on billions of mobile phones, tablets– World’s most popular & frequently installed mobile OS– Open Source (minimum-definition).
• Developing on Android– Multi-platform tools– Based on Java– Uses Android Studio IDE– Excellent third-party tools support
• See http://developer.android.com/tools/index.html
4
Arc
hite
ctur
e • Layered environment– Bottom tier: Linux 2.6 kernel– Mid tier: Android libraries/services– Top tier: Java applications
• Applications run in virtual machines (VM)– Each application has it’s own VM & address space– Restrictions on sharing resources and data– Dalvik virtual machine process
5
Arc
hite
ctur
e
6
Des
ign
Goa
ls • Applications provide multiple entry points; they’re distinct components that can be invoked individually.– Applications are typically composed of individual,
standalone screens.– Explicitly need to pass data between screens (reduces
overhead/memory).• Applications are dynamic
– You should provide different layouts (XML layouts, and resources) based on device characteristics.
– Applications should dynamically adjust based on device screen size and orientation.
7
http://developer.android.com/guide/index.html
Des
ign
Con
side
ratio
ns • Limited resources– Limited memory, processing power– Battery life is critical!
• Architectural tradeoffs– The OS aggressively flushes memory– Background computation is limited
• Unusual device characteristics– Small screen– Multiple orientations, dynamic layout– Single window/application running– Multi-touch input
8
Ove
rvie
w • What is Android?– Introduction– Architecture– Activities, Views, Intents
• Android Developer Tools– Installing the Android SDK– Setting up an AVD
• Demo– Creating a project, project structure– Building and deploying to an AVD– Using MVC
• Wrap-up– Tips & Tricks
9
Act
iviti
es • Different types of components that we can build in Android: applications, services, etc. (see chart below)
• A standard application component is an Activity– Typically represents a single screen– Main entry point (equivalent to main() method)– For most purposes, this is your application class!
Components Description
Activity Single-screen of an application
Service Long-running background application
Content provider Provides shared set of application data
Broadcast receiver Responds to system broadcast events
10
Act
ivity
Life
cycl
e • Activities have an explicit lifecycle– One activity runs at a time, others are paused in the background.– As users navigate through your application, they switch activities.
• Every activity has a state: run, paused, or stopped.- Changing state fires a corresponding activity method.
Running Paused/ Stopped
11
Act
ivity
Life
cycl
e • Common to switch between different activities/screens.– Activities can create other activities (“Back stack”)– Navigation forward/back through activities is typically triggered
by user actions
12
Inte
rrup
ted
Wor
kflo
w • Applications can stop at any time (i.e. user quits, OS kills it). – Each activity needs to manage its own state.– Activities have methods for saving and restoring state
13
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
Inte
nts • We use intents to pass data between activities/screens.
– Data structure holding an abstract description of an action.– Use Activity startActivity() method to launch with intent.
• Explicit (named activity) vs. implicit (capabilities, e.g. camera)
14
https://developer.android.com/guide/components/intents-filters.html
Opt
iona
l: Fr
agm
ents • Fragments can be thought of as
portions of a UI• An activity can contain (and
manage) multiple fragments.– Allows for dynamic
adjustments to the UI– Fragments have their own
lifecycle, and their own layout.– Alternative to multiple activities
CS349 -- Direct Manipulation15
http://developer.android.com/guide/components/fragments.html
UI C
ompo
nent
s • android.view.ViewGroup– Abstract container class– Includes layout functionality directly– Subclasses: FrameLayout, GridLayout, LinearLayout,
RelativeLayout, Toolbar, …
16
• android.view.View– Base widget class (drawing and
event handling)– Subclasses:
• android.widget.Button• android.widget.ImageView• android.widget.ImageButton• android.widget.ProgressBar …
Layo
ut • Layout can be handled in one of two ways:1. Programmatically. You write code to instantiate
ViewGroups, Views and bind them together (sim. to how you’ve done this in Java).
2. Use XML to describe your layout. In-code, describe the screen elements (view groups and views) along with properties, and then tell your application to dynamically load it.
• Using XML is actually the preferred way to handle this.– Android Studio includes a GUI builder to make this easier!
CS349 -- Direct Manipulation17
Rec
ap: B
uild
ing
App
licat
ions A typical application will include:
• Activities – MainActivity as your entry point– Possibly other activities (corresponding to multiple screens)
• Views– Screen layouts– ViewGroups containing Views (i.e. components)
• Intents– Only required if you need to communicate or pass data
between screens
18
Ove
rvie
w • What is Android?– Introduction– Architecture– Activities, Views, Intents
• Android Developer Tools– Installing the Android SDK– Setting up an AVD
• Demo– Creating a project, project structure– Building and deploying to an AVD– Using MVC
• Wrap-up– Tips & Tricks
19
And
roid
Stu
dio
20
• Android Studio• Released by Google, based on IntelliJ• Runs on Mac, Windows, Linux
• Install from the Android Developer Portal• Develop -> Tools
• Single installation includes• IDE (GUI)• Software Development Kit
(SDK) includes compiler, debugger, profiler etc.
• Documentation• Sample code
Working in a text editor is NOT
recommended –you need an IDE.
And
roid
SD
K M
anag
er • Android SDK Manager can be used to install components, sample code, documentation, etc.– Android Studio: Tools -> Android -> SDK Manager
21
And
roid
Virt
ual D
evic
e (A
VD) • Android Virtual Device is an emulator, that can emulate a range of
devices/capabilities/OS versions (so that you don’t need a physical device to develop or test your code).– Android Studio: Tools -> Android -> AVD Manager
22
Ass
ignm
ent
Setu
p • Required Development Tools– Android Studio 2.1 (or later) on Mac, Windows or Linux– Android 6.0 Marshmallow (API 23) (installed by default)
• Project Setup– Create with the New Android Application wizard
• Blank Activity (accept defaults)• AVD Setup
– Create a Nexus 7 tablet device, with defaults• Marshmallow: API 23, ABI x86• Landscape orientation
– Advanced Settings• VM Heap: 256 MB
23
Get
ting
Star
ted 1. Install Android Studio
2. Create an empty project– Use the New Project wizard in Android Studio.
3. Setup the Android Virtual Device (AVD)– Configure the emulator for your target device.
4. Specify the Run Configuration -- optional– Specify the starting Activity that will be used, and the target
(typically the AVD that you configured above).5. Build and test6. Commit and push everything to your Git repo
24
Ove
rvie
w • What is Android?– Introduction– Architecture– Activities, Views, Intents
• Android Developer Tools– Installing the Android SDK– Setting up an AVD
• Demo– Creating a project, project structure– Building and deploying to an AVD– Using MVC
• Wrap-up– Tips & Tricks
25
Dem
o • The New-Project wizard generates “Hello World” starter code.• Great demonstration of how the project fits together.• Recall the application lifecycle. Event states map directly to
methods.
Running Paused/ Stopped
26
“Hel
lo W
orld
”
27
• OnCreate() - executed when the app runs• SetContentView(R.layout.activity_main) - Uses layout from
res/layout/activity_main.xml
“Hel
lo W
orld
”
28
Dem
o: M
VC A
pplic
atio
n • To implement MVC, we can instantiate our model, view and controller classes directly in code.– Define views using resource files, but use code to
instantiate them and bind them together.• See android_examples.zip on course website
29
Ove
rvie
w • What is Android?– Introduction– Architecture– Activities, Views, Intents
• Android Developer Tools– Installing the Android SDK– Setting up an AVD
• Demo– Creating a project, project structure– Building and deploying to an AVD– Using MVC
• Wrap-up– Tips & Tricks
30
Tips
& T
ricks • AVD
– The AVD is slow to launch, so keep it running in the background while you’re programming / debugging.
• Use the debugging tools in Android Studio– You can set breakpoints etc. as usual– logcat shows device output and you can write to it using the
android.util.Log class (sim. to printf).
31
Res
ourc
es • Android Developer http://developer.android.com/• Android Developer Channel @YouTube
– http://www.youtube.com/user/androiddevelopers• Android Open Source Project https://source.android.com• Recommended (Optional!) Books
– Phillips & Hardy, Android Programming: The Big Nerd Ranch Guide
– Mednieks et al., Programming Android
32