winter 2014 android tutorial

54
Human-Computer Interaction (COMP 4020) Winter 2014 Android Development Tutorial

Upload: gian-carlos-yannelli-ricca

Post on 06-Apr-2016

222 views

Category:

Documents


2 download

DESCRIPTION

Tutorial de desarrollo para Android actualizado a Noviembre del 2014

TRANSCRIPT

Page 1: Winter 2014 android tutorial

Human-Computer Interaction (COMP 4020)

Winter 2014

Android Development Tutorial

Page 2: Winter 2014 android tutorial

Installation Issues

Folder Structure

Core Components

Sample Applications

Today: Android Tutorial

1

2

3

4

2

Page 3: Winter 2014 android tutorial

Mobile Phone OS

• Symbian

• iOS

• BlackBerry

• Windows Phone

• Android

• ….

3

Page 4: Winter 2014 android tutorial

World-Wide Smartphone Sales (In Million Units)

Source: Smartphone Sales, by operating system, Q1 2009-Q3 2013 http://www.statista.com/statistics/266219/global-smartphone-sales-since-1st-quarter-2009-by-operating-system/

4

Page 5: Winter 2014 android tutorial

Java Runtime Environment (JRE)

Java Development Kit (JDK)

Android Developer Tools Bundle (ADT Bundle)

• Eclipse + ADT plugin

• Android SDK Tools

• Android Platform-tools

• The latest Android platform

• The latest Android system image for the emulator

Installation Requirements

1

2

3

5

Page 6: Winter 2014 android tutorial

http://www.java.com/en/download/installed.jsp

Verify Java Version

6

Page 7: Winter 2014 android tutorial

http://www.java.com/en/download/installed.jsp

Verify Java Version

7

Page 8: Winter 2014 android tutorial

Install JRE

http://www.oracle.com/technetwork/java/javase/downloads/index.html

8

Page 9: Winter 2014 android tutorial

Install JRE

9

Page 10: Winter 2014 android tutorial

Java Runtime Environment (JRE) √

Java Development Kit (JDK)

Android Developer Tools Bundle (ADT Bundle)

• Eclipse + ADT plugin

• Android SDK Tools

• Android Platform-tools

• The latest Android platform

• The latest Android system image for the emulator

Installation Requirements

1

2

3

10

Page 11: Winter 2014 android tutorial

Install JDK

http://www.oracle.com/technetwork/java/javase/downloads/index.html

11

Page 12: Winter 2014 android tutorial

Install JDK

12

Page 13: Winter 2014 android tutorial

Java Runtime Environment (JRE) √

Java Development Kit (JDK) √

Android Developer Tools Bundle (ADT Bundle)

• Eclipse + ADT plugin

• Android SDK Tools

• Android Platform-tools

• The latest Android platform

• The latest Android system image for the emulator

Installation Requirements

1

2

3

13

Page 14: Winter 2014 android tutorial

http://developer.android.com/sdk/index.html

Install ADT Bundle

14

Page 15: Winter 2014 android tutorial

1

2

Eclipse

15

Page 16: Winter 2014 android tutorial

16

Page 17: Winter 2014 android tutorial

17

Page 18: Winter 2014 android tutorial

Android SDK Manager

18

Page 19: Winter 2014 android tutorial

Android SDK Manager

19

Page 20: Winter 2014 android tutorial

Android SDK Manager

20

Page 21: Winter 2014 android tutorial

1

2

Android Virtual Device Manager

21

Page 22: Winter 2014 android tutorial

Android Virtual Device Manager

22

Page 23: Winter 2014 android tutorial

Android Virtual Device Manager

23

Page 24: Winter 2014 android tutorial

Android Virtual Device Manager

24

Page 25: Winter 2014 android tutorial

Utilized to copy data between your device and computer.

For Android 3.2 or below:

Settings > Applications > Development > USB debugging

For Others:

Settings > Developer Options > USB debugging.

Enabling USB Debugging on device

25

Page 26: Winter 2014 android tutorial

Java Runtime Environment (JRE) √

Java Development Kit (JDK) √

Android Developer Tools Bundle (ADT Bundle) √

• Eclipse + ADT plugin

• Android SDK Tools

• Android Platform-tools

• The latest Android platform

• The latest Android system image for the emulator

Installation Requirements

1

2

3

26

Page 27: Winter 2014 android tutorial

File -> New -> Android Application Project

27

Page 28: Winter 2014 android tutorial

1 2

3 4

28

Page 29: Winter 2014 android tutorial

29

Page 30: Winter 2014 android tutorial

Folder Structure For Android Project

Src Folder (Source folder) – contains the java code

of the application.

30

Page 31: Winter 2014 android tutorial

Folder Structure For Android Project

gen Folder: contains java files generated by ADT

These files have references to various resources

placed in the application.

It contains a special class ‘R’ which contains all

these references.

31

Page 32: Winter 2014 android tutorial

Folder Structure For Android Project

res Folder (Resource folder): Contains application

resources, such as drawable files, layout files, and

string values

drawable - For bitmap files (PNG, JPEG, or GIF), image

files, and XML files that describe Drawable shapes

layout - XML files that are compiled into screen layout

values - XML files that contain simple values, such as

strings, integers, and colors

32

Page 33: Winter 2014 android tutorial

Common Layouts

Image Source: http://android-meda.blogspot.ca/2012/02/tutorial-using-layouts-in-your-android.html 33

Page 34: Winter 2014 android tutorial

34

Page 35: Winter 2014 android tutorial

Common Layouts

Image Source: http://android-meda.blogspot.ca/2012/02/tutorial-using-layouts-in-your-android.html 35

Layouts: http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts

Page 36: Winter 2014 android tutorial

Layouts activity_main.xml file

Text field

Text field

Button

Text view

Radio Button

Radio Button 36

Page 37: Winter 2014 android tutorial

Output

37

Page 38: Winter 2014 android tutorial

Folder Structure For Android Project

Manifest file:describe the application

• Declare app’s name, version, icon, permission,

etc…

• Declare the application's components: activity,

service ,receiver or provider

38

Page 39: Winter 2014 android tutorial

Activity

Core Components

• An activity is a user interface screen

where visual elements (Views or widgets)

can be placed

• In this example, there are five

widgets(TextView, EditText, AnalogClock

and two Buttons)

• An application might consist of just one

activity or several

39

Page 40: Winter 2014 android tutorial

Intent, Service

Core Components

• Intent is a mechanism to describe specific action, such a

“Send an email”

• A service is a task that runs in the background without the

user’s direct interaction

Activity Activity

Intent

40

Page 41: Winter 2014 android tutorial

Activity Example

41

Page 42: Winter 2014 android tutorial

Activity Example

Text View

Button

42

Page 43: Winter 2014 android tutorial

Activity Example

Text View

43

Page 44: Winter 2014 android tutorial

Activity Example

Inherit from the

activity class

Set layout as

describe in

activity_main.xml

Find “button1”

and set actions

44

Page 45: Winter 2014 android tutorial

Activity Example

Set layout as describe

in activity_child.xml

45

Page 46: Winter 2014 android tutorial

Activity Example

46

Page 47: Winter 2014 android tutorial

Accelerometer Example

An accelerometer is defined as an instrument for measuring the time

rate of change of velocity with respect to magnitude or direction.

125.5 0.0 0.0

7 Text View, 1 Image View 47

Page 48: Winter 2014 android tutorial

Step 1-> Layout <LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:paddingTop="20dip"

android:text="Shaker Demo"

android:textSize="16sp"

android:textStyle="bold" />

<TableLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:paddingTop="10dip"

android:stretchColumns="*" >

<TableRow>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="X-Axis"

android:textSize="14sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="Y-Axis"

android:textSize="14sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="Z-Axis"

android:textSize="14sp" />

</TableRow>

<TableRow>

<TextView

android:id="@+id/x_axis"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center" />

<TextView

android:id="@+id/y_axis"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center" />

<TextView

android:id="@+id/z_axis"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center" />

</TableRow>

</TableLayout>

<ImageView

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:paddingTop="15dip"

android:visibility="invisible" />

</LinearLayout>

48

Page 49: Winter 2014 android tutorial

Step 2 -> Java Main File

SensorManager → access the device’s sensor

Get an instance of this class by calling getSystemService()

49

Page 50: Winter 2014 android tutorial

Step 2 -> Java Main File

onPaues()

onResume()

50

Page 51: Winter 2014 android tutorial

Sensor values

Difference between current

and last values

Display in Text View

51

Display Image

Page 52: Winter 2014 android tutorial

Accelerometer Example

125.5 0.0 0.0

52

Page 53: Winter 2014 android tutorial

Permission

• Manifest update:

– Add permission

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

//…

android:versionName="1.0">

<uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk android:minSdkVersion="8" />

//…

53

Page 54: Winter 2014 android tutorial

Human-Computer Interaction (COMP 4020)

Winter 2014

Android Development Tutorial