Android Question [SOLVED] How to get relative top from a scrollview?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I use this codes to get relative top & left from this pos :

It worked OK, but on scrollview relative left & top doesn't works.

Attached is the simple project to demonstrate it.
On the project, there is a panel, when clicked, will log top position.

On ScrollView, top position is always the same even scrollview scrolled.

Can anybody help me how to fix it?
Thanks in advance.
 

Attachments

  • Test.zip
    11.1 KB · Views: 61

incendio

Well-Known Member
Licensed User
Longtime User
I don't know if this is the proper codes.
Change codes to this
B4X:
Sub GetRelativeTop(V As JavaObject) As Int
    Private offset=0 As Int
    If GetType(V) = "android.view.ViewRoot" Or GetType(V) = "android.view.ViewRootImpl" Then
        Return 0
    Else
        If V.RunMethod("getParent",Null) Is ScrollView Then
            Private s As ScrollView = V.RunMethod("getParent",Null)
            offset = s.ScrollPosition
            LogColor(offset,Colors.Yellow)
        End If
        
        Try
            Dim VW As View = V
            Return VW.Top + GetRelativeTop(V.RunMethod("getParent",Null)) - offset
        Catch
            Return GetRelativeTop(V.RunMethod("getParent",Null)) -  offset
        End Try
    End If
End Sub

Seem work fine.
 
Last edited:
Upvote 0
Top