Android Question B4Xpages how to load a panel saved by Designer

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys

I have what appears to be a simple question - How to you load a panel (created and saved using Designer) onto a B4X page?

I have tried the following code and it does not show the new panel.
My code to load panel:
Private Sub B4XPage_Created (Root1 As B4XView)
    Log("B4XAddPlayer_Created")
    Root = Root1
    Root.LoadLayout("frmAddPlayer") ' This is the B4X page - this bit works as expected.

    panel1.visible = false             ' This hides the original panel added to frmAddPlayer.
    ' This is my code - it was taken from Eric's example loading panels into customerlistview
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 280 , 290)
    p.LoadLayout("pnlAddPlayer")    ' This is the Panel containing a few views.
    p.BringToFront                    ' My bit just to make sure it appears on top
    p.Visible = True
End Sub

Basically, the old panel1 vanishes (as expected) but the new panel does not appear.

Some help, or a pointer a previous post, would be much appreciated.

Very kind regards
Dave
 
Solution
In my opinion it is not a good idea to mix up layouts in Designer and code.
Why do you add a Panel in the code to add a layout ?
Simply load the new layou directly onto Rootpanel

Brian Dean

Well-Known Member
Licensed User
Longtime User
Sorry for a rushed reply but I do not have much time. You have made two errors. You are declaring your new panel inside a subroutine - a local variable. When the subroutine ends the panel is destroyed. Secondly, you are not initialising the panel.

Try this code ...
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Dim p As Panel                                    ' Declare the panel here ####
   
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    p.Initialize("")                                    ' Initialise the panel #####
    p.Color = xui.Color_Green
    Root.AddView(p, 0, 0, 280dip, 200dip)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi Brian

Thanks help, looks like using methods for updating customerListView was totally barking up the wrong street. Did not even look at the Addview() (as they say "There are more things in heaven and earth, Horatio").

I look forward to giving your suggestion a try and report back (hopefully it will be useful for the community).

Regards
Dave
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi

Nearly worked but I need to load a layout generated by Designer - so l replace the line#18 with p.loadLayout("pnlAddPlayer") and it works.

However, for some reason the panel loads slightly high and undersize - not sure why. I have attached a example project file to show what I have done, if anybody can explain why the loaded panel is unsize I would be most grateful.

Also with regards to the layout file pnlAddPlayer.bal. I initially set the anchors on the view panel "pnlAddPlayer1" to top, bottom, left and right but for some reason it would not show until its anchors where changed to top and left only.

Thanks for the help.

Kind regards
Dave
 

Attachments

  • TestPanel.zip
    17.2 KB · Views: 239
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Root.Width, Root.Height - 100) 'set the content size
    p.LoadLayout("pnlAddPlayer")
    Root.AddView(p, 0, DipToCurrent(100), p.Width, p.Height)
End Sub

1656035716970.png
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In my opinion it is not a good idea to mix up layouts in Designer and code.
Why do you add a Panel in the code to add a layout ?
Simply load the new layou directly onto Rootpanel
 

Attachments

  • TestPanel1.zip
    33.1 KB · Views: 235
Upvote 0
Solution

TILogistic

Expert
Licensed User
Longtime User
or use a container panel for layout

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private PanelContainer As B4XView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    PanelContainer.LoadLayout("pnlAddPlayer")
End Sub

1656054963042.png

1656055107864.png
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys
I adopted Klaus's method of using root.loadlayout() - (see the attachment to his post) as I believe it gave the simplest solution and scaled itself to match the phone I was using.

Thanks to everybody for all the help

Kind reads
Dave
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Hi, Guys
I adopted Klaus's method of using root.loadlayout() - (see the attachment to his post) as I believe it gave the simplest solution and scaled itself to match the phone I was using.
Good, but your query was to 'load in a panel' so @Omar Parra A. gave solutions for it.
Anyway please mark @klaus post as solution.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately, quite some people ask a question, but do not explain the problem.
The answer to the question can be very different from the answer to the problem.
In my professional life I encountered this quite often:
I have a question ?
OK, but what is your problem.
With experience, I never answered the question before I got an explanation on 'what is the problem'.
And very often the solution was not a direct answer to the question, but another more efficient one.

Well, here, I gave an answer to the problem.
 
Upvote 0
Top