B4J Question [B4X] How to center panels in a loop with gap

Alexander Stolte

Expert
Licensed User
Longtime User
I feel pretty stupid right now, but I just can't get these panels to display centered in the loop with spacing between panels.
B4X:
    Dim GapBetween As Float = 20dip
   
    Dim WidthHeight As Float = 20dip
   
    For i = 0 To 5 -1
       
        Dim xpnl As B4XView = xui.CreatePanel("")
        xpnl.SetColorAndBorder(xui.Color_Red,0,0,WidthHeight/2)
        Root.AddView(xpnl,(WidthHeight + GapBetween)*i,Root.Height/2 - WidthHeight/2,WidthHeight,WidthHeight)
       
    Next
All my attempts were not centered at the end.
1649448559041.png
 

Attachments

  • test.zip
    9.1 KB · Views: 206
Solution
B4X:
Dim totalWidth as float = 5 * (widthHeight + GapBetween) -  GapBetween
Dim xOffset as Float = root.width / 2 - totalWidth / 2
Dim pnlLeft as float = xOffset + (widthHeight + GapBetween) * i

Alexander Stolte

Expert
Licensed User
Longtime User
is unfortunately not quite centered, or have I read something wrong?
B4X:
Dim TotalWidth As Float = WidthHeight*5
xpnl.Left = i* (WidthHeight + GapBetween) - GapBetween + Root.Width/2 - TotalWidth/2
1649451453874.png
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
B4X:
Root.AddView(xpnl,xOffset + (WidthHeight + GapBetween)*i,Root.Height/2 - WidthHeight/2,WidthHeight,WidthHeight)

Where xOffset is defined as in #2
 
Upvote 0
Top