I'm trying to get the absolute coordinates of a view (that is, relative to the activity, not the view's parent object).
There's a Java method called getLocationOnScreen that does this, but I'm not sure how to call it from B4A. Mainly I'm having trouble with the parameter it wants (an array of 2 integers).
I don't know that Java method (someone will answer to you)
but you could use getParent (recursively, if necessary) and calculate the absolute position.
B4X:
Sub GetParent(v As View) As Object
Dim jobj = v As JavaObject
Return jobj.RunMethod("getParent", Null)
End Sub
int leftTop[] = new int[2];
aView.getLocationOnScreen(leftTop);
My attempt to do this in B4A is:
B4X:
Dim leftTop(2) As Int
Dim JO As JavaObject = aView
JO.RunMethod("getLocationOnScreen", Array As Object(leftTop))
viewLeft = leftTop(0)
viewTop = leftTop(1)
...but no matter which view I feed it (aView), it always returns 0, 33.
Hi, Dave!
Maybe you've already found the solution.
I think you called getLocationOnScreen from Activity_Create, when the screen isn't painted yet.
Try to call it later by using a timer, or a button as LucaMs did.
I think you called getLocationOnScreen from Activity_Create, when the screen isn't painted yet.
Try to call it later by using a timer, or a button as LucaMs did.
Actually, I was calling it from activity_resume, but I think you're right about the screen not being painted fully, because when I used a CallSubDelayed there, it worked properly.