Here is my first day experience of writing my first Hello, World program using Android.
A. Install the Android SDK
- Download the SDK file [ android-sdk_r04-windows.zip ]
- Unzip it. Actually, it will unzip to a directory [ android-sdk-windows ].
- Put this directory under directory [ \bin ] (or other directory you like, provided that its name has no white space char)
- Go to directory [ \bin\android-sdk-windows ]
- Noted that there is a sub-directory [ platforms ] which is empty. Now, we are going to set it up.
- Run the file [ SDK Setup.exe ]
- 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:
- Click the menu [ Tools -> android ]
- Check [ Settings -> Force https://... sources to be fetched using http://... ]
- Click [ Save & Apply ] button
- Close
- Run the file [ SDK Setup.exe ] again
- 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
- Download the file [ eclipse-jee-ganymede-SR1-win32.zip ]
- Unzip the directory [ eclipse ] to directory [ \temp ]
- Go to directory [ \temp ]
- Rename the directory [ eclipse ] to [ eclipse_A ]
- 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
- Go to the directory [ \bin\eclipse_A ]
- Run the file eclipse.exe
- Click the menu Help -> Install New Software
- In the [ Available Software ] dialog, click the [ Add ] button
- Then, the [ Add Site ] dialog appear
- In the [ Name ] field, input [ Android Plugin ]
- In the [ Location ] field, input this URL [ http://dl-ssl.google.com/android/eclipse/ ]
- Click the [ OK ] button
- Back to the [ Available Software ] dialog, you should now see [ Developer Tools ] added to the list.
- Select the checkbox next to [ Developer Tools ], which will automatically select the nested tools Android DDMS and Android Development Tools.
- Click the [ Next ] button
- The [ Install Details ] dialog will appear to list all the Android DDMS and Android Development Tools features.
- Click the [ Next ] button to read and accept the license agreement and install any dependencies.
- Then, click [ Finish ] button.
- Wait for the installation
- Restart Eclipse
- 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).
- Open DOS prompt
- cd \bin\android-sdk-windows\tools
- Run the following command :
android create avd --target 2 --name my_avd - 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
- Make a folder [ D:\Android_WS ] to store all Android related Eclipse workspaces
- Start Eclipse
- When you are prompt for a workspace, input the following [ D:\Android_WS\ws_hello ]
- Click the [ OK ] button. Eclipse will create the workspace directory [ ws_hello ] for you.
- Inside Eclipse, click the [ Workbench ] button on the right to go to the workbench panel
- Then, select the menu [ File -> New -> Project ]
- In the [ New Project ] dialog, there should be a folder [ Android ] which contains an item [ Android Project ]
- Select highlight the item [ Android Project ]
- Click the [ Next ] button
- 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
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);
}
} |
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);
}
} |
Now, the program is ready to run.
F. Run the Hello World Program
- Click the menu [ Run -> Run ]
- In the [ Run As ] dialog, select [ Android Application ]
- Click the [ OK ] button
- Wait a while as it needs time to startup the simulator
- A red simulation phone should appear
- If the screen is locked due to timeout, click the [ menu ] button on the simulation phone.
- A text message [ Hello Android ] should appear on the black screen of the phone
- Click the [ Back ] button to quit the application
- Close the simulation
No comments:
Post a Comment