There is a small problem in your source code: it will create and add new buttons every time the page appears (B4XPage_Appear!).
Did you know that in a B4XPage project you can only have one orientation, vertical or horizontal (unless you create another Activity in addition to the Main)?
Yes, I know, but my problem was a long time ago, before B4XPage's existed.
There is a small problem in your source code: it will create and add new buttons every time the page appears (B4XPage_Appear!).
The variables (IntCols and IntRows) should actually be a user option, ie you can decide how many buttons to show.
For example
5x3
8x4
[CODE lang="b4x" title="Last version of my code"]Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private pnlButtons As B4XView
Type btnObj (Text As String, Tag As Object, Picture As Bitmap, Padding As Int, Font As B4XFont)
Private intCols As Int = 8
Private intRows As Int = 4
End Sub
Public Sub Initialize
B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Appear
End Sub
Private Sub B4XPage_Created (RootMain As B4XView)
Root = RootMain
Root.LoadLayout("MainPage")
B4XPages.SetTitle(Me, Main.loc.Localize("app_name"))
ButtonShow
End Sub
Private Sub ButtonsCreator(btnProp As btnObj) As Button
Dim b As Button
b.Initialize("btn")
b.Text = btnProp.Text
b.TextSize = btnProp.Font.Size
b.Tag = btnProp.Tag
b.SingleLine =False
b.Padding = Array As Int (btnProp.Padding, btnProp.Padding, btnProp.Padding, btnProp.Padding)
If btnProp.Picture.IsInitialized Then
b.SetBackgroundImage(btnProp.Picture)
End If
Return b
End Sub
Private Sub ButtonShow()
Dim intSize As Int = ButtonSize(200, 40)
pnlButtons.SetLayoutAnimated(0, (Root.Width - (intSize * intCols)) / 2, (Root.Height - (intSize * intRows)) / 2, (intSize * intCols), (intSize * intRows))
Dim intCnt As Int = 0
For row = 0 To intRows - 1
For col = 0 To intCols - 1
Dim btnProp As btnObj
btnProp.Initialize
btnProp.Text = row.As (String) & "," & col.As (String)
btnProp.Tag = intCnt
btnProp.Padding = 2
btnProp.Picture = LoadBitmap(File.DirAssets, "logo.png").Resize(intSize, intSize, True)
pnlButtons.AddView(ButtonsCreator(btnProp), col * intSize, row * intSize, intSize, intSize)
intCnt = intCnt + 1
Next
Next
End Sub
Private Sub ButtonSize(SafeAreaW As Int, SafeAreaH As Int) As Int
Dim intSize As Int = 0
Dim intWidth As Int = (Root.Width / intCols) - (SafeAreaW / intCols)
Dim intHeight As Int = (Root.Height / intRows) - (SafeAreaH / intRows)
If intHeight > intWidth Then
intSize = intWidth
Else
intSize = intHeight
End If
Return intSize
End Sub
Private Sub btn_Click
ToastMessageShow("Acción: " & Sender.As (Button).Tag, False)
End Sub
Private Sub btn_LongClick
ToastMessageShow("Configuración: " & Sender.As (Button).Tag, False)
End Sub[/CODE]