B4J Question Number of nodes on form pane doubles

emexes

Expert
Licensed User
I load a "flat" single-variant layout with five nodes on it. Nothing fancy: just 4 buttons and a text field.

I count the number of nodes using GetAllViewsRecursive:

# before loading the layout: 0 nodes - no worries
# after loading the layout: 5 nodes - no worries
# after MainForm.Show: 10 nodes - huh?
# after another MainForm.Show: 10 nodes - well, at least it's stable

What are the extra nodes for? I could imagine they are created during window (re)sizing, but... why are they not deleted? Should they be? Can I ignore them? Are they always -1 width and height?
B4X:
Sub AppStart (Form1 As Form, Args() As String)
 
    MainForm = Form1
 
            Dim NumNodes0 As Int  = 0
            For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                NumNodes0 = NumNodes0 + 1
            Next
 
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
 
            Dim NumNodes1 As Int  = 0
            For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                NumNodes1 = NumNodes1 + 1
            Next
 
    MainForm.Show
 
            Dim NumNodes2 As Int  = 0
            For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                NumNodes2 = NumNodes2 + 1
            Next
 
    MainForm.Show
 
            Dim NumNodes3 As Int  = 0
            For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                NumNodes3 = NumNodes3 + 1
            Next
 
    Log("NumNodes = " & NumNodes0 & " " & NumNodes1 & " " & NumNodes2 & " " & NumNodes3)
 
    For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
        Log(N.Left & " " & N.Top & " " & N.PrefWidth & " " & N.PrefHeight)
    Next
 
End Sub
produces log:
B4X:
Waiting for debugger to connect...
Program started.
NumNodes = 0 5 10 10
10 10 100 30
37 5 -1 -1
370 10 100 30
37 5 -1 -1
10 320 100 30
37 5 -1 -1
380 330 100 30
37 5 -1 -1
20 50 440 260
206 120 -1 -1
 

Daestrum

Expert
Licensed User
Longtime User
GetAllViewsRecursive is returning the correct count. A button is 2 nodes (views) a Button and a LabelledText node (the text of the button)

To get the nodes only (not sub nodes) use
B4X:
 For a = 0 To MainForm.RootPane.NumberOfNodes -1
  Dim n As Node = MainForm.RootPane.GetNode(a)
  Log(N.Left & " " & N.Top & " " & N.PrefWidth & " " & N.PrefHeight)
 Next

Or if you want to get child count often use a sub like (uses javaobject)
B4X:
Sub childCount(j As JavaObject) As Int
 Return j.RunMethodjo("getChildren",Null).RunMethod("size",Null)
End Sub

and call with
B4X:
...
Log(childCount(Mainform.RootPane))
...
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Or you could use
B4X:
Log(Mainform.RootPane.NumberOfNodes)
 
Upvote 0
Top