Android Question How to press a button dynamically created?

vecino

Well-Known Member
Licensed User
Longtime User
Hello, how I can do?
Thanks and regards.

Sample Code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  CreateButtons( 10 )
  PressButton( 3 )
End Sub

Sub CreateButtons( iHowMany As Int )
  Dim i As Int
  '
  For i=0 To iHowMany -1
    Dim bt1 As Button
    bt1.Initialize("bt1")
    bt1.Tag = i
    '
    Activity.AddView(bt1, 10dip,10dip+(50*i),10%x,10%y)
  Next
End Sub

Sub bt1_Click
  Dim bt2 As Button = Sender
  Log(bt2.Tag)
End Sub

Sub PressButton( iButtonNumber As Int )
  bt1_Click ( iButtonNumber  )  '  <---  ?????????????????????
End Sub
 

klaus

Expert
Licensed User
Longtime User
You can either treat the click event directly in bt1_Click
B4X:
Sub bt1_Click
  Dim btn As Button = Sender
  Dim i As Int
  i = btn.Tag
  Select i
  Case 1
  Case 2
  .
  .
  End Select
End Sub
or use this code:
B4X:
Sub bt1_Click
  Dim bt2 As Button = Sender
  Dim i As Int
  i = Sender.Tag
  PressButton(i)
End Sub
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Sorry, I still do not understand. I will summarize:
B4X:
Sub CreateButtons
  Dim i As Int
  For i=0 To 9
    Dim bt1 As Button
    bt1.Initialize("bt1")
    bt1.Tag = i
    Activity.AddView(bt1, 10dip,10,10%x,10%y)
  Next
End Sub
How I can press any of the buttons?
B4X:
bt1_CLick      '  <--- ????????????
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In your CreateButtons you define several buttons and you set their event name to "bt1" !
This means that the events of ALL the buttons you defined in the loop have the same generic name.
If you click any of these buttons the bt1_Click event will be raised.
To check which button has raised the event we use the Tag property of the buttons.
The first button has Tag = 0
the second has Tag = 1
and so on.
With
B4X:
Dim i As Int
i = btn.Tag
Select i
Case 0
  ' code for button the first button
Case 1
  ' code for button the first button
Case n
.
End Select
you can add the code for each button.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thanks, I understand, and have no problem.
I think I have not explained properly, is the problem of not knowing the English language!
What I want is (in code) to simulate pressing a button.

How do I write to simulate pressing the button 1, 2, 3, etc..

bt1_Click?????

Thanks and regards.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Klaus already gave the right answer. Maybe is was not enough to state "just" the call of Pressbutton in his example above.

I have sorted it a bit and now it should be clear what Klaus wanted to suggest ;-)

Make a custom sub to handle the button-code of all buttons.

B4X:
Sub  PressButton(i as integer)
  Select i
  Case 1
  Case 2
  .
  .
  End Select
End Sub

Now we use the one Eventsub we need to handly all buttons.

B4X:
Sub bt1_Click
  Dim bt2 As Button = Sender
  ' Next line calls the sub we create above
  PressButton(bt2.Tag)
End Sub

You can use this to handle all the buttons you programmatically create AND you can simulate pressing button 3 with
B4X:
PressButton(2)
(in case you start count from 0 ;-))

Please note that you have to set the tag with integers only in this example. If you put a string in the tag then you have to change the PressButton-Signature which now expect "i" to be an integer. i dont know what type you need as parameter if you want to react like button.Tag. In a Tag you can store everything i think... Maybe "dim i as object"?
 
Last edited:
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thanks, friends, but I think it's not what I need :(

Once created buttons. How do I make pressing one by code?
B4X:
bt1_Click (3)??????
This is not press "from" inside "Sub bt1_Click" but call "Sub bt1_Click" from code. And I want to know which button to "simulate" pressing.

B4X:
Sub CreateButtons
  Dim i As Int
  For i=0 To 9
    Dim bt1 As Button
    bt1.Initialize("bt1")
    bt1.Tag = i
    Activity.AddView(bt1, 10dip,10,10%x,10%y)
  Next
End Sub

Sub AnotherSub
  bt1_Click ( 3 )    '   <----      ????????????????????  o_O
end Sub

Sub bt1_Click
  dim bb As Button = Sender
  log("The Button is: "&bb.tag)
end Sub
Thanks and regards.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub  PressButton(i as integer)
  Select i
  Case 1
  Case 2
  .
  .
  End Select
End Sub

B4X:
Sub CreateButtons
  Dim i As Int
  For i=0 To 9
    Dim bt1 As Button
    bt1.Initialize("bt1")
    bt1.Tag = i
    Activity.AddView(bt1, 10dip,10,10%x,10%y)
  Next
End Sub

Sub AnotherSub
  PressButton( 3 )   
end Sub

Sub bt1_Click
  dim bb As Button = Sender
  log("The Button is: "&bb.tag)
  PressButton(bb.tag)
end Sub
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
No, no, even insist, that does not work for what I get :D
B4X:
Sub PressButton(i as integer)
  Select i
  Case 1
  Case 2
  .
  .
  End Select
End Sub

Sub CreateButtons
  Dim i As Int
  For i=0 To 9
    Dim bt1 As Button
    bt1.Initialize("bt1")
    bt1.Tag = i
    Activity.AddView(bt1, 10dip,10,10%x,10%y)
  Next
End Sub

Sub AnotherSub
  PressButton( 3 )  ' <--  That call to Sub PressButton, but that do not need it.
                    '  I want to call bt1_Click with the tag of a button.
end Sub

Sub bt1_Click
  dim bb As Button = Sender
  log("The Button is: "&bb.tag)
  PressButton(bb.tag)      ' <---    That can not go there.
end Sub

Thank you very much :)
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Now I understand. Separate into another "Sub" what the buttons and call it directly when needed :oops:

That's perfect :)

Thank you very much, I feel not well understood because of "translate.google" :D

Regards.
 
Upvote 0
Top