Creating Your First Android Application

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

Introduction

 
In this article, we will create a Hello World kind of Android application. For that first, we will download what is required and then we will create our first Android application.
 
Getting the required tools
  • Download and install the JDK from here
  • Download the Android ADT bundle from here. It contains the Android SDK, Eclipse IDE and ADT (Android Developer Tools). Click on the "Download the SDK ADT Bundle for Windows" button and select your System type, either 32-bit or 64-bit. Then click on the "Download the SDK ADT Bundle for Windows" button to download the SDK. Extract the downloaded compressed files to your preferred location
Creating an Android application using the Eclipse IDE
 
Step 1
 
The 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.
 
Android1.jpg
 
Step 2
 
You will be asked to select a workplace. Select a folder as a workspace where all your Android applications will be stored.
 
Android2.jpg
 
Step 3
 
Select "File" -> "New" -> "Android Application Project" in Eclipse to create a new Android application.
 
Android3.jpg
 
Step 4
 
Enter the Application Name, Project Name and Package Name in the New Android Application dialog box. Then select the appropriate Android API versions and click on Next.
 
Android4.jpg
 
Step 5
 
Click "Next" on the Configure Project window.
 
Android5.jpg
 
Step 6
 
In the Configure Launcher Icon dialog box, customize the launcher icon or leave it to default and click "Next".
 
Android6.jpg
 
Step 7
 
Click Next on Create Activity window
 
Android7.jpg
 
Step 8
 
Click "Next" on the Blank Activity dialog box.
 
Android8.jpg
 
Step 9
 
Finally, click "Finish" to launch the project.
 
The following is the components of an Android application:
 
Android9.jpg
 
Step 10
 
Open the "res/layout/activity_main.xml" file and write the following code to display a Button. All the UI that is displayed in an Android application is stored in the "res/layout" folder as an Android XML File.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.    
  6.     <Button  
  7.         android:id="@+id/btnHelloWorld"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:onClick="btnHelloWorld_Click"  
  11.         android:text="@string/click_me" />  
  12.    
  13. </RelativeLayout> 
Step 11
 
Open the "src/com.example.helloandroid/MainActivity.java" file and write the following to bind the layout to the display and specify the Button's onClick event. All the Java code is stored in the "src/your package/your" application folder.
  1. package com.example.helloandroid;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.Toast;  
  7. public class MainActivity extends Activity {  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13.     public void btnHelloWorld_Click(View vw){  
  14.        Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_SHORT).show();  
  15.     }   
In the onCreate method, the setContentView method is used to the view with the "activit_main" XML file from the "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 a "btnHelloWorld_Click" method that is called when clicking on the button. We used "makeText" a static method of the Toast class to show the "Hello World!" message. It takes three parameters, context, text to display and duration to show the message.
 
Step 11
 
Now run the application using "Run" -> "Run menu" or by pressing the "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 an Android Virtual Device Manager dialog box.
 
Android10.jpg
 
Step 2 
 
Click on "New" to open the "Create new Android Virtual Device (AVD)" dialog box.
 
Android11.jpg
 
Enter an AVD name, select the target Android API level, Device and click "OK." 
 
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.
 
Android12.jpg
 
Step 4 
 
A "Hello World!" message will be displayed when clicking on this button.
 
Android13.jpg