Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Android Hello World instance


May 21, 2021 Android


Table of contents


Android Hello World instance

Let's start real Android-based programming. B efore you start writing the first example using the Android SDK, make sure you've completed your Android development environment as described in the Android-Environment Build tutorial. At the same time, let me assume that you have some knowledge of eclipse IDE.

Now let's start writing a simple Android app that prints out Hello World.

Create an Android app

The first step is to create a simple Android app through the Eclipse IDE. F ollow the option File-New-Project and finally select Android New Application from the wizard list. Now name your application HelloWorld with the following window wizard:

Android Hello World instance

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

Android Hello World instance


Android app profiling

Before you run the app, you need to know some of the file directories and files in your Android project -

Android Hello World instance

Serial number Folders, files, and descriptions
1 src: Contains all the .java source files in the project, and by default, it includes an active class for the MainActivity .java source file, which runs when the application starts with the app icon.
2 gen: This contains the compiler-generated. R file, which references resources in all projects. The file cannot be modified.
3 bin: This folder contains a package file built by .apk APT and everything else you need to run an Android app.
4 res/drawable-hdpi: This directory includes all the draftable objects needed for high-density screen design.
5 res/layout: This directory holds files due to the definition of the user interface.
6 res/values: This directory holds a wide variety of XML files containing a range of resources, such as string and color definitions.
7 Android Manifest .xml: This is the application's manifest file, which describes the underlying features of the application and defines its various components.

The following sections give an overview of some important application files.


The main activity file

The main activity code is in the .java of the MainActivity. T his is the actual application file that will be converted to Dalvik executable and run. Here's the default code generated by the app wizard for hello World apps -

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


Manifest file

No matter what components you develop as part of your application, all components need to be declared in the .xml the root of the application project. T his file is the interface between the Android operating system and your application, so if you do not declare your components in this file, you 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>

Over here... B etween labels are application-related components. A ndnroid:icon properties indicate the application icon located below res/drawable-hdpi. This app uses a picture called a picture under the ic_launcher.png folder.

Labels are used to specify an activity, and the android:name property specifies the full name of an Actity class sub-class. T he android:label property specifies a string for the active name. You can use tags to specify multiple activities.

The action of the intent filter is named android.intent.action.MAIN, indicating that the activity is used as an entry point to the application. The intent filter's category is named android.intent.category.LAUNCHER, indicating that the application can be started by the icon of the device launcher.

@tring refers to the .xml (described later). T herefore, @string/app_name refers to the .xml defined app_name the strings, which is actually "Hello World". Similarly, other strings in the app are popular.

Here are the tags you'll use in your manifest file to specify different Android app components:

  • The active element
  • The service element
  • Broadcast receiver elements
  • The content provider element

Strings file

Strings.xml file is in the res/value folder and contains all the text that the application uses. F or example, buttons, label names, default text, and other similar strings. T his file is responsible for the content of their text. 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 glue between an active Java file (such as MainActivity.java) and a resource (such as .xml strings.xml). T his is an automatically generated file and do not modify the .java R file. Here is an example .java R-files:

/* 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. A n application is referenced when it builds its interface. Y ou will modify this file very frequently to change the layout of the application. In the Hello World application, this file has a default layout that reads:

<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 example of Relative Layout, more of which will be covered in separate chapters. T extView is an Android control used to build a user graphical interface. I t contains many different properties, such as android:layout_width, android:layout_height to set its width and height, and so on. @ string refers to strings and files under the res/values .xml folder. Therefore, @string/hello_world refers to the string .xml hello defined in strings: "Hello World!"

Run the application

Let's try to run the Hello World! application we just built. L et's say you created the AVD when you built the environment. R un the app from Eclipse, open an active file in your Android Hello World instance project, and click on the icon on the toolbar. E clipse installs the app on the AVD and launches it. If all goes well, the simulator window below will be displayed -

Android Hello World instance

Congratulations on the first Android app you've developed, and by following the remaining tutorials step by step, you'll be an Android developer for Cow B.