Android Introduction
Hello World
@2010 Mihail L. Sichitiu 1
Goal
Create a very simple
application
Run it on a real
device
Run it on the
emulator
Examine its
structure
@2010 Mihail L. Sichitiu 2
Google Tutorial
We will follow the tutorial at:
[Link]
orials/[Link]
Start Eclipse (Start -> All Programs ->
Eclipse)
Create an Android Virtual Device (AVD)
Create a New Android Project
@2010 Mihail L. Sichitiu 3
Package Content
All source code here Java code for our activity
All non-code Generated Java code
resources Helps link resources to
Java code
Images Layout of the activity
Strings used in the
program
Android Manifest
@2010 Mihail L. Sichitiu 4
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
@2010 Mihail L. Sichitiu 5
Activity
An Android activity
is focused on a
single thing a user
can do.
Most applications
have multiple
activities
@2010 Mihail L. Sichitiu 6
Activities start each other
@2010 Mihail L. Sichitiu 7
Revised [Link]
package [Link]; Inherit
from the
import [Link]; Activity
import [Link]; Class
import [Link];
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
TextView tv = new TextView(this);
[Link]("Hello, Android by hand");
setContentView(tv);
}
} Set the view by
hand from the
program
@2010 Mihail L. Sichitiu 8
Run it!
@2010 Mihail L. Sichitiu 9
/res/layout/[Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
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>
Further redirection to
/res/values/[Link]
@2010 Mihail L. Sichitiu 10
/res/values/[Link]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid by resources!</string>
<string name="app_name">Hello, Android</string>
</resources>
@2010 Mihail L. Sichitiu 11
[Link]
package [Link];
import [Link];
import [Link];
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
}
}
Set the layout of the
view as described in
the [Link] layout
@2010 Mihail L. Sichitiu 12
/gen/[Link]
package [Link];
public final class R {
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 textview=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
@2010 Mihail L. Sichitiu 13
Run it!
@2010 Mihail L. Sichitiu 14
Introduce a bug
package [Link];
import [Link];
import [Link];
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
Object o = null;
[Link]();
setContentView([Link]);
}
}
@2010 Mihail L. Sichitiu 15
Run it!
@2010 Mihail L. Sichitiu 16