Thursday, October 6, 2011

Android Hello World


Last week my wife gifted me a new mobile wishing not to miss her calls anymore. My (very very) old mobile celebrated its 7th birthday and I decided to allow it to RIP. For one reason, I held on to it for these many years. It simply did what its supposed to do, making/receiving calls and occasional use of sms. 
But, the new mobile in my hand is completely overloaded and I feel like Bond. It has touch, a full fledged slider qwerty, proximity sensor, accelerometer, GPS and rocket launcher. Yeah yeah I can hear you, nowadays even the most basic model has all these.
I connected it to my home wifi network and went to the roof of my apartment. Just to see how well it picks up the signal. A neighboring kid was curiously looking at me. I thought, he was going to ask me is that a video game console? He is just in his fourth grade. Believe me he asked, ‘uncle, is that an android?’
Android fever is really catching up. If you don’t murmur about android, people won’t believe you as a technologist. If you are a software developer you should at least write a “Hello World!” in android. That’s what we are going to do in this article. We know how android gets related to java and so to a java blog. Oops! I am not mentioning about the Oracle’s case against Google claiming the presence of java code in android.
As always, Hello World is the easiest step you can keep forward in learning a language. I would say, writing Hello World is not about learning the language. It is all about learning the setting up of the development environment, how to compile, interpret / execute.

IDE is everything in Hello, World

If you have completed setting up the development environment, then Android’s Hello World is simple. If you are using Eclipse as your IDE, settingup it for Android is also simple. Its about just installing a plugin. If you are not using Eclipse, don’t worry there are nice documentation available inandroid’s site.

Hello World requires the following steps

  1. JDK 5 or above (JRE alone is not sufficient) installation
  2. Eclipse Ganymede or above installation
  3. Android SDK Installation
  4. Android Development Tools plugin installation
  5. Android Emulator – Android Virtual Device (AVD) Creation
  6. New Android Project Creation
  7. Create Hello World Android source
  8. Run Hello World
A word of caution. If you are going to do this Hello World Android from your office computer, get necessary permissions from your manager. Since, you may be behind some proxy/firewall the software update sites might be blocked. You may get frustrated setting up the IDE.
If you are a java guy, you should be already having some JDK installed in your computer. Just check the version for compatibility. If you have a older version of Eclipse, download the latest version. No harm in having multiple Eclipse versions. Android SDK and plugin installation, just follow the instructions given the URL I have given above. They are more than sufficient.
After you install the ADT plugin, restart the eclipse. Then in Eclipse, Window -> Preferences (in left panel select Android) then give your SDK installed location in the right side panel.

Android Emulator

If you have an android mobile and you want to execute your Hello World in that, there is a time for it. First master yourselves with the basics and you can use the Android Emulator. Always try first with the emulator before going to the real mobile. It will save you lot of time. 
Click the android icon (with a white down arrow) from the Eclipse toolbar. I have show that icon in the below image. Then click the ‘New’ button and create a AVD. Finally it should look like the window in my image.

Hello World Project Creation

Right click in your Eclipse’s Package Explorer and New -> Android Project. Give a project name, then select the Build Target. The build target should match the api version you have given at the time of AVD creation. Then fill the properties panel also.

Now the HelloWorld android project is ready to use. Open the HelloWorld.java from the project source. You should have a HelloWorld class source code similar to the below.
package com.javapapers.android;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
Notice that you don’t have a main method. It is not needed. This HelloWorld is based on the Activity class and onCreatemethod will be called on initiation.
Now lets add our HelloWorld part to it. Edit the code and add the following snipped to it. Import TextView also.
TextView tv = new TextView(this);
tv.setText("Hello World!");
then set the contentView to the TextView you have created
setContentView(tv);
Now your Android Hello World should look like
package com.javapapers.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorld 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 World!");
        setContentView(R.layout.main);
    }
}

Run your Android Hello World

Right click on the project ‘Run As -> Android Application’
The AVD (emulator) will get started and it will take a while to respond. AVD needs that time to initialize itself. Wait for 2 to 3 minutes. Keep an eye on the console, it will give you some background process status information. Then you see “Hello World!”.

No comments: