1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Custom View in Custom View?

Discussion in 'Android Questions' started by agraham, Jul 10, 2019.

  1. agraham

    agraham Expert Licensed 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.

    Code:
    ' 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: Jul 11, 2019
  2. klaus

    klaus Expert Licensed User

    Sorry, but I don't understand what exactly you want to load with LoadLayout?
    I don't see any LoadLayout in your code.
     
    Last edited: Jul 11, 2019
  3. agraham

    agraham Expert Licensed User

    Oops! You are right. I copied and simplified the code to better show what was going on but accidentally omitted that line - Rectified!
     
  4. agraham

    agraham Expert Licensed User

    Ok - forget this, something weird is happening and it is probably me :( I can get a ScrollView2D on my Custom View but I can't set it up without getting errors. Needs more work!
     
  5. agraham

    agraham Expert Licensed User

    More or less working now. :) A classic combination of ignorance, stupidity and a non-obvious bug combining to obscure what was really happening!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Many custom views are made of other custom views.
    Do note that LoadLayout calls cannot be nested. This code will fail:
    Code:
    Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
     Base.LoadLayout(...)
    End Sub
    The solution is to add Sleep(0) before:
    Code:
    Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
     
    'do many things here
     Sleep(0)
     Base.LoadLayout(...)
    End Sub
     
    klaus likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice