Wednesday, December 23, 2009

My First Hello World Program in Android

Here is my first day experience of writing my first Hello, World program using Android.

A. Install the Android SDK

Firstly, I had to install the SDK :
  1. Download the SDK file [ android-sdk_r04-windows.zip ]
  2. Unzip it. Actually, it will unzip to a directory [ android-sdk-windows ].
  3. Put this directory under directory [ \bin ] (or other directory you like, provided that its name has no white space char)
  4. Go to directory [ \bin\android-sdk-windows ]
  5. Noted that there is a sub-directory [ platforms ] which is empty. Now, we are going to set it up.
  6. Run the file [ SDK Setup.exe ]
  7. This message may appear [ Failed to fetch URL https://dl-ssl.google.com/android/repository/repository.xml, reason: HTTPS SSL error. You might want to force download through HTTP in the settings. ]. Then, do the following:
    1. Click the menu [ Tools -> android ]
    2. Check [ Settings -> Force https://... sources to be fetched using http://... ]
    3. Click [ Save & Apply ] button
    4. Close
    5. Run the file [ SDK Setup.exe ] again
  8. Now, you should see a list of platforms. Simply install all of them.

After successful installation, there are many different sub-directories under [ \bin\android-sdk-windows\platforms ].

Also, the configuration information can be found in the directory [ %USERPROFILE%\.android ].


B. Install Eclipse 3.5.1

Because I use Eclipse for many other developments, I want to use a CLEAN Eclipse for this Android development. Therefore, I have to install a blank new Eclipse as follows :
  1. Download the file [ eclipse-jee-ganymede-SR1-win32.zip ]
  2. Unzip the directory [ eclipse ] to directory [ \temp ]
  3. Go to directory [ \temp ]
  4. Rename the directory [ eclipse ] to [ eclipse_A ]
  5. Move the directory [ eclipse_A ] under the directory [ \bin ]

Now, Eclipse 3.5.1 is successfully installed under the directory [ \bin\eclipse_A ]


C. Install the Android Development Tools (ADT) plugin for Eclipse

Follow these steps to install this plugin (basically it is the same steps as mentioned in the site http://developer.android.com/sdk/eclipse-adt.html) :
  1. Go to the directory [ \bin\eclipse_A ]
  2. Run the file eclipse.exe
  3. Click the menu Help -> Install New Software
  4. In the [ Available Software ] dialog, click the [ Add ] button
  5. Then, the [ Add Site ] dialog appear
  6. In the [ Name ] field, input [ Android Plugin ]
  7. In the [ Location ] field, input this URL [ http://dl-ssl.google.com/android/eclipse/ ]
  8. Click the [ OK ] button
  9. Back to the [ Available Software ] dialog, you should now see [ Developer Tools ] added to the list.
  10. Select the checkbox next to [ Developer Tools ], which will automatically select the nested tools Android DDMS and Android Development Tools.
  11. Click the [ Next ] button
  12. The [ Install Details ] dialog will appear to list all the Android DDMS and Android Development Tools features.
  13. Click the [ Next ] button to read and accept the license agreement and install any dependencies.
  14. Then, click [ Finish ] button.
  15. Wait for the installation
  16. Restart Eclipse
  17. Quit Eclipse

Now, the plugin is successfully setup in Eclipse.


D. Create Android Emulator - Android Virtual Device (AVD)

Before developing the program, you have to firstly setup the Android Emulator in which your application is run. The emulator is called Android Virtual Device (AVD).

Here are the steps to setup the most simple AVD (basically the steps are the same as documented in the Android developer site) :
  1. Open DOS prompt
  2. cd \bin\android-sdk-windows\tools
  3. Run the following command :
    android create avd --target 2 --name my_avd
  4. When being asked to create a custom hardware profile, press [ ENTER ] to accept the default [ no ]

Now the AVD named my_avd is ready for your Android application. There is a sub-directory [ my_avd.avd ] created under the directory [ %USERPROFILE%\.android\avd ].


E. Develop the Hello World Program

Before writing the Hello program, we have to setup Eclipse workspace and create an Android project inside Eclipse using these steps:
  1. Make a folder [ D:\Android_WS ] to store all Android related Eclipse workspaces
  2. Start Eclipse
  3. When you are prompt for a workspace, input the following [ D:\Android_WS\ws_hello ]
  4. Click the [ OK ] button. Eclipse will create the workspace directory [ ws_hello ] for you.
  5. Inside Eclipse, click the [ Workbench ] button on the right to go to the workbench panel
  6. Then, select the menu [ File -> New -> Project ]
  7. In the [ New Project ] dialog, there should be a folder [ Android ] which contains an item [ Android Project ]
  8. Select highlight the item [ Android Project ]
  9. Click the [ Next ] button
  10. The [ New Android Project ] dialog should appear, now fill in the Android project details.
    • For the [ Project name ] field, fill in [ HelloAndroid ]
    • For the [ Build Target ], tick the box [ Android 2.0.1 ]
    • For the [ Application name ], fill in [ Hello, Android ]
    • For the [ Package name ], fill in [ com.example.helloandroid ]
    • For [ Create Activity ] field, fill in [ HelloAndroid ]
    • For [ Min SDK Version ] field, fill in [ 2 ]
    • Click the [ Finish ] button

Once the [ Finish ] button is pressed, the project is automatically built. Then, a file [ HelloAndroid.java ] should be created under [ HelloAndroid -> src -> com.exmplae.helloandroid ] in the Package Explorer on the left. The HelloAndroid.java should look like this:
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); } }
Noted that the Eclipse will NOT show the line numbering. I add the numbering information for your easy reference.

Now, we have to write our own Hello World program. as follows :
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); } }
Noted that line-5, line-12 & line-13 are newly added coding.

Now, the program is ready to run.

F. Run the Hello World Program

Run the program inside Eclipse as follows:
  1. Click the menu [ Run -> Run ]
  2. In the [ Run As ] dialog, select [ Android Application ]
  3. Click the [ OK ] button
  4. Wait a while as it needs time to startup the simulator
  5. A red simulation phone should appear
  6. If the screen is locked due to timeout, click the [ menu ] button on the simulation phone.
  7. A text message [ Hello Android ] should appear on the black screen of the phone
  8. Click the [ Back ] button to quit the application
  9. Close the simulation



No comments:

Duplicate Open Current Folder in a New Window

Sometimes after I opened a folder in Win7, I would like to duplicate open the same folder again in another explorer window. Then, I can ope...