Dialogs on Android. (Don’t brutally roll your own!)

Recently for my “Brutally Roll Your Own Backend” video series, I built a custom input dialog box for use on an Android device.
As I was writing it I knew there must be something not quite right about what I was doing, because I had to use “Application.ProcessMessages()” which is about as close as you can get to committing a sin for the FMX framework. None the less, I told myself I could simply correct this later and all would work out fine.

Well it did not work out fine. When I deployed the application to an Android device, the dialog would often hang the system. I did try to get that box working, and came close using the threading library, however I was never able to capture the “back” button being pressed on the users keyboard. My apologies to those that spent their time watching Episode 10 of the video series, but it was sadly wasted time. There is however a solution

Before we look at the solution, I’d like to explain why I decided to write my own dialog box in the first place. Well, simply put the functionality to create dialog boxes under the FMX and in particular for the Android platform has changed several times over recent versions and I’ve not kept up. The documentation on how to do this with the Berlin release of RAD / Delphi was not clear and is somewhat incomplete.

As it turns out, creating something as simple as a dialog box is somewhat tricky on the Android platform. I don’t fully understand why or the underlying details, but it’s this trickiness that has lead to such a turn-over in revisions for this functionality within the FMX framework. There is however a solution contained within the FMX framework, and here it is…

uses
  FMX.DialogService;

{...}

procedure TForm1.Button1Click(Sender: TObject);
begin
  TDialogService.InputQuery( 'Please enter some info:', ['Info'], ['default-value'],
    procedure (const AResult : TModalResult; const values: array of string)
    begin
      if AResult<>mrOk then begin
        Exit;
      end;
      //- Perform success action here.
	  ShowMessage( Values[0] );
    end
   );
end;

The TDialogService.InputQuery() method has several overloads. The one that I’m using here accepts a primary prompt text, then an array of input field captions, an array of default values, and an anonymous method to handle the results. Within the anonymous method, we can check the value of AResult to see that the dialog box “Okay” button was clicked, and then process the array of resulting values.

Sadly it appears that this issue may not be entirely resolved within the FMX framework, as the code above does cause errors on application shutdown on the windows platform. This is not too significant an issue, the dialog box that I built for the video series works for windows, so perhaps we can just use conditional defines to select the target appropriate solution for now. Rest assured, I’m following up these issues at Embarcadero with the development team, with the hope that we’ll have a good robust working solution soon.

Once again, my apologies to those of you that spent time going through the construction of a custom dialog box in Video #10. I hope that there was sufficient educational content in the video that you don’t feel it was a total loss. We now have a working solution for dialog boxes on Android. 

Thanks for reading!