Android Question Add CustomView by code Multiple times

Sagenut

Expert
Licensed User
Longtime User
This question refer to this discussion:
https://www.b4x.com/android/forum/t...-creation-with-code-not-with-designer.117247/

In the linked discussion has been given the solution how to add a CustomView by code:
making a dedicated layout with the CustomView and loading the layout on a panel.
But the CustomView on the layout will have it's own name.
Loading the same layout 2 times, in the need to add 2 times (or more) the same CustomView, will create the problem that the working CustomView will always be the last added one.
There is a way to solve this?

Looking in the Klaus's Booklet about CustomViews we found that adding a small Sub into the CustomView would make it possible to add it by code without the trick of the layout.
Here it is
B4X:
Public Sub AddToParent(Parent As Activity, Left As Int, Top As Int, Width As Int, Height As Int)
   mBase.Initialize("mBase")
   Parent.AddView(mBase, Left, Top, Width, Height)
End Sub
And the call from code (for B4A, B4i)
B4X:
Private clsTest2 As ClsCustomView
clsTest2.Initialize(Me, "clsTest2")
clsTest2.AddToParent(MyPanel, 10dip, 10dip, 200dip, 50dip)
Adding that Sub to CustomView (CustomListView in this case) would make it possible to add it in that way?
Or did I misunderstood something?
In the case can we get it on next CustomListView update?
Should this thing be taken into consideration by CustomView Developers?
 

DonManfred

Expert
Licensed User
Longtime User
Customviews must be loaded with a Layout!

Create a Layout just with only the clv.
Load the layout to a panel whenever you need a CLV.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Customviews must be loaded with a Layout!

Create a Layout just with only the clv.
Load the layout to a panel whenever you need a CLV.
Of course this method works, but give you possibility to add it only once.
Because of the same name only the last added layout containing the CustomView will work.
But I am sure I does not need to teach this to anyone. :)
The method found in Klaus's Booklets seems to give possibility to create new CustomViews instances with different names.
I would like to know if this method is actual and realizable or if it's out of date or not implementable for some reason.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
You can add the xCustomListView per code with this:
B4X:
Dim tmplbl As Label
    tmplbl.Initialize("")
    Dim tmpmap As Map
    tmpmap.Initialize
    tmpmap.Put("DividerColor",0x00FFFFFF)
    tmpmap.Put("DividerHeight",0)
    tmpmap.Put("PressedColor",0x00FFFFFF)
    tmpmap.Put("InsertAnimationDuration",0)
    tmpmap.Put("ListOrientation","Horizontal")
    tmpmap.Put("ShowScrollBar",False)
    
    xclv_main.Initialize(Me,"xclv_main")
    xclv_main.DesignerCreateView(mBase,tmplbl,tmpmap)
I'm using it in all my AS Views where i need the list. The disadvantage is, if Erel would add another designer property, then i have to adapt the code around this property.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I took into consideration XCLV just as an example.
But the discussion is about having the chance to add by code any CustomView.
Your method looks to be a first solution.
Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I am sure I does not need to teach this to anyone. :)
The method found in Klaus's Booklets seems to give possibility to create new CustomViews instances with different names.
This is also trivial to solve and it is a big mistake to use all kinds of hacks because of this.

Two options:
B4X:
Pnl.LoadLayout("CustomListView")
CLV123 = CustomListView1

In the case of newer custom views, such as all XUI Views, there is a convention that the custom view is set to the base panel tag:
B4X:
Pnl.LoadLayout("B4XSwitch")
Dim s As B4XSwitch = Pnl.GetView(0).Tag 'assuming that Pnl was defined as B4XView
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I think (I hope I don't sound presumptuous) that what @Sagenut is asking is simple:

Why do not add to all custom views the AddToParent routine so that you can always chose to create them by Designer, by code or both.
Is there a reason why it is necessary to develop a custom view that can only be added by Designer?

Right now I don't remember (also, I'm watching a very stupid movie 😄) why it wasn't done, for example, in the xCustomListView. I will search the reason or... I will try to modify the xCustomListView by adding the AddToParent method to it.
 
Last edited:
Upvote 0
Top