Android Question Button Click inside ScrollView

bijou

Member
Licensed User
Hi
i try to click on the buttons inside the scrollview, each one will open a new activity according to variable i, i use a code posted in the forum by claus and try to modify it but i don't can't find a way to use if condition clicking button inside scrollview to open each time a new page ! any help please.

B4X:
Sub Globals
    Dim ScrollView1 As ScrollView
    Dim lstChecks As List
    Dim height As Int
    height = 50dip

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Activity.LoadLayout("Layout1")
    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl = ScrollView1.Panel
    Activity.AddView(ScrollView1, 10%x, 10%y, 20%x, 60%y)
    lstChecks.Initialize
   
    For i = 1 To 50
        Dim btn As Button
        btn.Initialize("")
        btn.Text = i
        lstChecks.Add(btn)
        pnl.AddView(btn, 0, height * (i - 1), 40dip, height)
        'pnl.AddView(lbl1, 125dip, height * (i - 1), 120dip, height)
    Next
    pnl.Height = lstChecks.Size * height
    Activity.AddMenuItem("Display clicked", "btnClicked")
End Sub

Sub btnClicked_Click

    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To lstChecks.Size - 1
        Dim btn As Button
        btn = lstChecks.Get(i)
        Case i
        StartActivity("Page1")
        If btn(i) Is clicked Then
            StartActivity("Page1")
        End If
    Next
End Sub
 

klaus

Expert
Licensed User
Longtime User
I suppose that with 'claus' you mean me.
Which example code are you speaking of?
But to answer your question the code below does what you expect.
B4X:
Sub Globals
    Dim ScrollView1 As ScrollView
    Dim height As Int
    height = 50dip
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ScrollView1.Initialize(0)
    Activity.AddView(ScrollView1, 10%x, 10%y, 20%x, 60%y)

    For i = 1 To 50
        Dim btn As Button
        btn.Initialize("btnTest")    'set an event name
        btn.Text = i
        btn.Tag = i                 'set a Tag
        ScrollView1.Panel.AddView(btn, 0, height * (i - 1), 40dip, height)
        'pnl.AddView(lbl1, 125dip, height * (i - 1), 120dip, height)
    Next
    ScrollView1.Panel.Height = (i - 1) * height
End Sub

Sub btnTest_Click
    Private btn As Button
    Private Tag As Int
 
    btn = Sender
    Tag = btn.Tag
 
    Log("Button clicked: " & Tag)  'shows in the Logs which button was clicked
 
    Select Tag
        Case 1
        Case 2
        Case 3
            'etc
    End Select
End Sub
 
Upvote 0
Top