This document summarizes an Android programming workshop. The workshop covers:
1. Differences between Android and Java programming
2. Tools for developers like LogCat and draw9patch
3. Creating a "Hello World" Android app
4. Making a simple game that uses sensors
5. Deploying apps to Android devices
6. Publishing apps on the Android Marketplace
2. Content of the Workshop:
1. How android programming differs from plain
Java programming
2. Tools for developers
3. Making Hello World
4. Making simple game
5. Deploying application to Android device
6. Publishing application in Adnroid Marketplace
2
Ismo Harjunmaa – Workshop Gent 9.2.2012
3. Folder structure of Android project
.java Source files are in src-folder
R.java resource class in gen-folder
Fonts etc. in assets-folder
Project specific resources res-folder
Graphic files in drawable-folders
Vertical UI layout in layout-folder
User-interface texts, tables, styles
and colors in values-folder
3
Ismo Harjunmaa – Workshop Gent 9.2.2012
4. Activity-class
The core of your application
All the event handlers are implemented in this
class (these classes)
4
Ismo Harjunmaa – Workshop Gent 9.2.2012
5. UI layout in XML-file
By default main.xml file is created in folder
res/layout
All the User Interface elements are described in
XML-file
Root element is the layout and there is eight
different ones to pick from
5
Ismo Harjunmaa – Workshop Gent 9.2.2012
6. Designing UI layout
UI is designed in
Eclipse either in
graphical layout
view or writing
elements with
attributes to
main.xml
Elements are placed
by dragging them
from library
6
Ismo Harjunmaa – Workshop Gent 9.2.2012
7. Description file AndroidManifest.xml
The most important description file
AndroidManifest.xml is created in the root
folder of your project
In this file you can specify level of the API,
screen resolution support, testing methods,
permissions etc.
Eclipse shows the attributes of the file either in
plain text or selection fields
7
Ismo Harjunmaa – Workshop Gent 9.2.2012
8. Content of the Workshop:
1. How android programming differs from plain
Java programming
2. Tools for developers
3. Making Hello World
4. Making simple game
5. Deploying application to Android device
6. Publishing application in Adnroid Marketplace
8
Ismo Harjunmaa – Workshop Gent 9.2.2012
9. DDMS (Dalvik Debug Monitor Server)
Perspective in Eclipse that shows all running
emulators or device
Inside this perspective is a view where you can
emulate incoming call, SMS or GPS-signal
In this perspective you can browse the folder
structure of the emulator or device
9
Ismo Harjunmaa – Workshop Gent 9.2.2012
10. Android-SDK command-line tools
All of the tools that Eclipse uses are actually in
the Android-SDK as command-line tools
One command-line tool is NOT in Eclipse: adb
(Android Debug Bridge)
With adb you can list processes, connect to
SQLite databases etc.
10
Ismo Harjunmaa – Workshop Gent 9.2.2012
11. Tools for developers: LogCat
Developer needs to know bit more what is the
state of the application
Under folder android-sdk/platform-tools is the
adb command
Adb logcat shows the logs that device has output
11
Ismo Harjunmaa – Workshop Gent 9.2.2012
12. Tools for developers: LogCat
In the code you use logs like this:
import android.util.Log;
private static final String TAG =
HelloWorldActivity.class.getSimpleName();
Log.d(TAG, ”This is DEBUG level msg”);
Log.e(TAG, ”This is ERROR level msg”);
12
Ismo Harjunmaa – Workshop Gent 9.2.2012
13. Tools for developers: traceview
Writes full log from all events into SD-card
Remember to choose SD-card size when creating
the AVD
Application needs permission to write external
file
Permissions are given in the applications
AndroidManifest.xml -file
After the trace file is commited traceview is the
tool to analyze the trace file
13
Ismo Harjunmaa – Workshop Gent 9.2.2012
14. Tools for developers: draw9patch
Application windows have frames which consists
nine pieces: upper-left corner, top... etc.
So called patch dividing tool is installed with
Android SDK
Before using the tool you have to draw button,
window or frame in drawing tool like photoshop
Go to Androidsdk/tools folder and open
draw9patch
14
Ismo Harjunmaa – Workshop Gent 9.2.2012
15. Tools for developers: zipalign
If you are NOT using Eclipses ADT Plug-in, you
have to run zipalign tool which compacts the
APK-package
15
Ismo Harjunmaa – Workshop Gent 9.2.2012
16. Tools for developers: sqlite3
In the android-SDK package there is SQLite3
database ready-to-use
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DATABASE_NAME = "SensorGame";
private static int DATABASE_VERSION = 3;
public DatabaseHelper(Context context, String name,
CursorFactory factory,int version) {
super(context, DATABASE_NAME, null,
DATABASE_VERSION);
}
...
16
Ismo Harjunmaa – Workshop Gent 9.2.2012
17. Content of the Workshop:
1. How android programming differs from plain
Java programming
2. Tools for developers
3. Making Hello World
4. Making simple game
5. Deploying application to Android device
6. Publishing application in Adnroid Marketplace
17
Ismo Harjunmaa – Workshop Gent 9.2.2012
18. Making Hello World
Open Eclipse
Choose from menu:
File > New... > Android Project
Choose Build Target from option list:
Android 3.X Android Open Source Project
Open main.xml from folder res/layout
While on Graphical Layout view drag
TextView-element on the layout and set text
18
Ismo Harjunmaa – Workshop Gent 9.2.2012
19. Media player
Found in package android.media
Can play music or video
Played resources are positioned in res/raw folder
Common methods are available
playing: play()
pausing: pause()
stopping: stop()
19
Ismo Harjunmaa – Workshop Gent 9.2.2012
20. Sensors in Android Device
From version 1.6: Light, Proximity, Temperature,
Pressure, Gyroscope, Accelerometer, Magnetic
field, Orientation
From version 2.3: Gravity, Linear acceleration,
Rotation vector, Near Field Communication
Not all of them are in your device
SensorManager is the class which controls the
sensors
20
Ismo Harjunmaa – Workshop Gent 9.2.2012
21. Making simple game
Create another Android project:
File > New... > Android Project
Drag three textViews and three seekBars onto
the main.xml
Give names for views and bars like XView, XBar,
YView, YBar... so you can reference them
21
Ismo Harjunmaa – Workshop Gent 9.2.2012
22. Making simple game
In this example we are using Acceleration sensor
Use the SensorManager to create the
acceleration sensor
For using sensor you have to first register the
sensor and on leave unregister
22
Ismo Harjunmaa – Workshop Gent 9.2.2012