Easy Tutorial
❮ Android Intents Filters Android Broadcast Receivers ❯

Android Hello World Example

Let's start programming with the Android framework for real. Before you begin writing your first example with the Android SDK, make sure you have set up your Android development environment as described in the Android - Environment Setup 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".

Create an Android Application

The first step is to create a simple Android application through the Eclipse IDE. Go to 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, keep all the default inputs, and proceed until the last step. Once the project is successfully created, you will see the following project interface -


Anatomy of an Android Application

Before running the application, you need to know some of the directories and files in an Android project -

No. Folder, File, and Description
1 src: Contains all .java source files, including a MainActivity.java source file by default, which corresponds to the activity class that runs when the application is launched via the app icon.
2 gen: This contains the .R file generated by the compiler, which references all resources in the project. This file should not be modified.
3 bin: This folder contains the .apk package files built by Android APT, as well as everything else needed to run the Android application.
4 res/drawable-hdpi: This directory includes drawable objects designed for high-density screens.
5 res/layout: This directory holds files that define the user interface.
6 res/values: This directory holds various XML files that contain a collection of resources, such as string and color definitions.
7 AndroidManifest.xml: This is the application's manifest file, which describes the basic characteristics of the application and defines its components.

The following sections will provide an overview of some important application files.


Main Activity File

The main activity code is in the MainActivity.java Java file. This is the actual application file that will be converted into a Dalvik executable and run. Below is the default code generated by the application wizard for the Hello World app -

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 refers to the activity_main.xml file in the res/layout directory. onCreate() is one of the methods that is called after the activity is loaded.


Manifest File

Whatever components you develop as part of your application, they must be declared in the manifest.xml file located in the root directory of the application project. This file acts as an interface between the Android OS and your application, so if your components are not declared in this file, they will not be recognized by the OS. For example, a default manifest file looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >

   &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >

       <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" >

          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>

       </activity>

   </application>
</manifest>

Here, the intent filter's action is named android.intent.action.MAIN, indicating that this activity serves as the entry point of 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 (to be introduced later). Therefore, @string/app_name refers to app_name defined in strings.xml, which is "Hello World". Similarly, other strings in the application are also popular.

The following tags are used in your manifest file to specify different Android application components:

-

-

-

-


Strings File

The strings.xml file is located in the res/value folder and contains all the text used by the application. For example, 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:

<resources>
   <string name="app_name">HelloWorld</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">MainActivity</string>
</resources>

R File

The gen/com.example.helloworld/R.java file is the glue between the activity's Java file, such as MainActivity.java, and resources like strings.xml. This is an automatically generated file, and you should not modify the contents of R.java. Here 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

The 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 the default layout, with the following content:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   &lt;TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />

</RelativeLayout>

This is a simple example of a RelativeLayout, with more details to be covered in separate sections. TextView is an Android widget used to build the user graphical 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" defined in strings.xml: "Hello World!".

Running the Application

Let's try running the newly created Hello World! application. Assuming you have already set up an AVD during the environment setup. To run the app from Eclipse, open one of the activity files in your project and click on the toolbar.

Congratulations on developing your first Android application! Follow the remaining tutorials step by step, and you will become an excellent Android developer.

❮ Android Intents Filters Android Broadcast Receivers ❯