Sample code for how to use the AlertDialog Builder setPositiveButton (delphi)

Hi, I’m trying to figure out how to code the AlertDialog Builder (Delphi), specifically how to handle a Yes button click. Does anyone have some sample code I could see please. I’ve watched Brian Long’s Torch video which handles this but his code for the CloseTorchDlg “Builder.PositiveButton” seems to be no longer valid. I’m guessing this has been replaced with Builder.setPositiveButton but I can’t work out how to assign the OnClick event handler for this. TIA

Android API’s typically don’t use “event handlers” in the way that you would recognise in Delphi. In the case of AlertDialog Builder.PositiveButton, the “click handler” is required to be a reference to an implementation of an interface supporting the required callback.

So you can either implement this interface on the class that is presenting the AlertDialog and then pass a reference to that instance, or you could create a separate class for this purpose.

In Oxygene you would typically use an inline, anonymous class. I don’t have a specific example for the AlertDialog.Builder case, but here’s an example of assigning an OnCompletion listener to a MediaPlayer:

media.OnCompletionListener := 
   new class MediaPlayer.OnCompletionListener
      (
       onCompletion :=  method(aPlayer: MediaPlayer)
                        begin
                          aPlayer.release();
                        end
      );

It should be fairly obvious how to adapt this for the onClick listener required for the AlertDialog Builder case (in Oxygene), though I’m afraid I couldn’t tell you how to adapt this to Delphi. Sorry, I know that is what you were asking for, but hopefully the Oxygene way will give you the pointers you need.

Hi, thanks for taking the time to respond. I did manage to figure it out with some help from your example code, only took me another 3 hours … man this is a steep learning curve for me. I’m sure this can be optimised or done better but …

I had a static method declared as …

MainActivity = public class(Activity)
private

public

class method CloseAppButtonOnClick(d: DialogInterface; z: integer); static;
end;

class method MainActivity.CloseAppButtonOnClick(d: DialogInterface; z: Integer); //(v: View);
begin
System.exit(0);
end;

Then with the AlertDialog.Builder…

var Builder := new AlertDialog.Builder(self);
Builder.Title := 'Close My App Dialog!';
Builder.Message := 'Close this App?';
var dlgPosButtonClick: DialogInterface.OnClickListener; 
dlgPosButtonClick := @MainActivity.CloseAppButtonOnClick;
Builder.setPositiveButton('Yes', dlgPosButtonClick);
Builder.setNegativeButton('No', nil);
exit Builder.&create;

again thanks for your help.