Runoob Tutorial ArchiveEnglish

Android Hello World Example

Android Hello World Example

Let's start real Android framework programming. Before writing the first example using the Android SDK, please ensure you have set up your Android development environment as described in the Android - Environment Setup tutorial. I also assume you have some knowledge of Eclipse IDE.

Now let's start writing a simple Android application that can print "Hello World".

Create Android Application

The first step is to create a simple Android application using Eclipse IDE. Follow the options File -> New -> Project, and then select Android New Application from the wizard list. Now, use the following window wizard to name the application HelloWorld:

Image

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

Images


Android Application Analysis

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

Hello World Example

Sequence Folder, File, and Description
1 src: Contains all .java source files in the project. By default, it includes the activity class corresponding to a MainActivity.java source file, 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 resources in the project. This file cannot be modified.
3 bin: This folder contains the .apk package files built by APT for Android, as well as everything else required to run the Android application.
4 res/drawable-hdpi: This directory includes all drawable objects designed for high-density screens.
5 res/layout: This directory stores files used to define the user interface.
6 res/values: This directory stores various XML files containing a series of resources, such as string and color definitions.
7 AndroidManifest.xml: This is the application's manifest file, which describes the basic properties of the application and defines its various components.

The following chapters 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 compiled 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 in the res/layout directory. onCreate() is one of the many methods called after the activity is loaded.


Manifest File

Regardless of what component you develop to be part of the application, you must declare all components in the manifest.xml file in the root directory of the application project. 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 operating system. 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" >



   <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 tags between are application-related components. The android:icon attribute points to the application icon located in res/drawable-hdpi. This application uses the image named ic_launcher.png in the drawable folder.

The tag is used to specify an activity, and the android:name attribute specifies the fully qualified name of an Activity subclass. The android:label attribute specifies the string used for the activity name. You can usethe tag to specify multiple activities.

The action of the intent filter is named android.intent.action.MAIN, indicating that this activity is used as the entry point for the application. The category of the intent filter is named android.intent.category.LAUNCHER, indicating that the application can be launched via the device 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 common.

Below are the tags you will use in your manifest file to specify different Android application components:


Strings file

The strings.xml file is 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:

<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 active Java file, acting as the glue between activities like MainActivity.java and resources like strings.xml. This is an automatically generated file, so do not modify the content 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 in the res/layout directory. It is referenced when the application builds its interface. You will modify this file very frequently to change the application's layout. In the "Hello World" application, this file has the default layout, which is as follows:

<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" >



   <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 RelativeLayout example; more content will be explained in a separate chapter. TextView is an Android control 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!".

Run the application

Let's try running the Hello World! application we just created. Assume you have already created an AVD when setting up the environment. Run the app from Eclipse, open an activity file in your project, and click the icon on the toolbar.ImagesEclipse installs the app on the AVD and starts it. If everything goes well, the simulator window will display as follows -

Images

Congratulations on developing your first Android application. By following the remaining tutorials step by step, you will become an awesome Android developer.