Android First Example: Hello World

In this tutorial, we show you how to create a simple “hello world” Android project in Eclipse IDE + ADT plugin, and run it with Android Virtual Device (AVD). The Eclipse ADT plugin provided easy Android project creation and management, components drag and drop, auto-complete and many useful features to speed up your Android development cycles.
Summary steps to develop an Android application :
  1. Install Android SDK
  2. Install ADT Eclipse plugin
  3. Create an Android Virtual Device (AVD)
  4. Create Android Project with Eclipse (Wizard)
  5. Code it…
  6. Start it in Android Virtual Device (AVD)
Tools used in this tutorial :
  1. JDK 1.6
  2. Eclipse IDE 3.7 , Indigo
  3. Android SDK

1. Install Android SDK

Visit this Android SDK page, choose which platform and install it.
In Android SDK installed folder, run “Android SDK manager”, choose what Android version you want to develop.
android sdk manager

2. Install ADT Eclipse plugin

To integrate Android SDK with Eclipse IDE, you need to install Eclipse ADT plugin. Refer to this official guide – “Installing the ADT Plugin“.
In Eclipse IDE, select “Help” -> Install New Software…”, and put below URL :
https://dl-ssl.google.com/android/eclipse/
android ADT plugin

3. Create an Android Virtual Device (AVD)

In Eclipse, you can access the “Android Virtual Device (AVD)” in Eclipse toolbar. Click “new” to create a AVD.
android avd manager
Later, Eclipse will deploy the application into this AVD.

4. Create Android Project

In Eclipse, select “File -> New -> Project….”, “Android Project”, and input your application detail. Eclipse will create all the necessary Android project files and configuration.
Eclipse new android project wizard
folder structure

5. Hello World

Locate the generated activity file, and modify a bit to output a string “Hello World”.
File : HelloWorldActivity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        TextView text = new TextView(this);
        text.setText("Hello World, Android - mkyong.com");
        setContentView(text);
    }
}

6. Demo

Run it as “Android Application“, see output.
hello world output
Press “Home” button (on right hand side), and you will noticed that “HelloWorld” application is deployed successfully on the Android virtual device.






android deployed


A program to demonstrate usage of EditText, Textview and Button widgets in android this program is very useful for beginners and give the basic overview on how to use the Api's, using Droiddraw i was able to design the User Interface with two textbox(EditText) and Label(TextView) and button as shown in below Pic

below is the XML for above UI generated from DroidDraw
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/txtbox1"
android:layout_width="145px"
android:layout_height="40px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="75px"
android:layout_y="54px"
>
</EditText>
<EditText
android:id="@+id/txtbox2"
android:layout_width="144px"
android:layout_height="40px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="75px"
android:layout_y="109px"
>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="106px"
android:layout_height="36px"
android:text="Button"
android:layout_x="88px"
android:layout_y="224px"
>
</Button>
<TextView
android:id="@+id/lbl1"
android:layout_width="141px"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="76px"
android:layout_y="166px"
>
</TextView>
</AbsoluteLayout>

and below is the Java Source code which contains the logic to add two numbers and the refrence to id of the controls can be found in above XML

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class fit extends Activity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbox1= (EditText) findViewById(R.id.txtbox1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.lbl1);
txtbox2= (EditText) findViewById(R.id.txtbox2);
button1.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
tv.setText(vis.toString());
}
}
}
 

5 comments:

  1. I moorthy anna .I'm Rakesh who completed my BE(ECE). I want to learn Android so that i need to learn Java also. so,please prefer me some websites to learn java ....
    I've another doubt also. If i start with Android.How good my future will be?
    Please Reply Anna .

    ReplyDelete
  2. Just follow my blog "moorjava.blogspot.in" to learn java, this will be easy to learn.In future everyone will use android smart phones and so many vacancies are there,so there is no worry....if u have doubts on android u can make comment..and all the best my friend...

    ReplyDelete
  3. Moorthy nu soldranga,

    I just want to have some basics concepts and codes in snapshot basis in android and want to prepare for an interview , kindly let me know what are the questions may ask while attending an interview and myself working for SYsunivers as developer , i am looking for an opportunity in iphone or wndroid since i have basic knowledge in java and good knoledge in c and c++ and also going for class on android&iphone.
    let me know a overview please!

    ReplyDelete
  4. already i have question and answers in my blog kindly follow this link http://moorandroid.blogspot.in/ u can download it under "download source".
    Thanks for your valuable comment.

    ReplyDelete