Android Tutorial [B4X] How to get <custom view here> from <CLV or any other container>

This is a common question and for a good reason.

Custom views classes are not views by themselves.
The views tree only holds views.
This means that this code cannot work:
B4X:
Dim B4XFloatTextField1 As B4XFloatTextField = CLV.GetPanel(x).GetView(y)
The actual view that is added to the views tree, with most custom views, is the "base" panel, usually named mBase.

To solve this problem the following convention is used in all XUI Views and is recommended for all new custom views:
The class instance is set to the base panel Tag property.
This allows developers to get the class instance using the base panel Tag property:
B4X:
'Will work
Dim B4XFloatTextField1 As B4XFloatTextField = CLV.GetPanel(x).GetView(y).Tag
Another convention is to add a Tag property to the class and set it like this:
B4X:
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mBase As B4XView 'ignore
    Private xui As XUI 'ignore
    Public Tag As Object '<---
End Sub

Public Sub DesignerCreateView (Base As Object, lbl As Label, Props As Map)
    mBase = Base
    Tag = mBase.Tag '<-- store the designer set tag value
    mBase.Tag = Me '<-- set the class reference discussed above
Developers can use the class Tag field to store their additional data.

As these conventions are rather new, they are not used by some popular custom views such as xCLV. You can set the base panel tag yourself in such cases. For example:
B4X:
Activity.LoadLayout("1")
clv.AsView.Tag = clv
'now we can get the clv with:
Dim c As CustomListView = Activity.GetView(<clv index>).Tag
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Very useful.

Just my usual... disturb (can you live without it? šŸ˜„)


It would be useful if these things were in the template for the new custom views.
(I take this opportunity to renew my request for a snippets manager in the IDE šŸ˜‡)

For the moment I will use the method to add custom class templates by IDE (B4A - Sorry, I was unable to find the related thread and therefore could not write the link)


P.S. found; I searched for a tutorial but it isn't:
https://www.b4x.com/android/forum/threads/custom-class-templates-in-b4a.90552/post-572445
 
Last edited:
Top