How can I create multiple copies with minimum processing?

GuyBooth

Active Member
Licensed User
Longtime User
This sounds like a very basic question - but I don't know how to do it!

I want to use a label many many times.
Every time I use it it will have the same charactersitics (color, size, text size etc) but for each copy I will want to add different text.
How do I do this without having to recreate all the properties every time? Somehow set the defaults to be the ones I want? I would like to initialize it just once, and then reuse it ...
 

Informatix

Expert
Licensed User
Longtime User
You cannot create a model and replicate it. But you can ease your work this way:
B4X:
Sub Label_Initialize(MyLabel As Label, Text As String)
   MyLabel.Initialize("")
   MyLabel.Gravity = Gravity.CENTER_VERTICAL + Gravity.LEFT
   MyLabel.TextColor = Colors.Black
   MyLabel.TextSize = 18
   MyLabel.Typeface = Typeface.DEFAULT
   MyLabel.Text = Text
End Sub
You call the function for each label. Example:
B4X:
Dim Label1 As Label
Label_Initialize(Label1, "text1")
MyPanel.AddView(Label1, 0, 0, 100dip, 20dip)
Dim Label2 As Label
Label_Initialize(Label2, "text2")
MyPanel.AddView(Label2, 0, 30dip, 100dip, 20dip)
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You can dim an array of labels and set their common properties using a loop.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is my code which I think is in line with what mc73 is suggesting. Informatix's (Fred) recommendations are always top notch too. Suit yourself.
B4X:
'BELOW DISPLAYS A SERIES OF LABELS THAT SHARE COMMON PROPERTIES, BUT HAVE DIFFERENT TEXT
Dim MyLabelText(5) As String
Dim ArrayOfLabels(5) As Label
Dim i As Int

MyLabelText = Array As String ("Klaus","NJDude","Informatix","mc73","Erel")
For Each MyLabel As Label In ArrayOfLabels
            MyLabel.Initialize("")
            MyLabel.Gravity = Gravity.CENTER_VERTICAL + Gravity.LEFT
            MyLabel.TextColor = Colors.Cyan
            MyLabel.TextSize = 18
            MyLabel.Typeface = Typeface.DEFAULT
            MyLabel.Text=MyLabelText(i)
            Activity.AddView(MyLabel,0,i*45dip,50%x,50dip)
            i=i+1
Next
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Thanks for all your very good suggestions...they all help with organizing (as would creating a class).
I'm looking specifically for performance improvement (my apologies - I should have stated that up front). Since all of these methods still requires a complete loop through the settings for every label created, am I right in thinking I wouldn't gain performance-wise?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You will not lose any performance by going through the loop. I just created 100 labels using the code I gave you in my previous post. It took 0.29 second to create them and display them all on the screen, which is insignificant.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
As Mahares said, performance is not much affected by this way of doing things. When you create a view, most of the time is devoted to creating an instance of the view and adding it to a container, not filling its properties, so whatever solution you may choose, the performance gain will be marginal.
I don't recommend using a loop with an array (except if you have a lot of views to create) because it's easier to maintain a program with explicit variable names than a code full of label(7), panel(5), imgvw(8), etc.
 
Upvote 0
Top