Using Oxygene for Java to Develop for Android devices - Intro

Brian Long Consultancy & Training Services Ltd.
January 2012

Page selection: Previous  Next

Oxygene for Java

Building your first application

Now all the preliminaries are out of the way we can launch Visual Studio and start building an application with Oxygene for Java.

Each time you create or open an Oxygene for Java project there is a "pre-flight check" that's run to ensure all the pre-requisites are in place and can be found. So it checks it can find the Android SDK and the JDK. if either of these cannot be found, maybe because you installed one or both of them to non-default directories, you are alerted to the situation and given the opportunity to install the missing item or manually locate it. For example, if I install the Android SDK to a non-default location, meaning Oxygene for Java cannot readily locate it, I get this when creating an Android project:

Oxygene for Java pre-flight check

Taking the option to manually set the location, I get the following dialog. Notice that the invalid path is greyed out.

Manually setting the Android SDK path

Once the correct path is identified, pressing Retest on the pre-flight check allows us to proceed.

At any point before creating or opening a project you can review the Android and Java directory options by selecting Tools, Options..., Projects and Solutions, Java & Android: in the original RTM version, or Tools, Options..., Projects and Solutions, Java & Android, SDK Paths in later versions:

The Android and Java path settings


Choosing File, New, Project... yields the usual dialog but with some Oxygene for Java project templates available:

Creating a new Android project with Oxygene for Java

Taking them in turn, the Android Application template creates a basic, standard, not-quite-empty Android application. This project has the innate knowledge of the Android tool chain required to take regular Java code, convert it to Dalvik Executable code and package it up with all your Android resources and sign the resultant Android package. This is what we'll always start with when building Android apps with Oxygene for Java.

The Class Library template creates a shared library in a Java .jar file. This can be shared among projects built with Oxygene for Java and also shared with projects built by Java developers.

The Java Applet template generates a project that creates a Java applet that runs in a web page. The project includes a sample web page to test the applet.

The final template creates a basic cross-platform console application. This starts as a command-line application but can be turned into a GUI app by using Java's Swing or AWT libraries, for example.

Since we're looking at Android here we'll use the first template and create an Android Application. This gives a simple sample application with a small amount of code. For the time being we'll ignore the structure of an Android application and just get the first application up and running. So, ignoring the class containing the method and also ignoring for now how the method gets invoked, the code looks like this:

  1. namespace;
  3. interface
  5. uses
  6.   java.util,
  8.   android.content,
  9.   android.os,
  10.   android.util,
  11.   android.view,
  12.   android.widget;
  14. type
  15.   MainActivity = public class(Activity)
  16.   private
  17.     Count: Integer = 0;
  18.   public
  19.     method onCreate(savedInstanceState: Bundle); override;
  20.     method ButtonOnClick(v: View);
  21.   end;
  23. implementation
  25. method MainActivity.onCreate(savedInstanceState: Bundle);
  26. begin
  27.   inherited;
  28.   // Set our view from the "main" layout resource
  29.   ContentView := R.layout.main;
  31.   // Get our button from the layout resource,
  32.   // and attach an event to it
  33.   var myButton: Button := Button(findViewById(;
  34.   myButton.OnClickListener := new interface View.OnClickListener(OnClick := @ButtonOnClick);
  35. end;
  37. method MainActivity.ButtonOnClick(v: View);
  38. begin
  39.   inc(Count);
  40.   (v as Button).Text := WideString.format(String[R.string.my_button_text_2], Count);
  41. end;
  43. end.

The code is trivial and the comments help you bluff through what is unfamiliar. In short it sets up a button event handler. Each time the button is clicked the button's text/caption is updated with a formatted string. From just this source we can't tell what the formatted string is as the value of the string is held elsewhere. We'll see later how the string was defined and how it is being accessed.

You can run the application by pressing Ctrl+F5. This starts the launch process by building the application. Assuming this goes fine Oxygene for Java then chooses an Android device to run the application on. If you have just an emulator running (an Android Virtual Device), that will be chosen. If you just have a physical device connected, that will be chosen. If you have an emulator and a device running, the first one found will be chosen. You can specify the default device (in the case of one or more emulators as well as one or more physical devices being available) in the Java & Android settings dialog we saw earlier. Or you can specify a particular device for this project to run on in the project options sheet:

Selecting the device on which to run the application

Note: In the device list emulator images typically start with the word emulator and have a numeric suffix visible on the emulator's caption bar (see the emulator screenshot below). Physical devices are listed by their serial numbers.

Note: In order to have physical devices appear in the list of available devices to deploy to, you must have already set up your system to support connecting to the device. Installing whatever sync software comes with your device normally does this - this will install any drivers necessary for communication. For example, in the case of my HTC Desire I installed the HTC Sync software that was supplied on my phone's SD card while the phone was hooked up in the external drive mode. After installing HTC Sync the phone could be communicated with by the Android SDK tools (notably adb, the Android Debug Bridge) just by connecting it with the USB cable. If you cannot locate the drivers you could try looking at the Android OEM USB Drivers page.
You will additionally need to enable USB Debugging on the Android device. You do this by pressing the device Menu button and choose Settings, Applications, Development and ensure that USB Debugging is checked.

When the application starts up it looks like this in the emulator (this AVD was created with a HVGA, or 320x480, resolution):

The project template app running in the Android emulator

After a couple of clicks of the button, the button's text is updated as per the code in the event handler, and we get this when running on an actual device (this phone is running with a WVGA800, or 480x800, resolution):

The project template app running on an Android device

So there we have our first application running on an Android device, be it an emulator or a physical telephone or tablet.

Go back to the top of this page

Go back to start of this article

Previous page

Next page