Android Question [SOLVED] Access views from layout in Custom View

cutstone

Member
Licensed User
Longtime User
I have a question about Custom View and how to access views from a layout created in Designer. I know this question might be dumb, but I have searched and read the documentation, and cannot figured it out.

My issue is that, from DesignerCreateView, I can access the view Label1 but later in setGo Label1 is Null, and I get an error when assign new value to it's attributes.

What am I missing? Shouldn't it be possible to access views from pnlGlobal?


SharedView:
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mBase As B4XView
    Private xui As XUI 'ignore
    Public Tag As Object
    Private Label1 As B4XView
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
End Sub

'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base

    Sleep(0)
    mBase.LoadLayout("pnlGlobal")
    Label1.Text = "Start"
End Sub

Private Sub Base_Resize (Width As Double, Height As Double)
 
End Sub

Public Sub setGo(newText As String)
    Label1.Text = newText
End Sub


B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private sh As SharedView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    sh.Initialize(Root1, "SharedView")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
    sh.Go = "Off we go!"
End Sub
 

Attachments

  • log.png
    log.png
    85.1 KB · Views: 134
  • pnlGlobal.png
    pnlGlobal.png
    110.8 KB · Views: 136

udg

Expert
Licensed User
Longtime User
I would say that if Go isn't a property which you want to reach by setter and getter (setGo, getGo), then when calling from MainPage a sub named setGo you should call it as sh.setGo(<your new text>)

Edit: on a second tought (but still before my coffee), try moving sh.Initialize(Root1, "SharedView") in the Initialize of MainPage
 
Last edited:
Upvote 0

cutstone

Member
Licensed User
Longtime User
Thank you for all the quick response. I have attached the project here, which is a very small project that show the issue.

udg: I tried moving sh.Initialize(Root1, "SharedView") to Initialize and changed it to sh.Initialize(Me, "SharedView"), but this didn't make any difference to the problem.

klaus: Yes, I added sh as SharedView in MainPage. I then tried your advise of removing the sh.Initialize(Root1, "SharedView"). But the IDE gave a warning: "Variable 'sh' was not initialized. (warning #11)"

What do you mean by add sh as a SharedView CustomView ? I use the statement: Private sh As SharedView
 

Attachments

  • TestCustomView.zip
    11.6 KB · Views: 99
Upvote 0

cutstone

Member
Licensed User
Longtime User
Thank you so much, klaus. That was exactly the problem. I completely missed that relation. Now that everything is clear, I realize you actually pointed it out in your first answer.
 
Upvote 0
Top