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

#### Alexander Stolte

##### Expert
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)

Next``````
All my attempts were not centered at the end.

#### Attachments

• test.zip
9.1 KB · Views: 144
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``````

#### William Lancee

##### Well-Known Member
Longtime User
Do you want the x-offset for the 5 parts?

Total width of five parts is W = 5 * ( widthHeight + GapBetween) - GapBetween
x-offset is root.width / 2 - W / 2

#### Alexander Stolte

##### Expert
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``````

#### William Lancee

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

Where xOffset is defined as in #2

#### William Lancee

##### Well-Known Member
Longtime User
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
Longtime User
Thank you very much!

Replies
7
Views
3K
Replies
236
Views
31K
Replies
19
Views
8K
Replies
21
Views
7K
Replies
1
Views
7K