Android Question Control array with designer?

techknight

Well-Known Member
Licensed User
Longtime User
Is it possible to create a label control array in the designer? I know it can be done in code but I am still not very good at placing positioning with code. Id rather use the designer.

If its not possible, can this feature be added please?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Currently it is not possible to directly create control arrays. However it is simple to mark the views you like in the designer and then convert them to an array. You even don't need to add a "dim" line for them.

Use the Tag property of each of the labels and set it to 1.

Now at runtime we build the array:
B4X:
Sub Globals
   Dim labels() As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Layout1")
   labels = CollectViews("1")
End Sub

Sub CollectViews(Tag As String) As Label()
   Dim lblsList As List
   lblsList.Initialize
   For Each v As View In Activity.GetAllViewsRecursive
     If v.Tag = Tag Then lblsList.Add(v)
   Next
   Dim lbls(lblsList.Size) As Label
   For i = 0 To lbls.Length - 1
     Dim lbl As Label = lblsList.Get(i)
     lbls(i) = lbl
   Next
   Return lbls
End Sub
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Well, I got the label indexing working, but its not loading the array in the correct order. it loads Label1, then label10, then label2, etc etc...

Edit: Nevermind, I fixed it by making the even labels a different tag.

Edit again: Oh this is hopeless. the order at which the items are found and loaded into the array varies. For example, I figured out panels, and I have it named pnlItem1 through pnlItem9 and it loads backwards! pnlItem9 goes into array (0)
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Who am I to argue with Erel's code, buy why can't you use something like this:
B4X:
  Dim Label1, Label2, label3,.....,Label10 As Label   'In Globals
Dim Labels() As Label                                'In Globals

Activity.LoadLayout("mylayout")
Labels=Array As Label(Label1,Label2,label3,...,Label10))  'this line must be after you load the layout
Labels(0).Text="Techknight"  'this corresponds to Label1
Labels(6).Text="B4A"         'this corresponds to Label7
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Now I got a new problem. When i switched it from Debug(Rapid) to Debug(legacy) becuase of program speed issues, I now get a bug:

its crashing at: If v.Tag = Tag Then lblsList.Add(v)

If v.Tag = Tag Then lblsList.Add(v)
java.lang.NullPointerException
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I had to change that line to this to fix it:

If v.Tag <> Null AND v.Tag = Tag Then lblsList.Add(v)

That fixed it. Why the old statement worked in debug mode rapid, I have no idea. but it doesnt work in release or debug mode legacy. So I had to check for a null in Tag for V.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Tons. labels, panels, buttons. etc...

Like i said, checking for null fixed the issue. If i dont check for null, the first thing passed is a null and crashes the application.
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
Currently it is not possible to directly create control arrays. However it is simple to mark the views you like in the designer and then convert them to an array. You even don't need to add a "dim" line for them.

Use the Tag property of each of the labels and set it to 1.

Now at runtime we build the array:
B4X:
Sub Globals
   Dim labels() As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Layout1")
   labels = CollectViews("1")
End Sub

Sub CollectViews(Tag As String) As Label()
   Dim lblsList As List
   lblsList.Initialize
   For Each v As View In Activity.GetAllViewsRecursive
     If v.Tag = Tag Then lblsList.Add(v)
   Next
   Dim lbls(lblsList.Size) As Label
   For i = 0 To lbls.Length - 1
     Dim lbl As Label = lblsList.Get(i)
     lbls(i) = lbl
   Next
   Return lbls
End Sub
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
This works great for me with labels. And I can make the individual labels visible or invisible with code like this for the second label in the array ----- Labels(1).Visible = True or Labels(1).Visible = False. However when I try the same thing with panels changing all the "Labels" to "Panels" in the code and all the "lbl" in the code to "pnl" I am unable to access individual panels as I am able to access individual labels. Specifically, the Panels(1) in Panels(1).Visible = True shows up in red and hovering over it I get the message "undeclared variable 'panels' is used before it is assigned any value". I don't get that problem with label views and code. Does this code not work with panels? Thanks!
 
Upvote 0
Top