Dim x, y As Int
Dim width, offsetX, offsetY As Int
width = 37dip
offsetX = ((layoutval.Width - bts*47)/2)/2'left (100%x - width * sw - 10dip * 2 ) / 2
offsetY = 20'top(100%y - width * sw - 10dip * 2) / 2
For x = 0 To sw
For y = 0 To 1
Dim b As Button
b.Initialize("button") 'All buttons share the same event sub
Activity.AddView(b,offsetX + x * (width + 3dip), offsetY + y * (width + 3dip), width, width)
Buttons(x, y) = b 'store a reference to this view
Next
Next