Android Question How to add action on a code-created button ?

gotirod

New Member
Licensed User
Longtime User
Hello,
I did modify the slidingPanels example to add a button on the panel:

Sub Activity_Create(FirstTime As Boolean)
Dim panels(6) As Panel
For i = 0 To panels.Length - 1
panels(i).Initialize("panels")
panels(i).Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
Dim btn1 As Button
Dim lbl As Label
lbl.Initialize("")
btn1.Initialize("")
lbl.Text = "Page:" & i
lbl.TextSize = 20
lbl.TextColor = Colors.White
panels(i).AddView(lbl, 75%x, 2%y, 60%x, 30dip)
panels(i).AddView(btn1, 25%x, 10%y, 20%x, 70dip)
Activity.AddView(panels(i), 100%x, 0, 100%x, 100%y - 80dip) 'add the panel to the layout
Activity.AddMenuItem("Panel #" & i, "Menu")
Next
....
End Sub

As result, on each of my Panels I have a button created.
I would like to add action on each individual button when it's get pressed (btn1_click) on each panel.

How do I do that ?

Thanks.

rg.
 

mangojack

Expert
Licensed User
Longtime User
Hi gotirod..

Please use [ Code] [ /Code] tags (without spaces) .. make it much easier to read your code.

When you initialize your buttons stipulate the event sub name ..

B4X:
 btn1.Initialize("btn1")


Sub btn1_Click

' .... event code

End Sub

Cheers mj
 
Upvote 0

gotirod

New Member
Licensed User
Longtime User
Hi mj,
Are you saying that I've to declare a routine in the loop?
The code is
Sub Activity_Create(FirstTime As Boolean)
Dim panels(6) As Panel
For i = 0 To panels.Length - 1
panels(i).Initialize("panels")
panels(i).Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
Dim btn1 As Button
Dim lbl As Label
lbl.Initialize("")
btn1.Initialize("")
lbl.Text = "Page:" & i
lbl.TextSize = 20
lbl.TextColor = Colors.White
panels(i).AddView(lbl, 75%x, 2%y, 60%x, 30dip)
panels(i).AddView(btn1, 25%x, 10%y, 20%x, 70dip)
Activity.AddView(panels(i), 100%x, 0, 100%x, 100%y - 80dip) 'add the panel to the layout
Activity.AddMenuItem("Panel #" & i, "Menu")
Next
....
End Sub
 
Upvote 0

gotirod

New Member
Licensed User
Longtime User
btn1 is a local variable declared in the routine Activity_Create.
Could be more explicit,by modifying the code below?
[ Code]
Sub Activity_Create(FirstTime As Boolean)
Dim panels(6) As Panel
For i = 0 To panels.Length - 1
panels(i).Initialize("panels")
panels(i).Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
Dim btn1 As Button
Dim lbl As Label
lbl.Initialize("")
btn1.Initialize("")
lbl.Text = "Page:" & i
lbl.TextSize = 20
lbl.TextColor = Colors.White
panels(i).AddView(lbl, 75%x, 2%y, 60%x, 30dip)
panels(i).AddView(btn1, 25%x, 10%y, 20%x, 70dip)
Activity.AddView(panels(i), 100%x, 0, 100%x, 100%y - 80dip) 'add the panel to the layout
Activity.AddMenuItem("Panel #" & i, "Menu")
Next
'add the Left and Right button
btnLeft.Initialize("Left")
btnLeft.Text = ">"
Activity.AddView(btnLeft, 60%x, 100%y - 75dip, 100dip, 50dip)
btnRight.Initialize("Right")
btnRight.Text = "<"
Activity.AddView(btnRight, 10%x, 100%y - 75dip, 100dip, 50dip)
'Load Bitmaps for indicator
ActiveBitmap.Initialize(File.DirAssets, "indicator_active.png")
InactiveBitmap.Initialize(File.DirAssets, "indicator_inactive.png")
'*****************************
'Initialize the SlidingData object and set the array of panels.
'Then we call SlidingPanels.Initialize to prepare the animation objects.
'The last call to ChangePanel brings the first panel.
sd.Initialize
SlidingPanels.Initialize(sd, panels, True, 150)
sd.currentPanel = currentPanelBeforePaused - 1
Indicator = SlidingPanels.CreatePageIndicator(panels.Length, InactiveBitmap, 120dip, 16dip)
Activity.AddView(Indicator, (100%x - 120dip) / 2, 0, 120dip, 16dip)
Indicator.BringToFront
ChangePanel(0)
End Sub
[ /Code]
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
gotirod ... [ Code] [ /Code] tags (without spaces) ...

Misread your first post .. do you want all 6 buttons to execute different code when clicked ?
If so you could assign tags to the buttons when created and use that to determine what code to run ..

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim panels(6) As Panel
   For i = 0 To panels.Length - 1
   panels(i).Initialize("panels")
   panels(i).Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
   Dim btn1 As Button
   Dim lbl As Label
   lbl.Initialize("")
   btn1.Initialize("btn1")
   btn1.Tag = i
  '...................................

Then in the Click Event Sub ...

B4X:
Sub btn1_Click

   Dim v As View
   v = Sender 
 
   Dim strTag As String = v.tag
 
   Select Case strTag
     Case "0"     
       'execute button 0 's code ....
     Case "1"
       'execute button 1 's code ....
     'Case "2"
     'and so on  .............
   End Select
   
End Sub

Cheers mj
 
Last edited:
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I 've seen another strategy in AHDashboardExample. I think that it could answer your this.
 
Upvote 0
Top