Skip to content Skip to sidebar Skip to footer

How Do I Detect / Handle A Screen Rotate Using Firemonkey For Delphi Xe5

First of all - I am a beginner when it comes to Android and FireMonkey programming, so please bear this in mind :-). I have made a FireMonkey/Android application that can resize/re

Solution 1:

If you can't get the form's OnResize event to work, then you can subscribe to the FMX orientation changed message thus:

uses
  FMX.Forms, FMX.Messages, FMX.Types;

//In the definition of TFooForm you define:
FOrientationChangedId: Integer;
procedure OrientationChangedHandler(const Sender: TObject; const Msg: TMessage);

//Subscribe to orientation change events in OnCreate or similar
FOrientationChangedId := TMessageManager.DefaultManager.SubscribeToMessage(
  TOrientationChangedMessage, OrientationChangedHandler);

//Unsubscribe from orientation change events in OnDestroy or similar
TMessageManager.DefaultManager.Unsubscribe(
  TOrientationChangedMessage, FOrientationChangedId);

procedure TFooForm.OrientationChangedHandler(const Sender: TObject; const Msg: TMessage);
begin
  Log.d('Orientation has changed');
end;

Solution 2:

to use IFMXScreenService is better test if is supported by the platform, so if isn't supported it generate a "Segmentation Fault" i use it like this:

uses FMXPlatform;

...

procedure TForm2.FormResize(Sender: TObject);
var
  ScreenService: IFMXScreenService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) thenbegin
    if ScreenService.GetScreenOrientation in [TScreenOrientation.soPortrait, TScreenOrientation.soInvertedPortrait] then
      ShowMessage('Portrait Orientation')
    elseBegin
      ShowMessage('Landscape Orientation');

     End;

  end;
end;

Solution 3:

You also can use next approach: When application is rotated, TForm.OnResize is invoked. So you can set handler on this event and check current orientation through service IFMXScreenService.GetScreenOrientation.

Post a Comment for "How Do I Detect / Handle A Screen Rotate Using Firemonkey For Delphi Xe5"