B4J Question Need help with some UI Math

techknight

Well-Known Member
Licensed User
Longtime User
Math is not my strong point. Never has been.

So, if i have a panel that is 428 tall, and i want to fit 5 labels in there that are 40 tall, How do I calculate and place them all evenly? Sometimes there might be 6 instead of 5 depending on the mode which is why i want to calculate this out.

Doing this dynamically in code btw, not in the visual designer.

Thanks.
 

aeric

Expert
Licensed User
Longtime User
You mean using designer script?
Maybe this post can help you.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi:

Doing this dynamically in code btw, not in the visual designer.

Maybe something in the middle?
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I dont want to use the designer script, Doing it in code because the thing will be dynamic. depending on whether you are running in basketball or volleyball mode, the number of entries change.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Something like this? I've not run it but the idea is fine.
B4X:
PanelHeight = 428
NumberOfLabels = 6
LabelHeight = 40
LabelTotalHeight = NumberOfLabels * LabelHeight
PanelFreeHeight = PanelHeight - LabelTotalHeight
VerticalGap = PanelFreeHeight / (NumberOfLabels + 1)

' there  are now various ways of distributing the labels
Label1.Top = VerticalGap
Label2.Top = Label1.Bottom   + VerticalGap
'...

' or in a loop you could use a Array, or a List with slightly different code
For i = 0 to labels.Length
  Dim lbl As Label = Labels(i)
  lbl.Top = (LabelHeight + VerticalGap ) * i + VerticalGap
Next
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Something like this? I've not run it but the idea is fine.
B4X:
PanelHeight = 428
NumberOfLabels = 6
LabelHeight = 40
LabelTotalHeight = NumberOfLabels * LabelHeight
PanelFreeHeight = PanelHeight - LabelTotalHeight
VerticalGap = PanelFreeHeight / (NumberOfLabels + 1)

' there  are now various ways of distributing the labels
Label1.Top = VerticalGap
Label2.Top = Label1.Bottom   + VerticalGap
'...

' or in a loop you could use a Array, or a List with slightly different code
For i = 0 to labels.Length
  Dim lbl As Label = Labels(i)
  lbl.Top = (LabelHeight + VerticalGap ) * i + VerticalGap
Next

That just might work!

Thanks. I will play with it and see. Then I need to post a question on how to access all the UI objects that are using the label/pane parent for each one.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ok, so i tried it and it didnt work. Here is the code:

B4X:
    Dim PlayerSlots(6) As Pane
    Dim StatPanelHeight As Int = 428
    Dim NumberOfPanes As Int = 6
    Dim PlayerSlotHeight As Int = 40
    Dim PaneTotalHeight As Int  = NumberOfPanes * PlayerSlotHeight
    Dim PanelFreeHeight As Int = StatPanelHeight - PaneTotalHeight
    Dim VerticalGap As Int = PanelFreeHeight / (NumberOfPanes + 1)
    
    For I = 0 To 5 Step 1
        PlayerSlots(I).Initialize("")
        PlayerSlots(I).LoadLayout("PlayerSlot")
        pnlHomeStats.AddNode(PlayerSlots(I), 0, (PlayerSlotHeight + VerticalGap) * 1 + VerticalGap, pnlHomeStats.Width, PlayerSlotHeight)
    Next

Here is the result, only adding one layout into the one pane:
1628609045884.png


Thoughts? Thanks
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
B4X:
pnlHomeStats.AddNode(PlayerSlots(I), 0, (PlayerSlotHeight + VerticalGap) * 1 + VerticalGap, pnlHomeStats.Width, PlayerSlotHeight)

Should be

B4X:
pnlHomeStats.AddNode(PlayerSlots(I), 0, (PlayerSlotHeight + VerticalGap) * i + VerticalGap, pnlHomeStats.Width, PlayerSlotHeight)

Looks the same?
 
Upvote 0
Top