Configure & Build android hello world sample application using eclipse.

In this tutorial, we do two tasks
1. Configure an android project in Eclipse
2. Develop and run an android hello world example

Android applications can be developed with the following IDE

1. Eclipse ADT
2. Android Studio
3. Existing IDE with ADT

Let us see how to develop android applications with the existing IDE (here Eclipse)

Required tools

1. ADT plugin
2. Eclipse 3.7.2 (Indigo) or greater
3. JDK 1.6 or above

1) Dowload ADT Plugin (Android Developer Tools )

Download the ADT Plugin from https://dl-ssl.google.com/android/eclipse/ and add to eclipse
To add the ADT plugin to Eclipse:
Start Eclipse, then select Help -> Install New Software.
Click Add
In the dialog box, in the Name, enter “ADT Plugin“, for  location, click on Archive and               select ADT Plugin zip file (for eg. ADT-23.0.2.zip) from the system downloaded above.                Then ok.

add adt pluginadd adt plugin1

2. Restart Ecplise

After restart, you should get the Android SDK manager in the tool bar. If not available in the tool bar, open Windows->Customs perspective-> Command Groups Availability-> Select Android SDK and AVD Manager from the available command groups.

3. Configure the ADT Plugin

       Add Android SDK. If you already don’t have Android SDK, please download it (installer_r23.0.2-windows) from https://developer.android.com/sdk/index.html#download and install. After install, android-sdk folder inside current folder is created that contains the SDK files.

           For Adding SDK to eclipse, Windows ->Preferences -> Select Android without expand, then give the SDK location (android-sdk) created in the local drive.

android add sdk

4. Download / Add some additional (extra) packages (For example, ARM EABI v7a System Image – Android Virtual Device (AVD) to run the android application, Android Support Library, etc.. ) using Android SDK and AVD manager

sdk manager

5. Now let us create a Hello World Project

In eclipse, File->New->Others->Android->Android Application Project OR press Ctrl N
Give the Application Name ,Project name (In our example, AndroidApps) and Package Name (here : com.test.androidapps)
Select Minimum required SDK, Target SDK , compile with and theme. I have selected Android 4.1 Jelly Bean

Note : For using 4.1 SDK, you need to download and add SDK packages as given in the step 4. i.e. using Android SDK Manager -> thne check Android 4.1 SDK , then download.

android step3

Then click Next ..
Select blank activity, then next.

Give Activity and Layout Name (here : Main and main) then Finish.
HereActivitity (Main) will be the default java file (Main.java) to be created and layout will be the main.xml that contains the details of interface to be created. The complete directory structure for the hello world application is given below.

android build structure

Some of the files you need to look into.

1. Main.java (inside src/com/test/androidapps) – activity file.
2. main.xml (inside res/layout)
3. string.xml (inside res/values) – contains string values.
4. AndroidManifest.xml (inside /res)
5. R.java (inside gen/com/test/androidapps) -automatically generated by ADT based on the resource file layout/main.xml and values/string.xml
5. .apk file (inside bin) – final app generated after building the project

The following code creates the hello world application

Replace the following code with Main.java file

Main.java

package com.test.androidapps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {
TextView Message;
Button TouchMe;
        @Override
    public void onCreate(Bundle savedInstanceState) {
        	super.onCreate(savedInstanceState);
        	setContentView(R.layout.main);
        Message = (TextView) findViewById(R.id.TextView1);
        TouchMe = (Button) findViewById(R.id.button1);

     TouchMe.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v)
        		{
        		Message.setText("Congrs..This is your Android Hello World Application!");
        		}
        	});
         }
 } 

Replace the following xml code with main.xml under res/layout

main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.androidapps.Main" >

    <TextView android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/welcome" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView1"
        android:layout_below="@+id/TextView1"
        android:layout_marginTop="38dp"
        android:text="Touch Me" />

</RelativeLayout> 

Replace the following xml code with string.xml file under res/values

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidApps</string>
    <string name="welcome">Welcome to Android Apps!</string>
    <string name="action_settings">Settings</string>

</resources>

AndroidManifest.xml file will be

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.androidapps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Note : @string means the values of the field will be replaced from string.xml.

6. Create an Android Virtual Device (AVD) an Emulator using AVD manager for running the above android application.
i) Select Android Virtual Device in the tool bar. If not available in the tool bar, add it as we did it in the Step 2.
ii) Click on Create and Start AVD as given in the picture.

avd create1avd create2
Note : Select Device , Target and Memory as per your requirement.

7. Run the sample android application by choosing the AVD created above.

run android1

You will get the below output.

output1

Click or Touch the “Touch Me” button and the output will be as given below.

output2

 

Reference :  Creating an Android Project

 

You may also like

Leave a Reply

Be the First to Comment!