Creating your first Android application

In this article we will see how to create your first Android application

Download Source Code

Introduction

In this article we will create a “Hello World” kind of Android application. For that first we will download required tools and then we will create our first Android application

Getting required tools

  • Download and install JDK from here
  • Download Android ADT bundle from here. It contains Android SDK, Eclipse IDE and ADT (Android Developer Tools). Click on “Download the SDK ADT Bundle for Windows” button and select your System type, 32-bit or 64-bit. Then click on “Download the SDK ADT Bundle for Windows” button to download the SDK. Then extract downloaded compressed ADT bundle to your preferred location

Creating Android application using Eclipse IDE

Step 1:

Eclipse IDE comes with the Android ADT bundle. We will use Eclipse to create the Android application

Launch Eclipse by opening “eclipse.exe” from the “eclipse” folder inside the extracted ADT bundle folder

Eclipse ADT Bundle

Step 2:

You will be asked to select a workplace. Select a folder as a workspace where all your android applications will be stored

Workspace Launcher

Step 3:

Select File -> New -> Android Application Project in Eclipse to create a new Android application

New Android Application Project

Step 4:

Enter Application Name, Project Name and Package Name in the New Android Application dialog box. Then select appropriate Android API versions and click on Next

New Android Application

Step 5:

Click Next on Configure Project window

New Android Application-Configure Project

Step 6:

In the Configure Launcher Icon dialog box, customize the launcher icon or leave it to default and click Next

New Android Application-Configure Launcher Icon

Step 7:

Click Next on Create Activity window to create an activity. An activity in Android is used to create a window where you can add your UI controls

New Android Application-Create Activity

Step 8:

Click Next on Blank Activity dialog box

New Android Application-Blank Activity

Step 9:

Finally click Finish to launch the project

Followings are the components of an Android application

Android Project Components

Step 10:

Open res/layout/activity_main.xml file and write following code to display a Button. All the UI that is displayed in an Android application is stored in res/layout folder as an Android XML File. We are using RelativeLayout to place the button. Text of the button is set to “@string/click_me”, which indicates it is set to click_me string constant stored inside res/values/strings.xml file

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

     <Button
          android:id="@+id/btnHelloWorld"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:onClick="btnHelloWorld_Click"
          android:text="@string/click_me"/>

</RelativeLayout>

Step 11:

Open src/com.example.helloandroid/MainActivity.java file and write following to bind the layout to display and specify Button’s onClick event. All the Java codes are stored in src/your package/your application folder

package com.example.helloandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
     }

     public void btnHelloWorld_Click(View vw){
          Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_SHORT).show();
     }
}

In onCreate method, setContentView method is used to the view with activity_main xml file from res/layout folder. This file is referenced using R.layout.activity_main. You use R(.java) to access any resources in Android.

We also defined btnHelloWorld_Click method that is called when clicking on the button. We used makeText static method of the Toast class to show “Hello World!” message. It takes three parameters, context, text to display and duration to show message.

Step 12:

Now run the application using Run -> Run menu or by pressing Ctrl+F11 keys.

But before running the application you need to set up an emulator or connect a real Android device to your system. We will test our application in an emulator. So let us create a new emulator

Creating an Emulator

Step 1:
Go to Window -> Android Virtual Device Manager to open Android Virtual Device Manager dialog box

Android Virtual Device Manager_2013-03-30_18-46-11

Step 2:

Click on New button to open Create new Android Virtual Device (AVD) dialog box

Enter AVD name, Select target Android API level, Device and click OK

Create new Android Virtual Device (AVD)_2013-03-30_18-51-11

Step 3:

Now run the application (Ctrl+F11) to test it in an emulator. It will take a moment to start the emulator with your application. Our application will be launched with a button labeled “Click Me”

5554MyVirtualDevice_2013-03-30_18-42-08

Step 4:

A “Hello World!” message will be displayed when clicking on this button

5554MyVirtualDevice_2013-03-30_18-42-38

One thought on “Creating your first Android application

  1. cevhyruz

    hi??why is it when i hit the button,the app crashes and says “unfortunately helloworld has stoped “

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *