Android Question [SOLVED] B4XPages Error, Get Relative Position on a View

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have a codes from this forum to get a relative position on any views.

With Default Project, this codes worked OK, but not with Project based on B4XPages.

The error occured on B4x Main Page, with these codes
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Log(GetRelativeLeft(Panel1)) 'this caused the error'
End Sub

Public Sub GetRelativeLeft(V As JavaObject) As Int
    If GetType(V) = "android.view.ViewRoot" Or GetType(V) = "android.view.ViewRootImpl" Then
        Return 0
    Else
        Try
            Dim VW As View = V
            Return VW.Left + GetRelativeLeft(V.RunMethod("getParent",Null))
        Catch
            Return GetRelativeLeft(V.RunMethod("getParent",Null))
        End Try
    End If
End Sub

If that codes change to this
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Sub Button1_Click
    Log(GetRelativeLeft(Panel1))
    xui.MsgboxAsync(GetRelativeLeft(Panel1), "B4X")
End Sub

When users click Button1, it worked fine.
Any hint how to solve this?

Attached is the sample project with that error.
 

Attachments

  • Page.zip
    10.2 KB · Views: 118

incendio

Well-Known Member
Licensed User
Longtime User
That code is a striped code from a complete project.

I have a custom view that created after layout loaded.
This custom view have a hiden panel that need its relative position to its parent.

I change the codes to this
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    sleep(50)
    Log(GetRelativeLeft(Panel1)) 'this caused the error'
End Sub

It seem solved the problem.
I guest, in Project based on B4XPage there is no 'wait for' to suspend execution until layout file loaded completely?
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
You want to find the left position relative to the root, right?
On Activity based project, I want to find left position relative to the Activity.

Don't use this code.

Use:
B4X:
Sub CalcLeftRelativeToRoot (v As B4XView)
Dim res As Int
Do While v <> Root
  res = res + v.Left
  v = v.Parent
Loop
Return res
End Sub

Now I try project based on B4xPages, not sure which one to use as an achor point, Root or Activity.

So temporary, changed the codes to this
B4X:
Sub CalcLeftRelativeToAct (v As B4XView) As Int
    Dim res As Int
    Do While v <> MainAct 'MainAct is the activity in main module'
        res = res + v.Left
        v = v.Parent
    Loop
    Return res
End Sub

Those codes, worked too, but still need to use this line of code in Private Sub B4XPage_Created (Root1 As B4XView)
B4X:
Sleep(50)

Otherwise, error still raised
java.lang.RuntimeException: Object should first be initialized (B4XView).
 
Upvote 0
Top