In the previous post Android Development Environment Setup with Eclipse we have installed the Android SDK
and ADT plugin for Eclipse IDE. Now, before writing the very first famous HelloWorld application, there is one more essential step that we have to perform and it is as follows.
Installing Android Platform in Eclipse
Start the Eclipse IDE and in the menu bar go to Windows -> Android SDK Manager as shown below.
By selecting the Android SDK Manager, you will see the dialog shown below. The Android SDK Manager shows the list of both Installed and Not Installed Packages. Here you can see that Android 4.0 (API 14) is already installed. I prefer to start with an older platform that is 2.1. You should be connected to the internet as the SDK Manager loads the list of Platforms and APIs from https://dl-ssl.google.com/android/repository/.
Check the Android Platform 2.1(API 7) and also check the Extras (if not installed) which include some more packages that you might need in the future. Hit Install [Count] packages button at the right bottom of the window to continue with the installation process.
The next dialog/window again asks about the packages to be installed and also shows the package description and license agreement. Check the Accept All for the licenses and hit Install.
The installation process will take time. So, be very patient! As the installation completes you can see the installed packages as shown in the window below.
Now we are ready to write the Hello World Android application.
Creating Android Project
In Eclipse IDE go to File -> New -> Project. In the New Project window as shown below select the Android Project and hit Next button.
The following New Android Project window will be shown. Here enter the name of the project as AndroidApp1 and hit Next.
In the next window Select Build Target as shown below, check the Android 2.1 as the target and hit Next button.
The following Application Info window will appear. Here enter the name of the package as com.androidapp.helloworld.
Create Activity checkbox is checked by default with the name AndroidAppActivity. Minimum SDK should be 7 (Android 2.1). Activity is an Android Application Component that represents a screen with a particular task. Now hit Finish button.
The project is created successfully.
Exploring Android Project
You can see in the following Package Explorer window, all folders and files that are created.
The above Package Explorer window of the project AndroidApp1 shows the src, gen, assets and res folders. Let us explore these folders.
- src/ Contains all the source code.
o
src/com.androidapp.helloworld/AndroidApp1Activity.java/
Starting point of the application. Following source code is written in this file.
package
com.androidapp.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class AndroidApp1Activity extends Activity {
/** Called
when the activity is first created. */
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
This activity launches the application and this property of it is specified in the AndroidManifest.xml file mentioned below.
·
- gen/ Contains an auto generated resource management file R.java.
·
- assets/ Contains un-compiled resource files of the project.
·
- res/ Contains and manages all resources(graphics, images, animations, strings ) of the application.
o
res/drawable Contains the graphic resources (application icon) according to different screen sizes etc like here there are three different drawable folders created drawable-hdpi, drawable-ldpi and drawable-mdpi according to screen densities.
o
res/layout/main.xml The layout folder contains the layout information of the screens. Here main.xml contains the layout detail of the only screen that is drawn in this application. Following is the content of the main.xml file.
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
If you want the layout to be different when the orientation changes then you can easily specify it in this folder.
o
res/values/strings.xml
This file contains all the string resources. Following is the content of this file.
<?xml
version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World,
AndroidApp1Activity!</string>
<string name="app_name">AndroidApp1</string>
</resources>
·- AndroidManifest.xml This file contains all the configuration detail of the application. Open this file to view it. You can see that it is organized in five different tabs that are Manifest, Application, Permissions, Instrumentation and AndroidManifest.xml.
The first tab shows the Manifest General Attributes that are Package Name, Version Code, Version Name etc.
Following is the second tab, Application. As the name suggests this tab is all about Application Attributes and other major information detai of the application. As you can see Label and Icon attributes are defined. Activities within the application are also defined here. Moreover, application services and functionalities can be defined under this
tab.
The following is the third Permissions tab. Under this tab you can add all the permissions that are required by the application for performing a specific task. For example, if the application is required to use the camera of the phone then it should add the permission android.permision.CAMERA.
Fourth Instrumentation tab is shown below. This tab is used to add the instrumentation classes required during the unit testing of the application.
Fifth tab is of AndroidManifest.xml as shown below. This file contains all the manifest configuration settings in the simple XML format.
Now if you want to make your application debuggable, then you have to configure your application using the Android Manifest. Under the Android Manifest Application tab set the Debuggable attribute to true. You can see the android:debuggable="true" added in the AndroidManifest.xml file as the attribute of the application.
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true">
Running/Debugging Android Application
For running/debugging the android application you have to first create the Android Virtual Device (AVD). In Eclipse IDE go to Window -> AVD Manager. Android Virtual Device Window will open.
In the AVD Manager window click New button. The following Create new AVD window will popup. Enter
the name of the device as the_avd or any other name you like and select the Android2.1 – API Level 7 Platform as the target. Do the settings as shown in the window below and hit Create AVD button at the bottom.
In the AVD Manager Window as shown below you can see the Android Virtual Device with the name the_avd for Android 2.1 platform is created. Now close this window.
Second step is the run/debug configurations step. Right click on the project in Package Explorer window and select Debug As -> Debug Configurations… Youwill see the following Debug Configurations window. Here select the Android Application from the left vertical menu to create the New_Configuration. Under
the Android tab at right Browse the application AndroidApp1.
Now we are ready to run the application in the emulator. Hit the Debug button shown in the configurations window above. As the debug configurations are done you can now debug the application by right clicking on the project in the Package Explorer and go to Debug As -> Android Application or you can simply press the small bug button of the eclipse present in the main toolbar.
The first launch of the application may take time, so you have to wait a little bit. First you will see the following Android splash screen.
The next screen is the following phone lock screen.
And then after sometime as soon as the debugger is attached and the application launches, you will see following screen with the string output Hello World, AndroidApp1Activity!
Hit the back button to close the application. Open the Main Menu of the emulator and you can see the following screen with your application AndroidApp1 icon. You can always use this icon to launch the application.
Now without closing the emulator open the strings.xml file in the eclipse and update the output string to Hello
Android World, AndroidApp1Activity! As shown below.
<string name="hello">Hello Android World, AndroidApp1Activity!</string>
Save the file and hit Debug. Note that the emulator is still running. You will see the updated output as shown below as soon as the application re-launches itself. Here the point is that for the updates you are not required to close the emulator. Just debug/run the application again and you will see the application changes reflected in the running emulator. Following is the new output.
I hope this tutorial will help you in creating and understanding your very first Android application. Feel free to question about
it in the comment section below.