Download - Winter 2014 android tutorial
![Page 1: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/1.jpg)
Human-Computer Interaction (COMP 4020)
Winter 2014
Android Development Tutorial
![Page 2: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/2.jpg)
Installation Issues
Folder Structure
Core Components
Sample Applications
Today: Android Tutorial
1
2
3
4
2
![Page 3: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/3.jpg)
Mobile Phone OS
• Symbian
• iOS
• BlackBerry
• Windows Phone
• Android
• ….
3
![Page 4: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/6.jpg)
http://www.java.com/en/download/installed.jsp
Verify Java Version
6
![Page 7: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/7.jpg)
http://www.java.com/en/download/installed.jsp
Verify Java Version
7
![Page 8: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/8.jpg)
Install JRE
http://www.oracle.com/technetwork/java/javase/downloads/index.html
8
![Page 9: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/9.jpg)
Install JRE
9
![Page 10: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/11.jpg)
Install JDK
http://www.oracle.com/technetwork/java/javase/downloads/index.html
11
![Page 12: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/12.jpg)
Install JDK
12
![Page 13: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/14.jpg)
http://developer.android.com/sdk/index.html
Install ADT Bundle
14
![Page 15: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/15.jpg)
1
2
Eclipse
15
![Page 16: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/16.jpg)
16
![Page 17: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/17.jpg)
17
![Page 18: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/18.jpg)
Android SDK Manager
18
![Page 19: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/19.jpg)
Android SDK Manager
19
![Page 20: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/20.jpg)
Android SDK Manager
20
![Page 21: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/21.jpg)
1
2
Android Virtual Device Manager
21
![Page 22: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/22.jpg)
Android Virtual Device Manager
22
![Page 23: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/23.jpg)
Android Virtual Device Manager
23
![Page 24: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/24.jpg)
Android Virtual Device Manager
24
![Page 25: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/25.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/26.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/27.jpg)
File -> New -> Android Application Project
27
![Page 28: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/28.jpg)
1 2
3 4
28
![Page 29: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/29.jpg)
29
![Page 30: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/30.jpg)
Folder Structure For Android Project
Src Folder (Source folder) – contains the java code
of the application.
30
![Page 31: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/31.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/32.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/33.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/34.jpg)
34
![Page 35: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/35.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/36.jpg)
Layouts activity_main.xml file
Text field
Text field
Button
Text view
Radio Button
Radio Button 36
![Page 37: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/37.jpg)
Output
37
![Page 38: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/38.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/39.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/40.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/41.jpg)
Activity Example
41
![Page 42: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/42.jpg)
Activity Example
Text View
Button
42
![Page 43: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/43.jpg)
Activity Example
Text View
43
![Page 44: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/44.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/45.jpg)
Activity Example
Set layout as describe
in activity_child.xml
45
![Page 46: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/46.jpg)
Activity Example
46
![Page 47: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/47.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/48.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/49.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/50.jpg)
Step 2 -> Java Main File
onPaues()
onResume()
50
![Page 51: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/51.jpg)
Sensor values
Difference between current
and last values
Display in Text View
51
Display Image
![Page 52: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/52.jpg)
Accelerometer Example
125.5 0.0 0.0
52
![Page 53: Winter 2014 android tutorial](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/53.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022020210/568ca63c1a28ab186d9058b0/html5/thumbnails/54.jpg)
Human-Computer Interaction (COMP 4020)
Winter 2014
Android Development Tutorial