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

Discussion in 'B4J Questions' started by MikeSimpson, Mar 14, 2015.

  1. MikeSimpson

    MikeSimpson Member Licensed 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?
     
  2. zyblux

    zyblux Member Licensed 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.
     
  3. MikeSimpson

    MikeSimpson Member Licensed 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!
     

    Attached Files:

  4. zyblux

    zyblux Member Licensed 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 :)
     

    Attached Files:

    MikeSimpson likes this.
  5. MikeSimpson

    MikeSimpson Member Licensed User

    Thank you very mutch. It works great.:)
     
  6. patrick14384

    patrick14384 Member Licensed 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
    Code:
    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.
     
  7. MikeSimpson

    MikeSimpson Member Licensed 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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    The equivalent B4J method is Pane.RemoveAllNodes.
     
    patrick14384 and MikeSimpson like this.
  9. MikeSimpson

    MikeSimpson Member Licensed 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice