Android Question B4XSwitch in runtime - is it possible

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

I trying to create a B4XSwitch in runtime using the code:
B4X:
    Dim itemEnabled As B4XSwitch
    itemEnabled.Initialize(Me,"itemEnabled")
    itemEnabled.mBase.AddView(pnlEditItem,10,panelPos,txtPrice.Height * 4,txtPrice.Height)

But this is generating the error:

java.lang.RuntimeException: Object should first be initialized (B4XView).

When I add the b4xswitch using the designer there is no error.
What am I doing wrong? Is it impossible to add B4Xviews at runtime ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is it impossible to add B4Xviews at runtime ?
You are confusing different things.
B4XSwitch is not a B4XView. B4XSwitch is a custom view.
The correct question is whether it is possible to add custom views at runtime. The answer is that you can create a layout file with the custom view and load it whenever you want to add one. You can use the same layout file to create any number of views.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
The answer is that you can create a layout
And maybe the direct answer is "no, you MUST to create a layout with the custom view before." - but why? Why can't I use a custom view like I use a traditional view, defining it in command line?
create a layout file with the custom view and load it whenever you want to add one
thanks @Erel . I'll use this technique .
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
And maybe the direct answer is "no, you MUST to create a layout with the custom view before."
Not exactly true. You can dynamically create custom views using a layout file.

Why? Because it makes it easier for developers to build custom views and it is very simple for other developers to use those custom views.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…