Android Question Custom View in Custom View?

agraham

Expert
Licensed User
Longtime User
Is it possible to have a Custom view located on another Custom View?

I am trying to use ScrollView2D on a Custom View and get it loaded by LoadLayout, and am getting a null reference error setting the ScrollView2D width in the ScrollView2D DesignerCreateView method.

Things work fine if I do it all programmatically, Initialize the Custom View, add it to the Activity then call InitTable. I just can't get it to work from a layout.

Am I doing things in the wrong order? I don't know the order in which things are called when LoadLayout loads a Custom View in a Custom View.

B4X:
 ' Table2D Custom View Module
Private Sub Class_Globals
    Private SV As ScrollView2D
    Private TablePanel As Panel
    '...
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    TablePanel = Base
     '...
     InitTable
End Sub

Public Sub Initialize (CallbackModule As Object, EventName As String)
    Callback = CallbackModule
    Event = EventName
End Sub

Public Sub InitTable
   SV.Initialize(TablePanel.Width, TablePanel.Height - RowHeight, "SV")
   TablePanel.AddView(SV, 0, RowHeight , TablePanel.Width, TablePanel.Height - RowHeight)
End Sub

' Used by the Main Module
Sub Globals
   Dim Table1 As Table2D
   '...
End Sub

Sub Activity_Create(FirstTime As Boolean)
  LoadLayout("Layout1")
   Table1.ResizeTable(5)
   For i = 1 To 500   
       Table1.AddRow(...)
   Next
   '...
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Many custom views are made of other custom views.
Do note that LoadLayout calls cannot be nested. This code will fail:
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
 Base.LoadLayout(...)
End Sub

The solution is to add Sleep(0) before:
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
 'do many things here
 Sleep(0)
 Base.LoadLayout(...)
End Sub
 
Upvote 0
Top