Climbing Mount Android (June 2011)

82
Climbing Mount Android 1 Rajesh Vasa, 2011 Twitter: @rvasa http://www.rvasa.com

description

A pre-101 level talk on the Android platform. Great for people that want to get started on Android (or) just curious about it.

Transcript of Climbing Mount Android (June 2011)

Page 1: Climbing Mount Android (June 2011)

Climbing Mount Android

1

Rajesh Vasa, 2011

Twitter: @rvasahttp://www.rvasa.com

Page 2: Climbing Mount Android (June 2011)

R. Vasa, 2011

Mobile Eco-System

2

Ad Networks

Platform

Content Providers(Music/Video/Books)

Handset OEMs

Telephone Networks

App. Distribution

Cloud Infrastructure

Billing

Page 3: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Eco-System

3

Ad Networks

Platform

Content Providers(Music/Video/Books)

Handset(OEMs)

Telephone Networks

App. Distribution

Cloud Infrastructure

**

Google, Double Click

Google, Amazon

Android

Google, Amazon

Samsung, HTC, Motorola, Sony ...

Billing

Google Checkout

Page 4: Climbing Mount Android (June 2011)

R. Vasa, 2011

Focus of this talk...

4

Ad Networks

Android Platform

Content Providers(Music/Video/Books)

Handset(OEMs)

Telephone Networks

App. Distribution

Cloud Infrastructure

**

Google, Double Click

Google, Amazon

Google, Amazon

Samsung, HTC, Motorola, Sony ...

Billing

Google Checkout

This talk will present a development perspective

Page 5: Climbing Mount Android (June 2011)

R. Vasa, 2011

Talk Overview

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Activities

•Activity Life Cycle (the odd thing)

5

Page 6: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Activities

•Activity Life Cycle (the odd thing)

6

Page 7: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Device User Interaction

7

Menu

HomeBack

Android devices have three key buttons

Page 8: Climbing Mount Android (June 2011)

R. Vasa, 2011

Vendors can add additional buttons

8

Page 9: Climbing Mount Android (June 2011)

R. Vasa, 2011

There are variations in physical form

9

But all phones have Home, Menu, & Back Buttons

Page 10: Climbing Mount Android (June 2011)

R. Vasa, 2011

Home Button...

•Typically, this will take you back to the Home Screen of the phone

•Default behaviour ~ iPhone / iPad button

10

Home

Page 11: Climbing Mount Android (June 2011)

R. Vasa, 2011

Back Button

•This will take you back to the previous screen

•If app. has only one screen, this will exit app.

11

BackPersonal Opinion: iPhone / iPad should borrow this button

Page 12: Climbing Mount Android (June 2011)

R. Vasa, 2011

Menu Button

•Shows a contextual menu (if one is available)

•Developers can write their own menus

•Quite handy (but, causes Usability issues)

• Low Discoverability

12

Menu (as open)

Page 13: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android for tablets is slightly different...

13

Back, Home

Where is the menu button?

Action Bar

(Buttons need not be physical in Android)

Page 14: Climbing Mount Android (June 2011)

R. Vasa, 2011

Tablet Menu Icon Visibility is contextual

14

Menu

Sadly, menu position is not mandated (yuk?)

Page 15: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Activities

•Activity Life Cycle (the odd thing)

15

Page 16: Climbing Mount Android (June 2011)

R. Vasa, 2011

What is Android?

•Android is a platform

•Operating System (with primitive API)

• Frameworks (incl. components and libraries)

16

Applications

Operating System(Customised Linux Kernel)

Android Frameworks

API

Page 17: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android

17

Image Source: http://developer.android.com

Programming languages: Java, C/C++

Page 18: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android is a bi-lingual platform

18

Image Source: http://www.tbray.org

JavaC/C++

Page 19: Climbing Mount Android (June 2011)

R. Vasa, 2011

Most Android Code is in Java (but...)

19

Android Java is not 100% Sun Java

Page 20: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Java is different (in many ways)

20

No RMI

No Bytecode

No AWT / Swing UI

Different 2D/3D libs

Different Class Load Mechanism

Page 21: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Activities

•Activity Life Cycle (the odd thing)

21

Page 22: Climbing Mount Android (June 2011)

R. Vasa, 2011

Development Tools

•Android SDK (Compiler and Emulator)

•Eclipse IDE + Android Plug-in

•Powerful debugger

•Visual UI Builder

•Ant (Build System)

22

Download from http://developer.android.com

Page 23: Climbing Mount Android (June 2011)

R. Vasa, 2011

Eclipse IDE

23

Page 24: Climbing Mount Android (June 2011)

R. Vasa, 2011

Eclipse IDE - Graphical UI Editor

24

Page 25: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Emulator

25

Permits checking Portrait and Landscape views

Page 26: Climbing Mount Android (June 2011)

R. Vasa, 2011

Emulator is nice .. but phone is better

•The emulator runs the Android O/S (you can run any version: 1.6 to 3.0)

•Emulates the phone hardware (like VM Ware)

•Emulator does not have sensors (e.g. GPS, Gyro, Accelerometer, Cell phone etc.)

• It however offers ways to simulate these events

• So, I can send an SMS to the emulator or make a phone call to it

26

Page 27: Climbing Mount Android (June 2011)

R. Vasa, 2011

Emulators Vs Simulator

•iOS offers a simulator

•Android has gone down the emulator path

•Trade-offs (pros and cons),

• Simulators start-up faster, good enough

•Emulators allow checking against rel. 1.6 of the O/S easily + closer to phone hardware

•Emulators and Simulators cannot mimic real CPU speed, disk speed, network speed etc. (yet!)

27

Page 28: Climbing Mount Android (June 2011)

R. Vasa, 2011

If your Java is rusty

28

Free e-Book: http://www.mindview.net/Books/TIJ/

Page 29: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Activities

•Activity Life Cycle (the odd thing)

29

Page 30: Climbing Mount Android (June 2011)

R. Vasa, 2011

The Android Way

30

Convention

Configuration

Development is organised around a few conventions

Page 31: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Project Structure (convention)

•Source code (src)

•Generated code (gen)

•Resources (res)

•Images (drawable)

•Layout of app (layout)

•Constants/Strings (values)

31

Page 32: Climbing Mount Android (June 2011)

R. Vasa, 2011

Needs Resources @Multiple Resolutions

32

High

Low

Medium

Convention

Page 33: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Screens (Activities)

•Activity Life Cycle (interesting design choice)

33

Page 34: Climbing Mount Android (June 2011)

R. Vasa, 2011

A Simple Android App.

34

Page 35: Climbing Mount Android (June 2011)

R. Vasa, 2011

What is involved?

•Place UI controls (Text and Image)

•Layout the controls

•Centre text

•Make text large font

•Display Image to take up all space

35

Page 36: Climbing Mount Android (June 2011)

R. Vasa, 2011

Each Screen is an Activity

•Android app. is made up of Activities

36

Activity

Page 37: Climbing Mount Android (June 2011)

R. Vasa, 2011

Views are Android’s Key UI Building Block

•We need two views on the Activity

37

TextView

ImageView

Page 38: Climbing Mount Android (June 2011)

R. Vasa, 2011

Views are Placed inside a View Group

•Different types of pre-defined layouts

• Linear, Relative, Table, Frame etc..

38

View Group

a.k.a Layout

Page 39: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android App. - Building Blocks

39

Activity Layout Views(View Group)

Page 40: Climbing Mount Android (June 2011)

R. Vasa, 2011

Separation of Concerns in Android

40

Presentation Functionality

Layout Definition (main.xml)

Activity Class

Event Handling, I/O ...

Page 41: Climbing Mount Android (June 2011)

R. Vasa, 2011

UI (Layout) Definition

•Layout definition is generally in an XML file

•Hand coded or Visual Editor

•Can be also be done in Java Code (yuk!)

41

Layout Definition XML File

Page 42: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Offers a number of UI Controls

•Offers all standard controls and lot more...

42

Progress

These are a small

subset of available controls

Page 43: Climbing Mount Android (June 2011)

R. Vasa, 2011

View Groups and Layouts

•Android offers the following View Groups,

• Linear Layout (Horizontal or Vertical)

•Absolute Layout (You set X, Y for each View)

•Table Layout (Rows and Columns)

•Relative Layout (Position relative to other Views)

• Frame Layout (Show only one view at a time)

• Scroll View (Permits scrolling of the screen)

•View Groups can be nested

43

Page 44: Climbing Mount Android (June 2011)

R. Vasa, 2011

Generated Code, Layout & Resources

•How did Australia image get into the App.?

•How did we set the text to “Australia”?

•Conventions

• Layout Defined in /layout/main.xml

•Resources Placed in /res/drawable-*

• String values defined in /values/strings.xml

44

Page 45: Climbing Mount Android (June 2011)

R. Vasa, 2011

Resources

45

Resources in “res”

Convention

Page 46: Climbing Mount Android (June 2011)

R. Vasa, 2011

Resources are given a unique ID

46

A unique reference id to resources is

generated by the Android SDK tools

Convention

Page 47: Climbing Mount Android (June 2011)

R. Vasa, 2011

Resources and Generated IDs

47

Generated Code

static final int australia=0x7f020000;

Page 48: Climbing Mount Android (June 2011)

R. Vasa, 2011

A Reference to Layout also Generated

48

All References are Integers

Page 49: Climbing Mount Android (June 2011)

R. Vasa, 2011

Identifiers are used to access resources

49

Reference to the layout

This is the Activity Class

(Android Runtime will Render this Layout)

Page 50: Climbing Mount Android (June 2011)

R. Vasa, 2011

Layout is Referred to from Java Code

50

Activity

View Group(Layout)

Activity Class (Java)

Layout Definition (main.xml)

Page 51: Climbing Mount Android (June 2011)

R. Vasa, 2011

Who writes the Activity code?

51

This block of code is created by IDE/SDK when we create new Android project

You can also write you own

Page 52: Climbing Mount Android (June 2011)

R. Vasa, 2011

Activity Creation

52

Method called (by Application Launcher) when App is first launched

Page 53: Climbing Mount Android (June 2011)

R. Vasa, 2011

Activity Creation - Layout Rendering

53

Method call will pass the reference to the layout that needs to be rendered on the screen

(“Rendering” is a two-pass process: measure and draw)

Page 54: Climbing Mount Android (June 2011)

R. Vasa, 2011

What is the “root” Activity?

•How does Android know which Activity to create first?

54

Answer: Application Manifest File

Page 55: Climbing Mount Android (June 2011)

R. Vasa, 2011

Application Manifest File

55

Activity Name

Application Icon Reference

Category

Page 56: Climbing Mount Android (June 2011)

R. Vasa, 2011

Application Manifest File

56

Category indicates that it can be launched

Page 57: Climbing Mount Android (June 2011)

R. Vasa, 2011

Application Manifest File

57

Main action indicates that it is the starting point

Page 58: Climbing Mount Android (June 2011)

R. Vasa, 2011

Layouts and String Information

•How did we set the text to “Australia”

58

String constant

Page 59: Climbing Mount Android (June 2011)

R. Vasa, 2011

Using Graphical Resources

•How did the Australia image get into layout?

59

Layout can refer to resources

@drawable is a special tag

Page 60: Climbing Mount Android (June 2011)

R. Vasa, 2011

What is it with the @ tag?

•Constant Resources in Android can be referred using the “@” tag in XML layout

•Example: @drawable, @string ...

60

@drawable/australia

@drawable/bots

Page 61: Climbing Mount Android (June 2011)

R. Vasa, 2011

@ Tag and Multiple Screen Resolutions

•Dealing with multiple screen resolutions

@drawable/icon

Android Runtime decides best resource to use based on hardware capabilities

Page 62: Climbing Mount Android (June 2011)

R. Vasa, 2011

Significance of hdpi/ldpi/mdpi

62

High-Res, 240 dpi screen

Low-Res, 120 dpi screen

Med-Res, 160 dpi screen

Different resolution images

Page 63: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Screens (Activities)

•Life Cycle & interesting design choices

63

Page 64: Climbing Mount Android (June 2011)

R. Vasa, 2011

Building an App with Simple Interaction

•Temperature Conversion (C -> F)

64

Page 65: Climbing Mount Android (June 2011)

R. Vasa, 2011

Views

65

TextView

EditText

Button

TextView

4 Views (UI components) using a Linear Layout

Page 66: Climbing Mount Android (June 2011)

R. Vasa, 2011

Linear Layout (View Group)

66

main.xml(Layout)

Page 67: Climbing Mount Android (June 2011)

R. Vasa, 2011

View Identifiers

•We need a way to identify components that we created in the layout

•E.g. To read input data from the field

67

@+id TAG creates new identifiers

Page 68: Climbing Mount Android (June 2011)

R. Vasa, 2011

UI Interaction Handling Pattern

•Component.setOn......Listener ( handler )

•E.g. button.setOnClickListener

•Handler is an anonymous inner class

•On...Listener handler = new On....Listener() {}

68

Page 69: Climbing Mount Android (June 2011)

R. Vasa, 2011

UI Interaction Handling Pattern

69

The View identifiers are defined in XML

Page 70: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Screens (Activities)

•Life Cycle & interesting design choices

70

Page 71: Climbing Mount Android (June 2011)

R. Vasa, 2011

Wiring up Multiple Activities

71

Select

Back

Contact List Activity Contact Details

Page 72: Climbing Mount Android (June 2011)

R. Vasa, 2011

Activities are Stacked in Android

•All current activities are placed on a Stack

•Newly started activities come into foreground

72

Contact List

Contact Details

Foreground/Active

starts Background/Paused

Back button will pop top most

activity from stack

Page 73: Climbing Mount Android (June 2011)

R. Vasa, 2011

Activities are like mini-processes

•Android activities have their own life cycle

•Communication between Activities is done by Asynchronous Messaging

73

Contact List Contact Detailsmessage

Page 74: Climbing Mount Android (June 2011)

R. Vasa, 2011

Async. messaging called “Intents”

74

Contact List Contact Detailsintent

Activities communicate with each other via Intents

Page 75: Climbing Mount Android (June 2011)

R. Vasa, 2011

Async. messaging called “Intents”

75

Activity-X Photo Viewerview photo

intent

You can send a general purpose message (intent), all applications capable of handling that Intent will respond

Framework prescribes conventions for common intents

Page 76: Climbing Mount Android (June 2011)

R. Vasa, 2011

Roadmap - Where are we?

•Devices

•What is Android?

•Development Tools

•The Android Way

•Anatomy of a Simple Android Application

•An Interactive Android Application

•Dealing with Multiple Screens (Activities)

•Life Cycle (& interesting design choices)

76

Page 77: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Activities are Managed by O/S

77

Activity-A

Activity-C

Activity-B

ApplicationActivities have a

parent application

Activity has Life Cycle Application is NOT managed

directly by the O/S

Life Cycle is Managed by Android Framework

Page 78: Climbing Mount Android (June 2011)

R. Vasa, 2011

Android Activity Life Cycle

78

Activity is re-started when orientation changes

Developers have to save and retrieve state if orientation changes

Page 79: Climbing Mount Android (June 2011)

R. Vasa, 2011

Security -- Android Devices

79

AndroidApplication

User UID (User ID)has

also has

The UID is generated at install time based on the signature and package name

File System Access Permissions

determines

Page 80: Climbing Mount Android (June 2011)

R. Vasa, 201180

A Short Plug!!!

Page 81: Climbing Mount Android (June 2011)

R. Vasa, 2011

Mobile Development @ Swinburne

•HIT8328 - Software Development for Mobile Devices

• Android focused

•HIT8329 - Creating Data Driven Mobile Applications

• iOS focused

• Portfolio Based Assessments

• We do not just cover API -- there is a strong conceptual foundation (prepare you to learn)

81

We Offer Options to Study Just One Subject

Teaching material will be available openly shortly

Page 82: Climbing Mount Android (June 2011)

R. Vasa, 2011

Mobile Development @ Swinburne

•HIT8328 - Software Development for Mobile Devices

•HIT8329 - Creating Data Driven Mobile Applications

82

Teaching material will be available under an open license shortly

Follow @rvasa or @swinfict for update