B4J Question (Sloved) How to load - unload and replace Layouts?

MikeSimpson

Member
Licensed User
Longtime User
Hello,

how do I handle multiple layouts?
If I have three layouts, Layout1, Layout2 and Layout3, how can I change between them in a program?

I started with this:

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
End Sub

To load Layout2 I used:

MainForm.Close
MainForm.RootPane.LoadLayout(Layout2")
MainForm.Show

It is loading, but on top of Layout1. How to do it correct?
 

zyblux

Member
Licensed User
Longtime User
Hi,
I had the same problem today and I decided to write a method like this to flip between them:

Sub SwitchTo(pane As String)

For Each n As Node In Array As Node(pane1, pane2, pane3 )
n.Visible = False
Next

Select Case content
Case PANE1: pane1.Visible = True
Case PANE2: pane2.Visible = True
Case PANE3: pane3.Visible = True
End Select

End Sub

I bascially have 1 content pane and I load 3 layouts into it, each with it's own pane. I like being able to update all 3 panes in the background and switching to any of them when needed.

I don't know if this is the most idiomatic way to do it in B4J though?!

Thanks.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
It look good, but I don't know how I can implement this to my code.
I have made a small sample with 3 Layouts. (see attachment) In this sample every Layout is put on top of the other.
How do I load them so that I can hide two of them, showing only one?
Would be very helpfull if you could implement your code into my sample.

Thank you!
 

Attachments

  • LayoutTest.zip
    2.6 KB · Views: 242
Upvote 0

zyblux

Member
Licensed User
Longtime User
Hi,
I have changed your sample app so you can switch between layouts.

Any questions, please let me know.

Cheers
p.s. I don't know if this is the ebst way to do it but it seems to work fine :)
 

Attachments

  • MuliLayouts.zip
    3.1 KB · Views: 335
Upvote 0

patrick14384

Member
Licensed User
Longtime User
Not questioning the validity of the proposed solution, but searching for a solution for my program I had the same issue. To solve it I remove all of the views before loading the new layout like this
B4X:
Dim teststring As String
teststring = LayoutLoadedList.Get(0) & ".bal"
If File.Exists(File.DirAssets,teststring) Then
    Activity.RemoveAllViews
    Activity.LoadLayout(LayoutLoadedList.Get(0) & ".bal")
Else
    ErrorHandler("File is missing.")
End If

Again, not saying this is better, but for me it worked much better than putting everything into panels.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Are you sure that you code is for B4J and not for B4A? Didn't work for me in B4J.
As far as I know "Activity.RemoveAllViews" is B4A code and not for B4J.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thanks Earl, that was what I was looking for. Mutch easier than the example from zyblux. But the solution from zyblux is great when you want to keep one panel open.
 
Upvote 0
Top