To run Android Apps you need either an Android device such as a smartphone or tablet or an emulator such as this:
Our emulator had these settings:
Android Emulator Details
(See Getting Started with Oxygene for Java for download details of the necessary software. You should also consult the Android_Primer, an authoritative reference). We start this section by demonstrating the use of widgets.
Your code consists of at least a source Oxygene for Java file, a layout file (xml) for providing the widgets used together with their initial properties and a strings file (xml). A default application icon is provided in three sizes.
The current command line compiler is reported not to work with Android applications, so you are obliged to compile code using Visual Studio. In the IDE, you not only obtain a dropdown list of possibilities for completion of the Oxygene code, but also for the properties in the layout files. We wanted to set the background colour of a LinearLayout, and saw "background" among the properties:
Content Completion in a Layout File
In a LinearLayout, android:background="#ffffff" changes the background colour from the default black to white.
In each of these first examples to get you started, we keep it simple and provide only one Activity (screen). These seven demonstrations include:
- the layouts LinearLayout, RelativeLayout, ScrollView.
- setting some values of strings in a separate file instead of using hard-coded "magic" values within the main code. (These strings can be changed readily and the method facilitates the writing of versions in multiple languages).
- these widgets: Button, DatePicker, ImageView, ListView, RadioGroup, TextView, Spinner.
- a pop-up message (Toast).
- handling events such as onClick for a Button or ImageView, and onItemSelected for a ListView.
- setting some common properties such as colour and size in layout files.
- the assignment to variables of objects described by a layout file e.g.
edtName := EditText(findViewById(R.id.edt1));
Our understanding of Android terms is:
- View = widget such as Button;
- Activity = screen;
- Bundle = assorted information for passing between Activities;
- sp = scale-independent pixel, used for font size to be compatible with the various types of screen;
- dp/dip = density-independent pixel, used for sizes other than of font;
To get an idea of the potential complexity of Android applications, look at the public methods of the TextView view (from which the EditView and Button are descended). See also this useful class hierarchy chart.
We devote one page to each demo app and draw your attention to key points. Each page contains a screenshot, the source code of the MainActivity.pas and the xml files to describe the layout (res\layout\main.layout-xml (unless stated otherwise) and the strings (res\values\strings.android-xml).
In Oxygene running in Visual Studio, start a new Android application then copy the code into these three pre-existing files. Create new .layout-xml files in the res\layout folder as necessary.