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 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

Note that is generated by the Eclipse Android plug-in, so don't bother with that. Also, when encountering compile problems with, 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 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

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 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

Hello World J2ME version

Before reading this, you should have downloaded Netbeans IDE 6.1, choose the Mobility version or the All version.

When creating a J2ME application, I usually separate my MIDlet class, view-controller classes and model classes.

I do not separate View and Controller since normally my views only have a couple of controls and I find it more organized that way.

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

Step 1. Create a new Project By File>New Project
Click Mobility from the Categories and MIDP Application from Projects.

Step 2. Write any Project Name and select a project location then click the Next Button.
Step 3. Select CLDC 2.0 and MIDP 2.0

Step 4. Click Finish. Delete all generated .java files as we will not be using the WYSIWYG editor.

Then create two classes, and
You should have the following files on your Project Tab.

Step5. Writing the code.

PauseApp, destroyApp and startApp are the abstract classes of the MIDlet class that needs to be implemented. I added two methods to be used by the UI class, showScreen(Displayble d) and showScreenAndAlert(Alert a, Displayble d).

The code in startApp() will be discussed after the next step.

Step 6. Coding the

As you may have noticed, the MIDlet class is passed to the constructor, I find this useful in creating a apps with a lot of forms. Further, the MIDlet class may also be used to store Application sessions data, also handling unexpected application problems.

The initComponents() method is where the UI Components are initialized and added to the Form.

If you do not want the controller and the view to be on the same class, you may create, or pass in the constructor a class that implements the CommandListener interface.

On the code, instead of setCommandListener(this), just replace this with a CommandListener instance.

Step 7. Building and Running.

Source Code


I am a Java Mobile Edition developer ever since MIDP 1.0, I've already experienced and done the workarounds of almost all of the limitation of the MIDP platform. The worst ever is not being able to get the Mobile Number of the Phones (This is for most Phones, some implementations offer a native utility for retrieving IMEI, Numbers, Contacts but not the standard MIDP API).

When I first reviewed Google Android, I got excited with the fact that everything in the phones is programmable and is not limited to a J2ME like tiny little sandbox. I worry about the Device's security though.

I'll be starting a Journal of how to program Android from a J2ME Developer's Perspective. I am planning to use my knowledge in MIDP, and try to figure out how to port it in android. In the future, I may start on some advanced Android apps and see what the android can really do.