Android Question Create the Multiple Buttons

dragonguy

Active Member
Licensed User
Longtime User
I want to create the 96 buttons to the List View, just wanna find the simple way to create. I think my code so complicated.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim ScrollView1 As ScrollView
    Dim lst_b As List
    Dim height As Int
    height=50dip
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    If FirstTime=True Then

    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl=ScrollView1.Panel
    Activity.AddView(ScrollView1,0,0,100%x,100%y)
    lst_b.Initialize

    For i = 1 To 1
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,0%x,0,15%y,height)
    Next

    For i = 2 To 2
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,20%x,0,15%y,height)
    Next

    For i = 3 To 3
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,40%x,0,15%y,height)
    Next

    For i = 4 To 4
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,60%x,0,15%y,height)
    Next
    For i = 5 To 5
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,80%x,0,15%y,height)
    Next

    For i = 6 To 6
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,0%x,120,15%y,height)
    Next

    For i = 7 To 7
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,20%x,120,15%y,height)
    Next

    For i = 8 To 8
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,40%x,120,15%y,height)
    Next

    For i = 9 To 9
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,60%x,120,15%y,height)
    Next
    For i = 10 To 10
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,80%x,120,15%y,height)
    Next

    For i = 11 To 11
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,0%x,240,15%y,height)
    Next

    For i = 12 To 12
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,20%x,240,15%y,height)
    Next

    For i = 13 To 13
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,40%x,240,15%y,height)
    Next

    For i = 14 To 14
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,60%x,240,15%y,height)
    Next
    For i = 15 To 15
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,80%x,240,15%y,height)
    Next

    For i = 16 To 16
        Dim b As Button
        b.Initialize("button")
        b.text=i
        lst_b.Add(b)
        pnl.AddView(b,0%x,360,15%y,height)
    Next

    pnl.Height=lst_b.Size*height

    End If

End Sub



How to use the "for ... next" to simple create the buttons?
Please help me.
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    If FirstTime=True Then
   
    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl=ScrollView1.Panel
    Activity.AddView(ScrollView1,0,0,100%x,100%y)
    lst_b.Initialize
   
    For i = 0 To 3
        Dim b As Button
        b.Initialize("button")
        b.text=i+1
        lst_b.Add(b)
        pnl.AddView(b,1dip*i*90,0dip,100%x/4,50%y/4)
    Next
   
    For k = 0 To 3
        Dim b As Button
        b.Initialize("button")
        b.text=k+5
        lst_b.Add(b)
        pnl.AddView(b,1dip*k*90,80dip,100%x/4,50%y/4)
    Next
   
    For a = 0 To 3
        Dim b As Button
        b.Initialize("button")
        b.text=a+9
        lst_b.Add(b)
        pnl.AddView(b,1dip*a*90,160dip,100%x/4,50%y/4)
    Next
   
    For q = 0 To 3
        Dim b As Button
        b.Initialize("button")
        b.text=q+13
        lst_b.Add(b)
        pnl.AddView(b,1dip*q*90,240dip,100%x/4,50%y/4)
    Next
   
    pnl.height=lst_b.Size*height
   
    End If
   
End Sub



this my code, but this only until 16 button, i just looking for write less code to create 96 buttons.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are:
B4X:
Sub Globals
    Dim ScrollView1 As ScrollView
    Dim NbButtons = 96 As Int
    Dim NbButtonsPerRow = 4 As Int
    Dim DeltaX = 100%x / NbButtonsPerRow As Int
    Dim Width = DeltaX - 2%x
    Dim DeltaY = DeltaX / 1.5 As Int
    Dim Height = DeltaY - 2%x As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl=ScrollView1.Panel

    Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
   
    Dim x = 0 As Int
    Dim y = 0 As Int
   
    For i = 0 To NbButtons - 1
        Dim b As Button
        b.Initialize("Button")
        b.text = i + 1
        b.Tag = i + 1
        pnl.AddView(b, x, y, Width, Height)
        x = x + DeltaX
        If x > 80%x Then
            x = 0
            y = y + DeltaY
        End If
    Next
    pnl.Height = Ceil(NbButtons / NbButtonsPerRow) * DeltaY
End Sub

Sub Button_Click
    Dim btn As Button
    Dim Index As Int
   
    btn = Sender
    Index = btn.Tag
    Activity.Title = "Button " & Index & " pressed"
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…