Android Program Structure

CE53105-3 Applied Communications Technology
Android lecture 2 - Structures
• Android File structure
• Resources
• Drawables
• Layout
• Values
• R Class
• Manifest
• Running programs
• DDMS ( Dalvik Debug Monitor Server )
• Explaining Interesting things in Notes3
Android File Structure
Source folder – all java files you create
Your Project – Should be a unique name
Individual Java source files
Generated folder – R class here – leave alone
Source objects for Android platform target
Resource folder – XML files defining resources
Drawable Folders – Picture, image, icons etc
Layout XML files – Used by Activities to define screens
Values folder – In this case a strings XML doc
The Manifest – Declares all bits of project
Drawable Folders
• Contains images that will be used in your
– Generally three (or one)
• Drawable-hdpi – high resolution screen
• Drawable –mdpi – medium resolution
• Drawable- ldpi – low resolution
– Means you may need several versions of your
image to suit the screen resolution!
• Becoming a problem as sizes proliferate
• Referenced using the R class
Layout folder
• Contains XML documents that define the layout of a particular
• Activity’s utilise views by the setcontextview statement.
– setContentView(R.layout.tigger1);
• Layouts can be complex and difficult to build
– DroidDraw -
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
• Files containing tuples of data
– name and value Name – Hello
value – Hello World
• Can be simple or complex data
– Arrays, menus etc
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/new_game"
android:title="@string/new_game" />
<item android:id="@+id/quit"
android:title="@string/quit" />
<?xml version="1.0" encoding="utf-8"?>
<string name="hello">Hello World, Viewdemo!</string>
<string name="app_name">Viewdemo</string>
<string-array name="countries_array">
The R Class
• contains resource IDs for all the
resources in your res/ directory
– Generated by aapt (Android Asset Packaging
– For each type of resource, there is
an R subclass
– for each resource of that type,
there is a static integer
– To use the resource simply add it
to your code
• R.string.hello or 0x7f040000
• R.layout.linearlayoutdemo or
• R.array.countries_array or 0x7f050000
• Means that you can change
details without having to
change the code!
public final class R {
public static final class array {
public static final int countries_array=0x7f050000;
public static final class attr {
public static final class drawable {
public static final int icon=0x7f020000;
public static final class id {
public static final int entry=0x7f060001;
public static final int label=0x7f060000;
public static final int ok=0x7f060002;
public static final class layout {
public static final int linearlayoutdemo=0x7f030000;
public static final int list_item=0x7f030001;
public static final int main=0x7f030002;
public static final int relativelayoutdemo=0x7f030003;
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
The DDMS Perspective
Emulators Running
Directory Structure
of emulator (can
push and pull files)
Emulator controls –
can simulate
various phone
actions eg
receiving a call or
gaining GPS input
Log provides useful
information on
what is happening
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
Package definition
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Viewdemo"
Intent Filter defines
what it can do
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
android:name="Listview" />
android:name="Linearlayout" />
android:name="Relativelayout" />
<!-- <activity
android:name="Tablelayout" />
Activities Commented out
<activity android:name="Gridview" />
<activity android:name="Tablayout" />
--> </application>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Permissions Required
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-sdk android:minSdkVersion="3"></uses-sdk>
Notepadv3 Key Points
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
<application android:icon="@drawable/icon">
<activity android:name=".Notepadv3" android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<activity android:name=".NoteEdit" />
Notepadv3 (cont)
1- @overide – places an existing definition of the method.
2- super is the root definition and is called to ensure all methods and
properties are properly instantiated.
3- setContentview(R.layout.notes_list) –
used to define the view being used by the activity.
In this case note_list!
4- a- call to a method defined in this class.
b- the method definition.
5- Intent starting a new activity (ACTIVITY_CREATE)
Note it expects the activity to return a result!
6- Intent starting a new activity (ACTIVITY_EDIT)
Also expecting a result to be returned.
Note i.putExtra method – allows data to be transmitted to activity.
7- The method dealing with the result returned.
8- Have to create ‘listeners’ for each view item – problematical!

similar documents