Recent

Author Topic: Create secondary form when I press a jButton  (Read 3214 times)

Bandy

  • Jr. Member
  • **
  • Posts: 84
Create secondary form when I press a jButton
« on: July 22, 2025, 11:50:10 am »
Hello.

In LAMW4Windows v3.8, I want to create a secondary form when I press a jButton placed on the primary form and return to the primary form when I press the "come back" button (<). Please help...

dseligo

  • Hero Member
  • *****
  • Posts: 1682
Re: Create secondary form when I press a jButton
« Reply #1 on: July 22, 2025, 12:12:40 pm »
If i.e. your second form is fmForm2, something like this should work:
Code: Pascal  [Select][+][-]
  1. if fmForm2 = nil then
  2. begin
  3.   gApp.CreateForm(TfmForm2, fmForm2);
  4.   fmForm2.Init;
  5. end;
  6.  
  7. fmForm2.Show;

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: Create secondary form when I press a jButton
« Reply #2 on: July 22, 2025, 02:02:13 pm »
I think I made a confusion in my first post.

I have AndroidModule1 (primary form) with one Button1:jButton, statically created, by hand. When I press Button1 I want to show secondary form (maybe called AndroidModule2), that is statically created (not dynamically created by code), and contains one EditText1:jEditText and one ListView1:jListView. When secondary form it's shown, I want to press the "come back" button (<) on the screen and secondary form to be closed and primary form to come back on the display. I hope it's clear now.

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: Create secondary form when I press a jButton
« Reply #3 on: July 22, 2025, 10:32:52 pm »
This may help:

https://forum.lazarus.freepascal.org/index.php/topic,69165.0.html

Then use KEYCODE_BACK to catch your back key tap to close AndroidModule2 using the OnSpecialKeyDown event on the AndroidModule2 form:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule2.AndroidModule2SpecialKeyDown(Sender: TObject; keyChar: char;
  2.   keyCode: integer; keyCodeString: string; var mute: boolean);
  3. begin
  4. if (KeycodeString = 'KEYCODE_BACK') then
  5.   begin    
  6.    AndroidModule2.close;
  7.    mute := true;
  8.   end;
  9.  end;

Remember your Form will be transparent by default and you will need to set a BackgroundColor or you won't see it.
« Last Edit: July 22, 2025, 10:42:35 pm by nicelybrewed »
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: Create secondary form when I press a jButton
« Reply #4 on: July 23, 2025, 10:17:15 am »
I've read the link you sad it may help me, but the AndroidModule2 is created dynamically (by using code gApp.CreateForm). I don't want like this. I want to show secondary form AndroidModule2 like I was doing in Lazarus IDE 4.0 for Windows: from File Menu -> New Form (this means statically), then I will add Buttons, ListViews, etc. to the new form AndroidModule2 and then just show the AndroidModule2 (Form2.Show). This is what I want. I hope it's clear. Please help me, thanks is advance.

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: Create secondary form when I press a jButton
« Reply #5 on: July 23, 2025, 10:48:49 am »
You just need to show the form if it's already created? or do you mean you need to know how to create the form statically within Lazarus using LAMW?

If you already have the form created and you just have to show the form with form2.show, just make sure you have set a BackgroundColour on your second form or it will not show.
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: Create secondary form when I press a jButton
« Reply #6 on: July 23, 2025, 12:35:28 pm »
Yes, I mean I need to know how to create the form statically within Lazarus using LAMW. I just go to File Menu -> New... -> LAMW [GUI] Android jForm?

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: Create secondary form when I press a jButton
« Reply #7 on: July 23, 2025, 01:16:21 pm »
You will need to add a new 'LAMW [GUI] Android jForm', this will prompt to save/create a .pas file and it should add it to the uses clause of the project.lpr.
The issue you're probably running into is that you need to create/initialise the form during runtime before you show it, if you haven't already done this (even if your form is created/exists in your project), you can do this in the OnJNIPrompt event or when you click the button, you can create the form at that point, you can add something like below, then you can call Form2.show. As mentioned previously, make sure you set a background color on the second form, as it defaults to being transparent, you won't see it otherwise.

Code: Pascal  [Select][+][-]
  1. If (Form2 = nil) then
  2.  begin
  3.    gapp.CreateForm(TForm2, Form2);
  4.    Form2.Init();
  5.  end;

Then:

Code: Pascal  [Select][+][-]
  1. Form2.show;

I personally initialise/create the form at startup and just show/hide the form when I need to, but it depends how often you need to use it I guess, I use it a lot in my project.
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: Create secondary form when I press a jButton
« Reply #8 on: July 23, 2025, 02:29:08 pm »
It works ok, I just replaced Form2 with AndroidModule2.

Now, with AndroidModule 2 shown on the display, I want to return to first form (AndroidModule1) when I tap the return button (<) on the display. How can I do that, please?

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: Create secondary form when I press a jButton
« Reply #9 on: July 23, 2025, 04:21:24 pm »
Use KEYCODE_BACK to catch your back tap to close AndroidModule2 using the OnSpecialKeyDown event on the AndroidModule2 form:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule2.AndroidModule2SpecialKeyDown(Sender: TObject; keyChar: char;
  2.   keyCode: integer; keyCodeString: string; var mute: boolean);
  3. begin
  4. if (KeycodeString = 'KEYCODE_BACK') then
  5.   begin    
  6.    AndroidModule2.close;
  7.    mute := true;
  8.   end;
  9.  end;
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: Create secondary form when I press a jButton
« Reply #10 on: July 23, 2025, 04:38:20 pm »
It works, thanks!

Where can I find help info with examples for developing Android apps?

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: Create secondary form when I press a jButton
« Reply #11 on: July 23, 2025, 06:15:54 pm »
Glad you got it working.

You should have them bundled on your system under the LAMW directory (LazarusModuleWizard) in a directory called 'demos', failing that:

https://github.com/jmpessoa/lazandroidmodulewizard/tree/master/demos/GUI

https://github.com/jmpessoa/lazandroidmodulewizard/archive/refs/heads/master.zip
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: Create secondary form when I press a jButton
« Reply #12 on: July 24, 2025, 10:23:39 am »
But help files with explanations for Android development/programming where can I find?
« Last Edit: July 24, 2025, 10:32:43 am by Bandy »

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: Create secondary form when I press a jButton
« Reply #13 on: July 24, 2025, 11:12:47 pm »
The core code is pascal, plenty of documentation on the internet like:
https://www.freepascal.org/docs-html/current/ref/ref.html

Regards Android development relating to LAMW you need to know how a manifest file is put together, it does helps:
https://developer.android.com/guide/topics/manifest/manifest-intro

Also knowing how a gradle file is put together is useful to optimise your app for targeting specific devices and Android APIs:
https://developer.android.com/build/gradle-build-overview
This is a build automation tool, but there are specific tweaks you need to do to the build.gradle file before you add an app to the Google Play Store, or at least you should review what's in the build file before you publish.

Some docs here too relating to LAMW:
https://github.com/jmpessoa/lazandroidmodulewizard/tree/master/docs
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

 

TinyPortal © 2005-2018