Recent

Author Topic: Dynamical creation of objects  (Read 1179 times)

peterbos

  • Newbie
  • Posts: 3
Dynamical creation of objects
« on: November 30, 2023, 09:10:55 pm »
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

  • Hero Member
  • *****
  • Posts: 2317
Re: Dynamical creation of objects
« Reply #1 on: December 02, 2023, 04:54:09 am »

Do like this:

Code: Pascal  [Select][+][-]
  1. {hint: Pascal files location: ...\AppCreateComponentAtRuntime\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  10.   cthreads,
  11.   {$ENDIF}{$ENDIF}
  12.   Classes, SysUtils, AndroidWidget, Laz_And_Controls;
  13.  
  14. type
  15.  
  16.   { TAndroidModule1 }
  17.  
  18.   TAndroidModule1 = class(jForm)
  19.     Button1: jButton;
  20.     TextView1: jTextView;
  21.     procedure AndroidModule1Create(Sender: TObject);
  22.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  23.     procedure Button1Click(Sender: TObject);
  24.   private
  25.     {private declarations}
  26.     ButtonDynamic: jButton;
  27.   public
  28.     {public declarations}
  29.     procedure ButtonDynamicClick(Sender: TObject);
  30.   end;
  31.  
  32. var
  33.   AndroidModule1: TAndroidModule1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39.  
  40. { TAndroidModule1 }
  41.  
  42. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  43. begin
  44.    // put here only pure pascal code  (that is, don't put here JNI api interface code...)
  45. end;
  46.  
  47. //put here JNI api interface code...
  48. //note that you will necessarily need a component of the same type on the form....
  49. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  50. begin
  51.   if ButtonDynamic = nil then
  52.   begin
  53.      //ShowMessage('"ButtonDynamic" Created at Runtime...!');
  54.      ButtonDynamic:= jButton.Create(Self);  //pascal side button
  55.      ButtonDynamic.Tag:= 2;
  56.      ButtonDynamic.Parent:= Self;
  57.      ButtonDynamic.Text:= 'ButtonDynamic';
  58.      ButtonDynamic.LayoutParamWidth:= lpTwoThirdOfParent;
  59.      ButtonDynamic.Anchor:= Button1;
  60.      ButtonDynamic.PosRelativeToAnchor:= [raBelow];
  61.      ButtonDynamic.PosRelativeToParent:= [rpCenterHorizontal];
  62.      ButtonDynamic.OnClick:= ButtonDynamicClick;  //delphi mode...
  63.      ButtonDynamic.Init;    //init java side button
  64.   end;
  65. end;
  66.  
  67. procedure TAndroidModule1.Button1Click(Sender: TObject);
  68. begin
  69.   ShowMessage('Hello from "Button1" ...');
  70. end;
  71.  
  72. procedure TAndroidModule1.ButtonDynamicClick(Sender: TObject);
  73. begin
  74.    ShowMessage('Hello from "ButtonDynamic" Created at Runtime!')
  75. end;
  76.  
  77. end.
  78.  
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maxerist

  • New Member
  • *
  • Posts: 28
Re: Dynamical creation of objects
« Reply #2 on: December 10, 2023, 11:17:54 am »

Do like this:

Code: Pascal  [Select][+][-]
  1. ....
  2.      ButtonDynamic:= jButton.Create(Self);  //pascal side button
  3. .....
  4.  

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

  • Hero Member
  • *****
  • Posts: 2317
Re: Dynamical creation of objects
« Reply #3 on: December 10, 2023, 11:19:36 pm »
Quote
And when I passed Nil, everything was ok...

Nice!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018