Dynamical creation of objects lazarus / 43-Android ---------------------------------------------------------------------- peterbos · 2023-11-30 20:10 Hi, How do I create an object dynamically? I tried this (and lots of variations on it) but it doesn't show the CheckBox: procedure TAndroidModule1.AndroidModule1Create(Sender: TObject); begin   ajCheckBox:=jCheckBox.Create(Self);   ajCheckBox.Parent:=Self;   ajCheckBox.PosRelativeToParent:=[];   ajCheckBox.Anchor:=CheckBox1;   ajCheckBox.Text:= 'Hello World!';   ajCheckBox.PosRelativeToAnchor:=[raBelow];   ajCheckBox.LayoutParamHeight:=lpWrapContent;   ajCheckBox.LayoutParamWidth:=lpWrapContent;   ajCheckBox.Init; end; On this forum I also found ajCheckBox.Init(gApp) but that does not work because gApp is apparently not recognized. Peter ---------------------------------------------------------------------- jmpessoa · 2023-12-02 03:54 Do like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {hint: Pascal files location: ...\AppCreateComponentAtRuntime\jni }unit unit1; {$mode delphi} interface uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes, SysUtils, AndroidWidget, Laz_And_Controls;  type   { TAndroidModule1 }   TAndroidModule1 = class(jForm)    Button1: jButton;    TextView1: jTextView;    procedure AndroidModule1Create(Sender: TObject);    procedure AndroidModule1JNIPrompt(Sender: TObject);    procedure Button1Click(Sender: TObject);  private    {private declarations}    ButtonDynamic: jButton;  public    {public declarations}    procedure ButtonDynamicClick(Sender: TObject);  end; var  AndroidModule1: TAndroidModule1; implementation  {$R *.lfm}   { TAndroidModule1 } procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);begin   // put here only pure pascal code  (that is, don't put here JNI api interface code...)end; //put here JNI api interface code...//note that you will necessarily need a component of the same type on the form....procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);begin  if ButtonDynamic = nil then  begin     //ShowMessage('"ButtonDynamic" Created at Runtime...!');     ButtonDynamic:= jButton.Create(Self);  //pascal side button     ButtonDynamic.Tag:= 2;     ButtonDynamic.Parent:= Self;     ButtonDynamic.Text:= 'ButtonDynamic';     ButtonDynamic.LayoutParamWidth:= lpTwoThirdOfParent;     ButtonDynamic.Anchor:= Button1;     ButtonDynamic.PosRelativeToAnchor:= [raBelow];     ButtonDynamic.PosRelativeToParent:= [rpCenterHorizontal];     ButtonDynamic.OnClick:= ButtonDynamicClick;  //delphi mode...     ButtonDynamic.Init;    //init java side button  end;end; procedure TAndroidModule1.Button1Click(Sender: TObject);begin  ShowMessage('Hello from "Button1" ...');end; procedure TAndroidModule1.ButtonDynamicClick(Sender: TObject);begin   ShowMessage('Hello from "ButtonDynamic" Created at Runtime!')end; end. ---------------------------------------------------------------------- maxerist · 2023-12-10 10:17 Quote from: jmpessoa on December 02, 2023, 04:54:09 am Do like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ....     ButtonDynamic:= jButton.Create(Self);  //pascal side button.....  I little observation about ownership. I suspect that there are cases when it's better to pass Nil and free the control (component) by own means. This is because jForm will call Init for every control owned when you leave the app (running) and start the cached app again. For me this was unconvinient when I used jBitmap and wanted to keep it loaded. SO, when I created a jBitmap with jForm ownership, I got the empty one after returning to the app (since Init clears the internal bitmap). And when I passed Nil, everything was ok ---------------------------------------------------------------------- jmpessoa · 2023-12-10 22:19 QuoteAnd when I passed Nil, everything was ok... Nice!