Check out the new version in message #3.
Did you ever wanted to put a lot of buttons on a screen and have them fill the screen?
No, not yet? Then here's how you can accomplish that.
What does it look like on different devices?
The 100 buttons are placed on a panel (or pane) and that panel is used to put on a scrollview (or scrollpane).
If the screen is too small for the 100 buttons then you can scroll vertically to the one you want to click (or tap) on.
The layouts contain a scrollpane (or scrollview) and a pane (or panel).
The code contains tests for B4A and B4J code.
Here's the code:
And you can download the source code in the attachments.
Hope you find this piece of code useful and you can use other views like a label, a panel or an image in the grid...
Did you ever wanted to put a lot of buttons on a screen and have them fill the screen?
No, not yet? Then here's how you can accomplish that.
What does it look like on different devices?
The 100 buttons are placed on a panel (or pane) and that panel is used to put on a scrollview (or scrollpane).
If the screen is too small for the 100 buttons then you can scroll vertically to the one you want to click (or tap) on.
The layouts contain a scrollpane (or scrollview) and a pane (or panel).
The code contains tests for B4A and B4J code.
Here's the code:
B4X:
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
#if B4J
Private Pane1 As Pane
Private ScrollPane1 As ScrollPane
#End If
#if B4A
Private Panel1 As Panel
Private ScrollView1 As ScrollView
#End If
End Sub
Public Sub Initialize
B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
B4XPages.SetTitle(Me,"Buttons grid")
#if B4J
set_button_grid(99,50,50,5)
#End If
#if B4A
Dim scale As Int = GetDeviceLayoutValues.Scale
Dim tabletsize As Int = GetDeviceLayoutValues.ApproximateScreenSize
If tabletsize >= 8 Then
scale = 2
End If
set_button_grid(99,50dip*scale,50dip*scale,5dip)
Panel1.RemoveView
ScrollView1.Panel.AddView(Panel1,0dip,0dip,ScrollView1.Width,Panel1.Height)
ScrollView1.Panel.Height = Panel1.Height
#End If
End Sub
Private Sub B4XPage_Resize (Width As Int, Height As Int)
#if B4J
set_button_grid(99,50,50,5)
ScrollPane1.InnerNode = Pane1
#End If
End Sub
Private Sub set_button_grid(cnt As Int, width As Int, height As Int, gap As Int)
#if B4J
Pane1.RemoveAllNodes
#End If
#if B4A
Panel1.RemoveAllViews
#End If
Dim rowpos As Int = gap
Dim colpos As Int = gap
For i = 0 To cnt
Dim btn As Button
btn.Initialize("btn")
btn.Text = "b" & i
btn.Tag = btn.Text
#if B4J
btn.prefWidth = width
btn.PrefHeight = height
Pane1.AddNode(btn,colpos,rowpos,width,height)
#End If
#if B4A
btn.Width = width
btn.Height = height
Panel1.AddView(btn,colpos,rowpos,width,height)
#End If
colpos = colpos + width + gap
If colpos > (Root.Width - width) Then
rowpos = rowpos + height + gap
colpos = gap
End If
Next
#if B4J
Pane1.PrefHeight = rowpos + height + gap
#End If
#if B4A
Panel1.Height = rowpos + height + gap
#End If
End Sub
Private Sub btn_Click
Dim btn As Button = Sender
Log("btn " & btn.Tag & " clicked")
End Sub
Hope you find this piece of code useful and you can use other views like a label, a panel or an image in the grid...
Attachments
Last edited: