Android Question LoadLayout - Proper way

Robert Valentino

Well-Known Member
Licensed User
I have a designer layout that I only need to load when a user asks for it - once loaded it stays around for later use

B4X:
   Private sPI_Partners_Main                            As Panel
   Private    sPI_Partners_Panel                        As Panel
   Private        sPI_Partners_Title                    As Label
   Private        sPI_Partners_List                    As ListView
   Private        sPI_Partners_OK                       As Button
I use to do the following:

B4X:
           If  sPI_Partners_Main.IsInitialized = False Then
               sPI_Partners_Main.Initialize("")

               mPlayerInfoPanel.AddView(sPI_Partners_Main, 0, 0, 100%x, 100%y)
         
               sPI_Partners_Main.LoadLayout("sPI_PartnersList")     
           end if
But doing the above code kept giving me Warning #20 - was added with designer should not be initialized

I do not like over-riding warnings because I could miss something (and at 68+ I am missing a lot).

So I changed the code to this

B4X:
   If  sPI_Partners_Main.IsInitialized = False Then
               Dim  LoadPanel As Panel
                     
               LoadPanel.Initialize("")

               mPlayerInfoPanel.AddView(LoadPanel, 0, 0, 100%x, 100%y)
         
               LoadPanel.LoadLayout("sPI_PartnersList")         

               sPI_Partners_Main = LoadPanel.GetView(0)
  end if
NOW the new code works fine and gets rid of the Warning #20 but looks convoluted

What is the proper way to load a designer layout dynamically.

BobVal
 

Robert Valentino

Well-Known Member
Licensed User
"programming mistake", not sure what you mean. This is a layout I load when needed (as stated above). Either I am not explaining correctly? The actual code is above in post #1
 

Robert Valentino

Well-Known Member
Licensed User
I guess I could move panel code (shown below) to my Main Panel and have it load with that. But this panel is shown so infrequently that I thought it would be best to load when needed

B4X:
Private sPI_Partners_Main                            As Panel
   Private    sPI_Partners_Panel                        As Panel
   Private        sPI_Partners_Title                    As Label
   Private        sPI_Partners_List                    As ListView
   Private        sPI_Partners_OK                       As Button
 

walterf25

Expert
Licensed User
What Erel is explaining, is that the warning you are receiving is due to the fact that you have already added one or more of the views on your layout, so there is no need to Initialize the view, for example I see in your code you have this line
B4X:
sPI_Partners_Main.Initialize("")
If this panel was added to your layout file using the Visual Designer, then there is no need to manually Initialize it in your code.

Walter
 

Robert Valentino

Well-Known Member
Licensed User
I understand.

But this panel is not loaded when I load my main layout.

if I were to do a sPI_Partners_Main.LoadLayout("sPI_PartnersList") I would crash because sPI_Partners_Main is NOT Initialized

I guess I'm just not explaining it right. So let me try again.

I have a splayerinfo.bal which is loaded with Activity.LoadLayout("splayerinfo") and contains about 50 different buttons, toggles, etc

I also have spi_partnerslist.bal which I only need to load when needed and contains the 5 screen fields described above
Now sPI_Partners_Main is in the spi_partnerslist.bal file so it was created with designer but because I haven't loaded the .bal file is isn't initialized

It's NOT initialized because I haven't loaded the .bal file so I can't do a LoadLayout on it because it's not initialized, and if I initialize it then I get the warning that it is initialized in designer.
Hench I did the following code to get around it:
B4X:
If  sPI_Partners_Main.IsInitialized = False Then
               Dim  LoadPanel As Panel
                     
               LoadPanel.Initialize("")

               mPlayerInfoPanel.AddView(LoadPanel, 0, 0, 100%x, 100%y)
         
               LoadPanel.LoadLayout("sPI_PartnersList")         

               sPI_Partners_Main = LoadPanel.GetView(0)
  end if
HOW do you load a layout that is NOT part of the Activity Layout
 

walterf25

Expert
Licensed User
if I were to do a sPI_Partners_Main.LoadLayout("sPI_PartnersList") I would crash because sPI_Partners_Main is NOT Initialized
Are you saying that sPI_Partners_Main is a panel that you added to a layout with the Visual Designer?
So basically you want to load another layout to a panel that is in a different layout?

If that is the case that is a very unusual way of doing things, if you are trying to load different layout, why don't you just create separate activities and load the required layouts in each separate activity, I think this will make things way easier.

Walter
 

Robert Valentino

Well-Known Member
Licensed User
Yes to sPI_Partners_Main being a panel that is a layout added with VD

Why would I need another activity.

The users asks for some action. I do the action and load and display the information then close the panel when complete.
There is no reason for another activity.

Think of this like a directory picker.
When you call FilePicker it loads a list of directories and displays it.
You aren't starting another activity it is just displaying another panel on top of your main panel and gets closed when done.

My users may only ask for this information once in 1000 times, if ever.
So I only load this panel when needed. Once loaded I would reuse it if they requested it again in the same session.

I could add all these fields to my main panel and have them set as Visible = false and Enabled = false. But why load all the extra fields if they aren't being used.

BOTH my examples in Post #1 work perfectly.

Doing it like I did in the first "If" generates the Warning #20.

Doing it like I did in the second "If" work perfectly with No warning. But seems a lot more complex then the actual task I need to do.

I'm doing a small project but it is basically the code from Post 1
 

Robert Valentino

Well-Known Member
Licensed User
I guess what I should have done was ask for a new function like:

Initialize2(<event>) or ' just initialize no complier warning
Initialize2(<event>, <Layout Name>) ' that would initialize and load the layout and no compiler warning
 

klaus

Expert
Licensed User
There is no need to add any other Panel on the main Activity to load SubPanel.bal, you should load the layout directly onto the Activity.
You should not position the views in the code! You should position them in the DesignerScript and use Anchors.
For example, the code below:
B4X:
Panel_SubPanel_Main.Left = 0
Panel_SubPanel_Main.Top = 0
Panel_SubPanel_Main.Width = 100%x
Panel_SubPanel_Main.Height = 100%y
Should be replaced by setting the two Anchors:
upload_2019-5-2_9-32-26.png


Attached you find a modified version of your test project.
 

Attachments

Top