Android Question Referencing multiple objects

joilso oliveira da silva

Member
Licensed User
Longtime User
Hi everyone.

Assuming I put a button on a screen, and I want all of it to be disabled. I have to by the command button.enable = False.
Now suppose on that same screen I want to put 40 buttons.
I'll have to put btn1.enable = False for all of them? Or is there any way to steady them all at once? So I would put Btn.enable = False and all 40 buttons would be disabled.


For this example I refer to buttons, but if there is this way re references 40 objects of the same pattern (button, label, edittext, panel, etc ...) how do I reference others too? for example use the same thing, but for edittext and labels?
 

Myr0n

Active Member
Licensed User
Longtime User
Try this code, it gonna disabled all the buttons that are on the activity, inside of the FOR you can have exceptions using If or Select Case.

B4X:
    For ii = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(ii) Is Button Then Activity.GetView(ii).Enabled = False    
    Next
 
Upvote 0

Myr0n

Active Member
Licensed User
Longtime User
For Labels.

B4X:
    For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(ii) Is Label Then Activity.GetView(ii).Enabled = False     
    Next
 
Upvote 0

Myr0n

Active Member
Licensed User
Longtime User
For Panels.

B4X:
    For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(ii) Is Panel Then Activity.GetView(ii).Enabled = False     
    Next

Can you see, what changed in the 3 snippets?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Try this code
it iwill not work with all Properties of a Button for ex. Enable, yes. But Text no.
Same for Labels, Panels.

Example to change all Buttons, all Labels and all Panels (it will work with any other View too). And the possibility to change ALL their properties too...

Here 20 Buttons,5 Labels and 5 Panel
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private Button5 As Button
    Private Button6 As Button
    Private Button7 As Button
    Private Button8 As Button
    Private Button9 As Button
    Private Button10 As Button
    Private Button11 As Button
    Private Button12 As Button
    Private Button13 As Button
    Private Button14 As Button
    Private Button15 As Button
    Private Button16 As Button
    Private Button17 As Button
    Private Button18 As Button
    Private Button19 As Button
    Private Button20 As Button
 
    Private allbtn As Map
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
    Private Panel5 As Panel
    Private Panel4 As Panel
    Private Panel3 As Panel
    Private Panel2 As Panel
    Private Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout40")
    allbtn.Initialize
    allbtn.Put("btn"&1,Button1)
    allbtn.Put("btn"&2,Button2)
    allbtn.Put("btn"&3,Button3)
    allbtn.Put("btn"&4,Button4)
    allbtn.Put("btn"&5,Button5)
    allbtn.Put("btn"&6,Button6)
    allbtn.Put("btn"&7,Button7)
    allbtn.Put("btn"&8,Button8)
    allbtn.Put("btn"&9,Button9)
    allbtn.Put("btn"&10,Button10)
    allbtn.Put("btn"&11,Button11)
    allbtn.Put("btn"&12,Button12)
    allbtn.Put("btn"&13,Button13)
    allbtn.Put("btn"&14,Button14)
    allbtn.Put("btn"&15,Button15)
    allbtn.Put("btn"&16,Button16)
    allbtn.Put("btn"&17,Button17)
    allbtn.Put("btn"&18,Button18)
    allbtn.Put("btn"&19,Button19)
    allbtn.Put("btn"&20,Button20)
    allbtn.Put("lbl"&1,Label1)
    allbtn.Put("lbl"&2,Label2)
    allbtn.Put("lbl"&3,Label3)
    allbtn.Put("lbl"&4,Label4)
    allbtn.Put("lbl"&5,Label5)
    allbtn.Put("pnl"&1,Panel1)
    allbtn.Put("pnl"&2,Panel2)
    allbtn.Put("pnl"&3,Panel3)
    allbtn.Put("pnl"&4,Panel4)
    allbtn.Put("pnl"&5,Panel5)
 
End Sub
Sub Button_Click
    For Each key As String In allbtn.Keys
        Dim v As View = allbtn.Get(key)
        If v Is Label Then
            Dim lbl As Label = v
            lbl.Color = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
        else if v Is Panel Then
            Dim pnl As Panel = v
            pnl.Color = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
        else if v Is Button Then
            Dim btn As Button = v
            btn.Color = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
        End If
    Next
End Sub
 

Attachments

  • buttonslabelspanels.zip
    10.1 KB · Views: 171
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. A Button is a subclass of Label.
B4X:
If v Is Label
This will catch also buttons, checkboxes and EditTexts.

2. You can use B4XView from the XUI library to easily access almost all propeties.

B4X:
For x As B4XView in Activity.GetAllViewsRecursive
 If x Is Button Then x.Text = "I'm a button"
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. A Button is a subclass of Label
i had in mind that there was something about it but i could not remember the order needed (my brain).

So i should first check for button and then others...

Or, probably better, learn how to do it with B4XView. Looks like it is much easier with it than to cast the Object to a specific viewtype. :)
 
Upvote 0
Top