Hello Android

Here I am going to show you how to start with Android programming.

Android is becoming very popular these days, so I thought why don't I also start to make a simple program for that. So in this article I will show the step by step procedure of printing hello world in the emulator of Android.

But before that we should know what the Android is?

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. More details are at:

http://developer.android.com/guide/basics/what-is-android.html

Now what is required to download for Android programming.

First of all we need to download the Android SDK. You can download it from:

http://developer.android.com/sdk/index.html


WinAzure1.gif

But before that visit the page of System Requirements here:

http://developer.android.com/sdk/requirements.html

After downloading the SDK, extract the android-sdk-windows and from that run the file.

SDK Manager.EXE

It will take a little time to refresh the resource, and load the windows which will show you "Package available to install".

Just click on "Cancel" from that:

WinAzure2.gif

Now when you click on Cancel you will get another window which will show you:


WinAzure3.gif

This will show you the installed packages in you Android SDK. Currently I have installed the Android platform 2.2 in my Android SDK.

You can Update or delete the these packages at any time.

When you click on available packages it will show you the packages available to download. We have to install just two packages for now:

WinAzure4.gif

That's it; now click on "Install Selected". Now it will download these packages from the internet and will install them on our Android SDK.

The work for the SDK is done!

Now it's time to install the development tool; that is, Eclipse.

First of all install the Eclipse from here or if you already have it then update it if needed.

If you need to install or update Eclipse, you can download it from http://www.eclipse.org/downloads/.

After the installation of Eclipse we need to install the ADT plug.

Android Development Tools (ADT) is a plug in for the Eclipse IDE that is designed to give you a powerful, integrated environment in which to build Android applications. Follow these steps to install ADT in Eclipse:

Start Eclipse - >Help -> Install new Software

It will give you the window:

WinAzure5new.jpg

Now write the url : http://dl-ssl.google.com/android/eclipse/ in work with and click on Add.

When you click on Add, another window will appear with the title "Add Repository" add the same url in both textboxes and click ok.

It will download the required files..

WinAzure6.gif

Click next and follow the steps by pressing next and at last click Finish. It will prompt you to restart the computer, click Restart Now.

After Windows restarts, we need to inform Eclipse of the location of the Android SDK in our computer. So for that start Eclipse and go to Windows - > Preferences.

Click on the Android tab. From the Right pane click on the browse button to locate the Android SDK Location.

WinAzure7.gif

After selecting click OK.

Now installation of all required softwares is done. So it's time to create a "Hello World Android project".

So let's start.

Start Eclipse. From File - > New -> Project a wizard will appear as below:

WinAzure8.gif

Click Next

WinAzure9.gif

This window ask for the Project Name. Give the appropriate project name.

Next you have to give the package name; a package name is required and that must be contain two identifiers, otherwise it will give you an error.

Now click on Finish.

Your Android project is now ready. It should be visible in the Package Explorer on the left.

Now you Expand your project - > SRC -> yourpackageName - > projectNameActivity.Java

WinAzure10.gif

This contains the data:

package myexample.main;

import android.app.Activity;
import
android.os.Bundle;

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
main
);
}
}

Notice that the class is based on the Activity class. An Activity is a single application entity that is used to perform actions. An application may have many separate activities, but the user interacts with them one at a time. The onCreate() method will be called by the Android system when your Activity starts - it is where you should perform all initialization and UI setup.

Now we have to modify this file to display a hello world message so modify the file this way:

First we are required to add a package; import android.widget.TextView as in:

package myexample.main;

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);
setContentView(R.layout.
main);
TextView tv =
new TextView(this);
//Set a text into view
tv.setText("Hello This is My First Android Application :)");
//set the view into activity view container
setContentView(tv);
}
}

An Android user interface is composed of hierarchies of objects called Views. A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects are a subclass of the View class and the subclass that handles text is TextView.

In this change, you create a TextView with the class constructor, which accepts an Android Context instance as its parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView.

Next, you define the text content with setText().

Finally, you pass the TextView to setContentView() in order to display it as the content for the Activity UI. If your Activity doesn't call this method, then no UI is present and the system will display a blank screen.

Now it's time to run our application:

Go to the Window - > Android SDK and AVD Manager.

In that window click New; give any name for ex :MyAndroid.

At SD Card give the size 256, Skin select HVGA.

Click Create AVD

WinAzure11.gif

Now it's time to run. Right-click on your project and choose:

Run As -> Android Application

The first time it will require a little time to configure, so wait until you get a window.

Now click on all application from the Emulator.

WinAzure12.gif

On all application you will get your hello world application too.

WinAzure13.gif

Click on it and see the result.

WinAzure14.gif

Thank you!