Sunteți pe pagina 1din 9

Creating an Android App

Contents
[hide]

• 1 Component Restrictions for FireMonkey Android Apps


• 2 Style Selector
• 3 View Selector
• 4 Using Frameworks in Android Apps
o 4.1 FireMonkey
o 4.2 VCL (not available for Android)
o 4.3 RTL
o 4.4 Android API and Java Libraries
• 5 Android Core Lib Names Cannot Be Used as App Names
• 6 Alignment Recommendations for Mobile Toolbars and Other FireMonkey Components
• 7 Using Custom Fonts in Android Apps
• 8 Setting the Required Uses Permissions for Your Android Apps
• 9 Using the Android Device's Back Button
• 10 Making Your Application Run in Fullscreen Mode
• 11 Settings for Virtual Keyboard in TEdit and TMemo Controls
• 12 Using Icons and Images in Multiple Resolutions
• 13 Loading and Deploying Files
• 14 Creating Applications for Google Glass
• 15 Implementing Android-Specific Features
• 16 See Also

Note: This page describes how to develop multi-device 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 RAD Studio.

To create a multi-device app that supports Android:

File > New > Multi-Device Application - Delphi


File > New > Multi-Device Application - C++Builder

Then select a multi-device application type in the Multi-Device Application wizard.

Component Restrictions for FireMonkey Android Apps


While you are designing your Android app in RAD Studio, you can only use components that are supported
on Android devices. When Android is the active target platform of your project, unsupported components
are grayed out on the Tool Palette.

Style Selector

At the top of the multi-device Form Designer is the style selector:

The style selector's drop-down menu lists all the styles that you can apply to the Master view.
Select the Android style from the style selector to see all the properties on the components according to the
target platform.

View Selector
At the top of the multi-device Form Designer is the view selector:

The view selector's drop-down menu lists all the available views, which show the different platforms, such
as iOS, Android, Windows and OS X.

Use the Master form for a general design of your application. Then you can add new views to your project
in order to customize your application for a specific device type. You can even create new customized
views; see Adding a Customized View to the View Selector. The predefined Android device views are:

• Android 3.5" Phone


• Android 4" Phone
• Android 5" Phone
• Android 7" Tablet
• Android 10" Tablet
• Google Glass

Note: For more information about the resolution of the predefined views see Display Specifications.
Note: Select the Android target platform to see all the available components for Android, otherwise
they appear grayed out.

Add new views to your project in order to customize your application for a specific Android device type. All
the views inherit the components and properties from the Master form. For more information see Form
Designer.

Note: If you want to customize some aspects of your application for all Android devices you can do
it using the Master form, provided you do not want to propagate to the rest of platforms. Otherwise
use the Code Editor, for example:
// You will see the changes applied at run-time.
{$IFDEF ANDROID}
SpeedButton1.StyleLookup := priortoolbutton;
{$ENDIF}

For more information:

• Mobile Tutorial: Using Layout to Adjust Different Form Sizes or Orientations (iOS and Android)

(shows how to use the Master form for different form factors (such as phone or tablet), without
using different views for each form factor)

Using Frameworks in Android Apps


FireMonkey

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

VCL (not available for Android)


The Visual Component Library (VCL) is available only on Windows (32-bit or 64-bit).

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 and Java Libraries). These units are prefixed
with the Androidapi unit scope.

See also Android C RTL.

Android API and Java Libraries

The RTL contains a number of units that provide Delphi 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 RAD Studio installation. The FireMonkey framework relies on some of these units. You may also use
them directly if you want.

You can also use parts of the Android API that are not mapped by the RTL, or use your own Java libraries in
your project:

• Using Java Libraries in Your RAD Studio Android Apps


o Using a Custom Set of Java Libraries In Your RAD Studio Android Apps
o Using the Built-in RAD Studio Java Libraries for Android

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 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:
o Margins: (left, right)
o Padding: (left, right)

For example, assume you have a toolbar with a child TLabel as the Title, and two left-aligned
speedbuttons with space between each button:

3. On the TToolbar, set:


▪ Align = Top
4. On the child TLabel, set:
▪ Align = Contents
5. Send the TLabel to the back using the context menu.
6. 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).
▪ For an example, see Mobile Tutorial: Using a Button Component with Different
Styles (iOS and Android).
• To use TLabel, you must set TLabel.AutoSize = False and set the Align property.
o 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 use custom fonts in your Android apps. One use for custom fonts is as icons in your mobile
applications. True-type fonts have advantages over raster image files in that fonts are light-weight and
vector-based, so they scale well to high resolutions. You can install your own custom fonts into your system
font library on your development system. Then the custom fonts should be available in the IDE for you to
select at design time.

However, if you deploy the app at this point to a target device, the fonts in the app are rendered using only
the installed fonts available on the device. You need to add the font to your project using the Deployment
Manager to ensure that the fonts you see at design time in the IDE are copied over to the device.

Here are the basic steps for incorporating a custom font in your Android app::

1. Install the custom font on your Windows system.


o See the Windows Help and Support on your system for instructions.
o For example, right-click the font file, and click Install.
2. Add the font file to the root directory of your project.
3. In the Object Inspector, assign the font to the Text Settings/Font property for the controls that use
the font.
4. Add the font file to your project in the Deployment Manager.

Full instructions and downloadable files for creating a mobile project that uses a custom installed font (Font
Awesome) are available here:

• http://community.embarcadero.com/index.php/blogs/entry/true-type-font-iconography-for-android-
and-ios-apps

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:

• Fonts on Android: http://developer.android.com/design/style/typography.html

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:
o Camera
o Read/Write external storage
• To use GPS location on the Android device, ensure that one or more of the Location Access
permissions are enabled:
o Access coarse location
o Access fine location
o Access location extra commands
• To record audio on the Android device, ensure that the following permission is enabled:
o 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:

Delphi:

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

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

Delphi:
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.

Implementing Android-Specific Features


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

• Using the Google Play In-app Billing Service


• Using the AdMob Service
• Using 9-patch Images

Find documentation about other, platform-agnostic features in the FireMonkey Applications Guide.
Adding Views to Your Multi-Device Project
Go Up to Using FireMonkey Views

A view is a version of the Master view that is customized for a given device or a type of device, for example
Windows Desktop.

• To toggle between views in the Form Designer, select a view on the drop-down Views menu.

• To add one or more views to your multi-device project, click a platform in the Available list. The
view you added is immediately used for your project in the Form Designer, and the view's name
appears under a new group, Created, separated from the Available views:

When you add a view, a new form is included in the project and is saved in the project folder with the
following name structure:

<Unit_name><platform>

The list of views appears in the code as follows:

Delphi:

The names of new view forms are listed in the Implementation section:

implementation

{$R *.fmx}
{$R *.Windows.fmx MSWINDOWS}
{$R *.iPhone4in.fmx IOS}
{$R *.LgXhdpiPh.fmx ANDROID}

C++:
The names of new view forms are listed in the <Unit_name>.cpp:

#pragma resource "*.fmx"


#pragma resource ("*.Windows.fmx", MSWINDOWS)
#pragma resource ("*.iPhone4in.fmx", IOS)
#pragma resource ("*.NmXhdpiPh.fmx", ANDROID)

To learn how to add customized views that are not listed in the Available section, see Adding a Customized
View to the View Selector.

S-ar putea să vă placă și