Android Question How to access 16 buttons ?

Hamo

Member
Licensed User
Longtime User
I have 16 toggle buttons which are used as input state indicators and I need to update the color and text on the buttons as inputs change. I am using 16 IF - ELSE statements but feel there must be a more elegant way of doing this with a FOR loop using an index. I already have tags (0 to 15) assigned to buttons but how can I access the buttons' properties in a FOR loop. I understand how to use SENDER when a button is clicked.
Hamo
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I am using 16 IF - ELSE statements but feel there must be a more elegant way of doing this
There are several ways.

You can add all the buttons to a List and then go over the items in the list:
B4X:
For Each tg As ToggleButton In ListOfButtons
 ..
Next

If all these buttons are children of the same parent then you can do something like:
B4X:
For Each v As View in Panel1
 If v Is ToggleButton Then
  Dim tb As ToggleButton = v
  ...
 End If
Next
 
Upvote 0
Top