Android Hello World Example
Let's start programming with the Android framework for real. Before you begin writing your first example using the Android SDK, please ensure you have completed the setup of your Android development environment as described in the (#) tutorial. Also, I assume you have some knowledge of the Eclipse IDE.
Now, let's start writing a simple Android application that prints "Hello World".
## Creating an Android Application
The first step is to create a simple Android application using the Eclipse IDE. Go to the menu File -> New -> Project, and finally select Android New Application from the wizard list. Now, use the following window wizard to name the application HelloWorld:

Next, follow the provided instructions, keeping all default inputs until the final step. Once the project is created successfully, you will see the following project interface -

* * *
## Android Application Anatomy
Before running the application, you need to know about some of the file directories and files in an Android project -

| No. | Folder, File, and Description |
| --- | --- |
| 1 | src: Contains all the .java source files for the project. By default, it includes a MainActivity.java source file corresponding to an activity class, which will run when the application is launched via the application icon. |
| 2 | gen: This contains the .R file generated by the compiler, which references all the resources in the project. This file should not be modified. |
| 3 | bin: This folder contains the Android .apk package file built by APT, along with everything else needed to run the Android application. |
| 4 | res/drawable-hdpi: This directory contains all the drawable objects designed for high-density screens. |
| 5 | res/layout: This directory holds the files used to define the user interface. |
| 6 | res/values: This directory holds various XML files containing a range of resources, such as string and color definitions. |
| 7 | AndroidManifest.xml: This is the application's manifest file, describing the fundamental characteristics of the application and defining its various components. |
The following sections will provide an overview of some important application files.
* * *
## Main Activity File
The main activity code is in the Java file MainActivity.java. This is the actual application file, which will be converted into a Dalvik executable and run. Below is the default code generated by the application wizard for the Hello World application -
package com.example.helloworld;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.support.v4.app.NavUtils;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
Here, R.layout.activity_main references the activity_main.xml file located in the res/layout directory. onCreate() is one of many methods that are called after an activity is loaded.
* * *
## Manifest File
Whatever components you develop as part of an application, all of them must be declared in the manifest.xml file located in the project root directory. This file is the interface between the Android operating system and your application, so if you do not declare your components in this file, they will not be recognized by the OS. For example, a default manifest file looks like this:
Here, the ... tags contain the application-related components. The android:icon attribute points to the application icon located under res/drawable-hdpi. This application uses an image named ic_launcher.png from the drawable folder.
The tag is used to specify an activity, and the android:name attribute specifies the full name of a subclass of the Activity class. The android:label attribute specifies a string used for the activity's name. You can use multiple tags to specify multiple activities.
The intent filter's action is named android.intent.action.MAIN, indicating that this activity is used as the entry point for the application. The intent filter's category is named android.intent.category.LAUNCHER, indicating that the application can be launched from the device's launcher icon.
@string refers to strings.xml (which will be introduced later). Therefore, @string/app_name refers to app_name defined in strings.xml, which is actually "Hello World". Similarly, other strings in the application are also referenced in this way.
Below are the tags you will use in your manifest file to specify different Android application components:
* Activity element
* Service element
* Broadcast Receiver element
* Content Provider element
* * *
## Strings File
The strings.xml file is located in the res/values folder and contains all the text used by the application. For example, the names of buttons, labels, default text, and other similar strings. This file is responsible for their text content. A default strings file looks like this:
HelloWorld Hello world! Settings MainActivity
* * *
## R File
The gen/com.example.helloworld/R.java file is the glue between the activity Java files, like MainActivity.java, and resources like strings.xml. This is an auto-generated file; do not modify the contents of R.java. Below is an example of an R.java file:
/* 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.example.helloworld;public final class R { public static final class attr { } public static final class dimen { public static final int padding_large=0x7f040002; public static final int padding_medium=0x7f040001; public static final int padding_small=0x7f040000; } public static final class drawable { public static final int ic_action_search=0x7f020000; public static final int ic_launcher=0x7f020001; } public static final class id { public static final int menu_settings=0x7f080000; } public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int activity_main=0x7f070000; } public static final class string { public static final int app_name=0x7f050000; public static final int hello_world=0x7f050001; public static final int menu_settings=0x7f050002; public static final int title_activity_main=0x7f050003; } public static final class style { public static final int AppTheme=0x7f060000; }}
* * *
## Layout File
activity_main.xml is a layout file located in the res/layout directory. It is referenced when the application builds its interface. You will frequently modify this file to change the application's layout. In the "Hello World" application, this file has a default layout with the following content:
This is a simple example of a RelativeLayout, which will be explained in more detail in a separate chapter. TextView is an Android widget used to build the user interface. It contains many different attributes, such as android:layout_width, android:layout_height, etc., to set its width and height. @string refers to the strings.xml file in the res/values folder. Therefore, @string/hello_world refers to the string named hello_world defined in strings.xml: "Hello World!".
## Running the Application
Let's try running the Hello World! application we just built. Assuming you have created an AVD during the environment setup. To run the application from Eclipse, open an activity file in your project and click the  icon on the toolbar. Eclipse will install the application on the AVD and start it. If everything goes well, the following emulator window will be displayed -

Congratulations, you have developed your first Android application. Follow the remaining tutorials step by step, and you will become an awesome Android developer.
YouTip