Add View (control) at Runtime

TedN

Member
Licensed User
Longtime User
Is there a tutorial or example of how to add a label (or any view) at runtime.

AddView seems pretty straightforward but I can't seem to get it to work.

B4X:
Sub EditMode()
     Dim lblControls As Label

     AddView(lblControls, 10, 20, 100, 200)
End Sub

I'm getting error: Undeclared variable 'addview' is used before it was assigned any value.
 

TedN

Member
Licensed User
Longtime User
Thanks for reply. I get no errors now but nothing appears on the emulator.

My revised code is as follows:

B4X:
Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("Main")
   EditMode
   
End Sub

Sub EditMode()
   
   Dim lblControls As Label
   lblControls.Initialize("controls")
   lblControls.Color = 255
   lblControls.Text = "TEST"
   lblControls.TextColor = 0
   lblControls.BringToFront
   Activity.AddView(lblControls, 10, 20, 100, 200)
   
End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
That is because you have a transparent label on a black background with black text.

Try modifying the line to
B4X:
lblControls.TextColor = Colors.Cyan
and you should see the text.
 
Last edited:
Upvote 0
Top