Sunday, September 28, 2008

Hello World Android version

For J2ME developers, read the J2ME version first.

Link for the source code at the end of the tutorial.

Step 1. Create Android Project By File > New then select Android Project.

Step 2. Enter Project Name, to be consistent with the J2ME version, I chose HelloWorld.

Android enforces a two identifier package, thus javapadawan.android is used.
The Activity name, entered in above will serve as the entry point of the application. This is similar to J2ME's MIDlet. One can create multiple MIDlets, but only once can be used as a program entry point.

Step 3. Create HelloWorldActivity.java.


Note that R.java is generated by the Eclipse Android plug-in, so don't bother with that. Also, when encountering compile problems with R.java, just do a Clean on the Eclipse Menu Project>Clean then Build the Project.

Step 4. Creating the User Interface.

When you created the HelloWorldActivity, the Eclipse Android plug-in already created the layout XML.


Above is generated XML. Although, one can program Android user-interfaces by hand, without using XML, but that's considered an advanced topic. Similar to coding UIs using MIDP's Canvas.


Below is the User Interface similar to the J2ME version of the HelloWorld. The one's encircled in red are going to be used in the HelloWorldActivity class, the @+id/ should be unique. The usage of such will be shown below. Further, detailed explaination for the xml layouts will come in the future tutorials.



Above is the result of the xml layout above. This uses the RelativeLayout.

Step 5. Coding the HelloWorldActivity.

As much as possible, I want the Android version to be similar to the J2ME version of the HelloWorld so that a J2ME developer may see the differences.


Thus, I also included a initComponents() method. This is where the User Interface Components defined in the XML are retrieved.

Notice the usage of the R.java. Whenever, there are thing modified in the XMLs such as the main.xml and string.xml, it gets reflected here.

The android:id identified above will become R.id..

So to be able to use the User Interface components, it need to be retrieved using the Activity method, findByViewId(...). The parameter needed by findByViewId is integer, but the R.java values should be used.

So just set the values of the findById(...) to a private variable inside the class. For the buttons, (similar to MIDP), listeners must be assigned. Since I used the same class as the listener in J2ME, I will also use the same for Android.

So just, add implements OnClickListener to the class and implement its method.


Similar to J2ME's commandAction(Command c, Displayable d), when a button is clicked, the View (Component) responsible to the event is also passed in the onClick(View v) method.

So above is the implementation I did. Notice the similarities of the MIDP version.

However, the Alert is quite different and more complex in android. Just see the AlertBuilder API.


Step 6. Clean, Build and Run


Clean and Buld the Project.

Run by going to the Menu then Run > Run (Ctrl + F11).
Then Run As Android Applicaiton. On the first Run, this may take a couple of minutes depending on your Hardware.





Step 7. Debugging and Cleaning the Emulator.
Unlike in Netbeans IDE, Mobility where the System.out.print, and stacktraces displays on the IDE's consolde

In Eclipse Android Plug-in, you need to go to the DDMS perspective to debug. Open a new Perspective Window > Open Perspective > Other


On the dialog, select DDMS.


Here's what the DDMS Perspective looks like. Also, you may delete the installed apps if you want. Highlight the .apk to be deleted and click the red (-) minus button shown.



Source Code

1 comment:

Giuseppe Morreale said...

I wish to point out this article.

Android First Example in Java: Hello World
http://programmaremobile.blogspot.com/2009/01/android-first-example-hello-world.html

It expain the steps(from java code to running into emulator) to make the first hello world without eclipse.
I hope it is welcome!

Email

java.padawan@androidph.com