Before creating the first Android program as discussed in this article, you need to get and setup Eclipse, install Android SDK, and setup the Android plugin for Eclipse. These tasks are described in the following articles:
Development tools for Android - EclipseThis article looks at getting started with Eclipse for Android, especially if you have not done much of Eclipse development or have been a Visual Studio developer.
Getting started with the Android SDK and plugins for EclipseEclipse at its core is a small platform runtime with a whole bunch of plugins providing the functionality for each area. So, the next step in getting ready for Android development is to install the appropriate plugin.
New Project
The File-New menu of Eclipse has several options – including creating New Java Project, choosing a Project type from a list of wizards, creating a new package, class, etc.
Choose File-New-Project … menu item and then choose Android Project from the list of wizards shown in the New Project dialog box. If you have not installed the Android plugin for Eclipse, you will not see this option.
Figure 1. New Android project wizard from Eclipse
Creating the first Android application
The Figure 2 shows the dialog box for creating the new Android application. Some notes below on the information you input into this dialog box.
Figure 2. First Android Application creation in the New Android Project dialog box
Project Name and Workspace
If you are creating a project out of the samples provided by the Android SDK (for example, the Snake sample or LunarLander sample), you would want to choose the Create project from existing source option. Obviously, you can also use this option, if you have source from somewhere else.
Since we are creating a new project, we need to choose Create new project in workspace. Since this becomes a subdirectory under the workspace, it is better not have spaces in the name of the project. You would have set up the default workspace location when you installed Eclipse (here that location is d:/eclipse/workspace).
Package Name
The package names follow a certain standard in Java applications. First of all, all the characters in the name are lower-case (when possible, of course; but you always see the package names in lower-case).
Various parts of the package name are separated by period. In this example, the package is named: com.techzest.android.first. These parts go from broader to smaller groupings. Here, com is the top-level domain name of a US company. Then, techzest is the name of the company. That is followed by android, which is a class of applications developed by this company. And finally, the grouping ‘first’ will hold all the classes that are to be developed for this first application.
Activity Name
Activity is an important class in Android (this is discussed more later). The activity name you create here (FirstAndroidApp) will become a class derived from the Activity class. Here the class naming conventions are followed – all the first letters of the words in the name of a class need to be capitalized. As you can see, first characters of all three words (First, Android, and App) are capitalized.
Application Name
And finally, a friendly title for the application. This title shows up on the title bar of the application in the mobile device. So, this name can contain spaces, etc. to make it friendly to the user.
Android Application from the Package Explorer
Now let’s take a look at how the Android application looks like from the Package Explorer window of Eclipse (shown in Figure 3).
Figure 3. Android application from Package Explorer
New Android Project wizard created the following files for us:
FirstAndroidApp.java
R.java
icon.png
main.xml
strings.xml
AndroidManifest.xml
In addition, you will see android.jar under the Referenced libraries.
Discussion of Source
Let’s look at the generated source in Java and XML files. Discussion here is brief – additional articles will go into more details.
FirstAndroidApp.java
This is the primary source file for this application – it defines an activity that is subclassed from the Activity class in the Android SDK.
Listing 1. FistAndroidApp.java
package com.techzest.android.first;
import android.app.Activity;
import android.os.Bundle;
public class FirstAndroidApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
}
The Java files in this application will have the package statement, suggesting that these classes belong to com.techzest.android.first package.
We have two import statements – one for the Activity class and the other for Bundle class. The onCreate method in the Activity class takes a variable of type Bundle as parameter.
The class definition shows that our primary class is subclass of the Activity class from the android.app package. The onCreate method is called when the activity is first created. The UI is created by the setContentView method where the layout is supplied R.
R.java
R is for Resources. This autogenerated file generates classes for the resources – the icon, string names, and the layout specified in xml file.
Listing 2. R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.techzest.android.first;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
}
}
main.xml
The main.xml file contains the UI specified declaratively (as opposed to via programming). You can see the user interface element TextView defined in this xml file. This is the basis for the layout in classes R and FirstAndroidApp.
Listing 3. main.xml (contains the UI layout)
<?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="Hello World, FirstAndroidApp"
/>
</LinearLayout>
strings.xml
This is another resource file which contains the localizable strings.
Listing 4. strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">First Android App</string>
</resources>
AndroidManifest.xml
And, finally, the manifest that links them all. This specifies which package contains the code, what icon to display in the Applications screen, the activity class, the application name, etc.
Listing 5. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techzest.android.first">
<application android:icon="@drawable/icon">
<activity class=".FirstAndroidApp" android:label="@string/app_name">
<intent-filter>
<action android:value="android.intent.action.MAIN" />
<category android:value="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Running the First Android App
Let’s run the First Android App and see how it looks on a Google Phone from two different locations. Figure 4 shows the application we have created with the default icon (icon.png in the res\drawable directory) in the Applications window on the mobile device.
Then Figure 5 shows the actual application. You can see the title and the text assigned to the TextView user interface element.
Figure 4. The First Android App icon shown in the Applications window
Figure 5. First Android App in the emulator