Using FireMonkey Views

From RAD Studio
Jump to: navigation, search

Go Up to FireMonkey Application Design

Views are a mechanism that allows you to define a different layout and components for the forms of your application, depending on the device where your application is running.

New forms initially contain a single view: the Master view, with the default name unit1.fmx.

  • You can add additional views to your application if you want to provide different views on different devices.
    We recommend creating views for the basic representative device types, such as phone vs. tablet. We do not recommend creating every view possible in the View Selector.


MultipleViews.png

View Inheritance

Any additional view that you add to your project inherits the components of the Master view and their properties.

When you add or remove components from the Master view, the Form Designer propagates this change to any existing view. You can only add or remove components from the Master view, you cannot add or remove components from any other view. If you only want one component to show up on certain views, hide that component on any other view.

When you change properties of your form or any of its components on the Master view, the Form Designer propagates those changes as well to any existing view unless that view has already specified a different value for that property. You can customize property values on any particular view to overwrite the value from the Master view only on the candidate view.

Restoring the Property Values of a Component from Master

To restore the property values from the Master view on a component of another view:

  1. Select the candidate view from the View Selector.
  2. On the Form Designer, right-click the target component.
  3. Select Revert to Inherited from the context menu.

All the properties of the selected component go back to the value that they have in the Master view.

Hiding a Component

You cannot remove a component from a view, but you can hide that component.

To hide a component on a particular view:

  1. Select the component.
  2. On the Object Inspector, set the Visible property to False.

To hide a component for a specific target platform, use conditional code in the Code Editor:

Delphi:

{$IFDEF ANDROID}
    SpeedButton1.Visible := False;
{$ENDIF}

C++:

#if defined(__ANDROID__) && defined(__arm__)
    SpeedButton1->Visible = false;
#endif

You can run this code from an event handler associated with the OnCreate event of your form, for example. Since you are changing the visibility of the component at run time, the component is still visible on the Form Designer.

View Loading Algorithm

When you run your application on a device, FireMonkey evaluates the views defined in your application, and lists them by priority. The list includes only views where the platform is the same as the platform where your application is running, and the priority of each view in the list is determined according to the following criteria, from highest (1) to lowest priority:

Priority Requirements

1

  • The diagonal of the device where your application is running is within the diagonal range of the candidate view: [minimum diagonal, maximum diagonal].
  • Of the diagonal ranges of all the views, the diagonal range of the candidate view is closest to the diagonal of the device where your application is running.
  • The device class of the candidate view is the same as the class of the device where your application is running.

2

  • The diagonal of the device where your application is running is within the diagonal range of the candidate view: [minimum diagonal, maximum diagonal].
  • Of the diagonal ranges of all the views, the diagonal range of the candidate view is closest to the diagonal of the device where your application is running.
  • The device class of the candidate view is different from the class of the device where your application is running.
  • The candidate view is not exclusive.

3

Note: Views with this priority level are sorted by the difference between the mean of the diagonal range of the candidate view (for example, 5 if minimum is 4 and maximum is 6) and the diagonal of the device where your application is running. Views with the lowest difference have a higher priority.

FireMonkey loads the first view of the list. If none of the views matches any of the requirements, FireMonkey loads the Master view instead.

You can find all the details about the RAD Studio built-in views in the platform-specific implementations of the System.Devices unit: System.Android.Devices.pas, System.Win.Devices.pas, and so on.

Ranges of Device Sizes that Match Each Android View

There are ranges of sizes used to determine the view that is used in Android apps. The table below shows the ranges for each view, specified in landscape coordinates. Note that the runtime does not require an exact match to select a view. The runtime chooses the closest match.

View Name Minimum Size
(pixels)
Maximum Size
(pixels)
PPI

Android 3.5" Phone

800 x 500

1000 x 600

320

Android 4" Phone

1168 x 730

1360 x 850

320

Android 5" Phone

1440 x 900

1708 x 960

320

Android 7" Tablet

1708 x 960

1920 x 1200

320

Android 10" Tablet

2400 x 1500

2560 x 1600

320

Form Names Are Different at Run Time

At run time, the Name property of the forms of your application may not match the names that you defined at design time. If FireMonkey loads a view other than Master, the names of the forms are the name that you gave the forms in the Master view plus the code name of the loaded view, joined by an underscore (_). For example, if the name of your form is "MyForm" and FireMonkey loads the "Windows Desktop" view at run time, the runtime name of your form is "MyForm_Windows".

You Cannot Create Views with Nameless Components

Before you can create a new view for your project, you must ensure that all the components in your project have a name.

If you open a multi-device application with one or more views that contain components without a name, RAD Studio shows the following error message:

 Cannot inherit from form <form name>.  It contains a component with a blank name property.

Topics

See Also