The first program to understand a new language or a new platform is the well known “Hello World!”. In this article there are the guide lines to write the first program for Android.
The first assumption is to use Eclipse as IDE: this makes the development faster and easier since Eclipse provides to the developers tools and help to shorten the time. Eclipse supports the developer in the creation of an AVD, Android Virtual Device, that is an emulator. In this way everything can run on a normal pc, without the actual use of any mobile phone or proprietary license.
The first step, after having set eclipse, is to create a new project. Eclipse automatically creates some files, the most important of which is helloandorid.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The file created must extends Activity: this means that this class can run and work. The method onCreate is the first that is launched when Activity starts.
The User Interface
To add a suitable look and feel to the application is necessary to change a little bit the class.
package com.android.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
The general class to create graphical objects is View and in this case only a subclass is needed, TextView (since the goal is only a text message).
Run the application
Once again Eclipse helps to perform that. It is required only to go the button Run Application.
Divide code and style
As good practice, the division between code and style is required. To accomplish that, the XML file main.xml, already created by Eclipse, can be edited.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
In this file width and height are the dimensions of the application on the screen and text is the link to the string we want to use.
The other file to change is strings.xml inserting the string to display.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, Android! I am a string resource!</string>
<string name="app_name">Hello, Android</string>
</resources>
Coming back the original file, it is necessary to specify that we are now using an external file.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Debug
Everything should run without problems. Just in case, Eclipse support also the Debug mode and developers can settle the break point and see what happens to the application.
Conclusion
This is the first step to get familiar with the Android platform. The first application Hello World is very easy. There is also the second step that is the division between code and style that gives a deeper understanding of how easy to develop for Android is. The code simplify the comprehension with a concrete example. Developing for Android could be the next way to do business.
References:
http://developer.android.com/resources/tutorials/hello-world.html
http://www.androidpeople.com/android-tutorial/building-simple-android-application/
Bookmark/Search this post with: