Android Program Structure

advertisement
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
programs
– 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
view.
• Activity’s utilise views by the setcontextview statement.
– setContentView(R.layout.tigger1);
• Layouts can be complex and difficult to build
– DroidDraw - http://www.droiddraw.org
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Values
• Files containing tuples of data
– name and value Name – Hello
value – Hello World
• Can be simple or complex data
items
– Arrays, menus etc
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game" />
<item android:id="@+id/quit"
android:icon="@drawable/ic_quit"
android:title="@string/quit" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Viewdemo!</string>
<string name="app_name">Viewdemo</string>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>
</resources>
The R Class
/* AUTO-GENERATED FILE. DO NOT MODIFY.
• contains resource IDs for all the
resources in your res/ directory
– Generated by aapt (Android Asset Packaging
Tool)
– 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
0x7f030000
• R.array.countries_array or 0x7f050000
• Means that you can change
details without having to
change the code!
package ac.uk.ere1.Viewdemo;
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
live!
Android Manifest
Activities
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ac.uk.ere1.Viewdemo"
Package definition
android:versionCode="1"
android:versionName="1.0“
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Viewdemo"
android:label="@string/app_name">
Intent Filter defines
<intent-filter>
what it can do
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Listview" />
<activity
android:name="Linearlayout" />
<activity
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>
</manifest>
Notepadv3 Key Points
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo.notepad3">
<application android:icon="@drawable/icon">
<activity android:name=".Notepadv3" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteEdit" />
</application>
</manifest>
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!
Download