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 ?
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.
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?