Show: Object Pascal C++
Display Preferences

Creating an Android App

From Appmethod Topics
Jump to: navigation, search

Go Up to FireMonkey Applications Guide

Go Up to Android Mobile Application Development


Note: This page describes how to develop FireMonkey applications that target the Android platform. See Android Mobile Application Development for topics that are not specific to the development phase, such as configuring your PC and Android devices to work with Appmethod.

To create an Android app:

File > New > FireMonkey Mobile Application - Object Pascal
File > New > FireMonkey Mobile Application - C++

Android Forms

Using the FireMonkey Mobile Form, you can add additional forms the same way you do with desktop applications.

The Android form has specific hardware-dependent properties. For example, the Android form has a different shape or footprint than a standard desktop FireMonkey form, as follows:

  • For a FireMonkey Android application, the default form is a vertical shape:
    • ClientHeight = 480 pixels
    • ClientWidth = 320 pixels
    These are the same default dimensions used for an iOS app.
  • For a FireMonkey desktop application, the form is a horizontal shape:
    • ClientHeight = 480 pixels
    • ClientWidth = 600 pixels

Component Restrictions for FireMonkey Android Apps

While you are designing your Android app in Appmethod, you can only use components that are supported on Android devices. However, be aware that the Tool Palette might contain some components that are Windows-only or are otherwise not supported on Android.

Using Frameworks in Android Apps

FireMonkey

The FireMonkey framework is ideal for the Android target platform, and it supports other target platforms as well.


RTL

Some run-time library (RTL) units that provide basic functionality are common for every supported target platform (including Android), such as System.pas or System.SysUtils.pas.

A number of RTL units are Android-only (see Android API). These units are prefixed with the Androidapi unit scope.

See also Android C RTL.

Android API

The RTL contains a number of units that provide Object Pascal and C++ interfaces to the Android native API. These units are scoped with Androidapi and are typically located in the source\rtl\android\ directory of your Appmethod installation. The FireMonkey framework relies on some of these units, but you do not use them directly.

Android Core Lib Names Cannot Be Used as App Names

You cannot assign a name to your project if the name conflicts with the name of a core library shared object file, such as <libNAME>.so.

For example, you cannot use any of the following as project names:

  • c (conflicts with libc.so)
  • m (conflicts with libm.so)
  • z (conflicts with libz.so)
  • ssl (conflicts with libssl.so)
  • ui (conflicts with libui.so)
  • gui (conflicts with libgui.so)
  • camera_client (conflicts with camera_client.so)

And so forth.

Alignment Recommendations for Mobile Toolbars and Other FireMonkey Components

To ensure that the toolbar in your mobile app behaves and looks the same on the iOS and Android platforms, you should set specific alignment properties in the Object Inspector, as follows:

  • Set the Align property for all controls that are children of a TToolbar. For example, the following are Align property values:
    (Left, Right, Center, Contents)
  • To maintain space between controls, or to maintain space between the left and right borders of toolbars, set the following properties:
    For example, assume you have a toolbar with a child TLabel as the Title, and two left-aligned speedbuttons with space between each button:
    1. On the TToolbar, set:
      • Align = Top
    2. On the child TLabel, set:
      • Align = Contents
    3. Send the TLabel to the back using the context menu.
    4. On the two child TSpeedButtons, set:
      • Align = Left, Right, Center, Contents, VertCenter
        For descriptions of these properties, see FMX.Types.TAlignLayout.
      • Margins Left = 5
  • To format a segmented control (styled TSpeedButtons with a shared group name) so that it is centered on the toolbar:
    1. Add a TLayout control to the TToolBar.
    2. Set TLayout.Align to Center.
    3. Add buttons to the TLayout and set alignment properties and margins for each button (that is, Align = Left, Margins = Left, Margins = Right, and so forth).
  • To use TLabel, you must set TLabel.AutoSize = False and set the Align property.
    • To center the label on a toolbar that already has buttons, you must set TLabel.Align = Contents and select Send to Back on the context menu.

Similar alignment rules pertain to other controls. For example, assume you have added a TSwitch to a ListBox item; to have the switch aligned to the right and centered vertically, set the following properties for TSwitch:

  • Align= Right
  • Margins Right = 5

Using Custom Fonts in Android Apps

You can install your own custom fonts into your font library on your development system. Then that font should be available in the IDE for you to select at design time. However, when you deploy the app to the target device (iOS or Android), the fonts in the app are rendered using the installed fonts on the device. Fonts that are shown at design time in the IDE are not copied over to the device; this is the expected behavior.

To know whether a font might be available at run time on a specific mobile device, you need to check whether the font you are using at design time is supported on your target devices:

Using Modal Dialog Boxes in Mobile Apps

Modal dialog boxes, which monopolize user input, are not directly supported in Appmethod Android apps. On Android, dialog boxes are essentially popups; for general information about dialog boxes on Android, see http://developer.android.com/guide/topics/ui/dialogs.html

You can use ShowModal on Android if you call ShowModal as an anonymous method, as described in ShowModal Dialogs in FireMonkey Mobile Apps. Calling ShowModal as is typically done for Windows or Mac OS X might give unexpected results.

Another strategy is to call Show, and have the form return and call your event.

Alternatives to ShowMessage

At run time, ShowMessage can freeze and become unresponsive. Here are two recommended alternatives:

Use TPopup

  • When the user changes the focus away from a message box, ShowMessage can become unresponsive.
  • When the user changes the focus away from a TPopup, the popup closes (as expected), but the user can easily invoke it again.

Use a Custom Form

Instead of ShowMessage, you can use a FireMonkey form with an edit control or a combobox instead of ShowMessage.

Setting the Required Uses Permissions for Your Android Apps

Some basic Android permissions are pre-set for all FireMonkey Android apps. You should verify that specific access permissions are enabled for your Android device on the Uses Permissions page in Project Options.

For example:

  • To use the Android device's camera, ensure that the following permissions are enabled:
    • Camera
    • Read/Write external storage
  • To use GPS location on the Android device, ensure that one or more of the Location Access permissions are enabled:
    • Access coarse location
    • Access fine location
    • Access location extra commands
  • To record audio on the Android device, ensure that the following permission is enabled:
    • Record audio

Using the Android Device's Back Button

To make your application handle when users press the Back button on their Android device, add an event handler to your form for OnKeyUp, and use the following code within your event handler:

Object Pascal:

if Key = vkHardwareBack then
begin
  // Do whatever you want to do here
  Key := 0; // Set Key = 0 if you want to prevent the default action
end;

C++:

if (Key == vkHardwareBack) {
    // Do whatever you want here
    Key = 0; // Set Key = 0 if you want to prevent the default action
}
Tip: The Tabbed with Navigation mobile template implements this event handling.

Making Your Application Run in Fullscreen Mode

By default, Android applications display the Android status bar/title bar.

If you want your application to run in fullscreen mode and hide the Android status/title bar, select Project > Options > Version Info and change the value of the theme key to No TitleBar.

For more information, see Version Info.

Settings for Virtual Keyboard in TEdit and TMemo Controls

The virtual keyboard automatically displays when a TEdit or TMemo control is displayed.

To enable the user to hide the virtual keyboard by clicking the Return key, set KillFocusByReturn = True.

Using Icons and Images in Multiple Resolutions

  • On Android, the following resolutions are supported:
    1x, 1.5x, 2x, 3x (Small, normal, large, extra large)
  • On iOS, the following resolutions are supported:
    1x and 2x (Non-Retina and Retina)

Multiple resolutions can be supported by creating a MultiResBitmap using the new MultiResBitmap Editor. For more information, see Using Multi-Resolution Bitmaps.

  • MultiResBitmap supports the following resolutions on Android: 1x, 1.5x, 2.5x
    (iOS supports 1x and 2x)

Loading and Deploying Files

When you deploy your application for the Android target platform, if you plan to provide files along with your application to load those files at run time, you must first decide whether you want to have them on the internal device memory or on a external memory (for example, an SD card). After you decide, use the Deployment Manager to place those files in the assets\internal (internal) or assets (external) folder during deployment.

AddDBforAndroidAssets.png

Use the following code to locate those files at run time:

Object Pascal:

TPath.Combine(TPath.GetDocumentsPath, 'filename')  { Internal }
TPath.Combine(TPath.GetSharedDocumentsPath, 'filename')  { External }

C++:

System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath(), L"filename") // Internal
System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetSharedDocumentsPath(), L"filename") // External

Here filename is the name of your file, such as 'dbdemos.gdb' in the screenshot above. The TPath record is declared in System.IOUtils unit, so you need to add System.IOUtils in the uses clause.

Creating Applications for Google Glass

The Mobile Form Designer provides a Google Glass design-time device, which enables you to create apps for Google Glass:

GoogleGlassDesignTimeDevice.png

If you are creating an app for Google Glass, use the custom Google Glass style that Appmethod provides:

C:\Users\Public\Documents\Embarcadero\Studio\14.0\Styles\Android\GoogleGlass.fsf 

The Google Glass style is optimized for the Google Glass user interface and resolution.

For steps to follow in using the Google Glass style in your FireMonkey apps, see Adding a Custom Style to Your FireMonkey Application.

For general information, see https://developers.google.com/glass/design/ui/index

Implementing Android-Specific Features

The following topics describe how to implement some Android-specific features in your applications:

Find documentation about other, platform-agnostic features in the FireMonkey Applications Guide.

See Also

Personal tools
In other languages