Android Question [Solved]How to addview by coding in B4XPages?

Theera

Expert
Licensed User
Longtime User
I always create view from layout designer. Today I have to use B4XPages Coding. I wonder that how to addview by coding in B4XPage directly.
Code:
Sub Activity_Create(FirstTime As Boolean)
 
    Private lbl1 As  Label
     
    lbl1.Initialize("lbl1")
    Activity.AddView(lbl1,100dip,100dip,100dip,100dip)
 
Last edited:
Solution
Like this:

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
   Root = Root1
  
   Private lbl1 As  Label
   lbl1.Initialize("lbl1")
   Root.AddView(lbl1,100dip,100dip,100dip,100dip)

LucaMs

Expert
Licensed User
Longtime User
The question isn't entirely clear to me, but I'll try to answer anyway.

Creating Views exclusively in Layouts (i.e., using the Designer) is a great practice. Then, in the B4XPage source code, you simply need to declare the variables (and the Designer itself can do this for you), load the Layout (mostly in Root), and that's it.

If, however, you want to create some Views in the project from code, you should know that you can do this with "native" Views, such as Button, Label, EditText, etc., but you can't do it with Custom Views; you'll always have to insert them into a Layout.

Finally, if your question was only about "Activity.AddView", simply write "Root.AddView".
 
Upvote 0
Top