Dim myNode As Node = Sender
Dim Jo1 As JavaObject
Jo1 = myNode
Dim BoundsInLocal As Object = Jo1.RunMethod("getBoundsInLocal", Null)
Dim LocalToScene As Object = Jo1.RunMethod("localToScene", Array As Object(BoundsInLocal))
Jo1 = LocalToScene
Dim objWidth As Object = Jo1.GetField("getWidth") ' <--- nice error here ^_^
Sub ResizeToFit(n As JavaObject)
Dim text As JavaObject
text.InitializeNewInstance("javafx.scene.text.Text",Array(n.RunMethod("getText",Null)))
text.RunMethod("setFont",Array(n.RunMethod("getFont",Null)))
Dim bounds As JavaObject = text.RunMethod("getBoundsInLocal",Null)
n.RunMethod("setPrefWidth",Array(bounds.RunMethod("getWidth",Null)))
n.RunMethod("setPrefHeight",Array(bounds.RunMethod("getHeight",Null)))
End Sub
Sub ResizeToFit(n As JavaObject)
Dim text As JavaObject
text.InitializeNewInstance("javafx.scene.text.Text",Array(n.RunMethod("getText",Null)))
text.RunMethod("setFont",Array(n.RunMethod("getFont",Null)))
Dim bounds As JavaObject = text.RunMethod("getBoundsInLocal",Null)
n.RunMethod("setPrefWidth",Array(bounds.RunMethod("getWidth",Null)))
n.RunMethod("setPrefHeight",Array(bounds.RunMethod("getHeight",Null)))
End Sub
where MinX e MinY SEEM to be the absolute position of the node, but in my project it is placed in 0,0.
May be because it is a Button?
B4X:
Sub ButtonX_Click
Dim Jo1 As JavaObject = Sender
Dim objBoundsInLocal As Object = Jo1.RunMethod("getBoundsInLocal", Null)
Dim joLocalToScene As JavaObject = Jo1.RunMethod("localToScene", Array As Object(objBoundsInLocal))
Dim objMinX As Object = joLocalToScene.RunMethod("getMinX", Null)
Dim objMinY As Object = joLocalToScene.RunMethod("getMinY", Null)
Dim objMaxX As Object = joLocalToScene.RunMethod("getMaxX", Null)
Dim objMaxY As Object = joLocalToScene.RunMethod("getMaxY", Null)
Dim objWidth As Object = joLocalToScene.RunMethod("getWidth", Null)
Dim objHeight As Object = joLocalToScene.RunMethod("getHeight", Null)
Log("MinX:" & objMinX)
Log("MinY:" & objMinY)
Log("MaxX:" & objMaxX)
Log("MaxY:" & objMaxY)
Log("Width:" & objWidth)
Log("Height:" & objHeight)
End Sub
Dim Jo1 As JavaObject = Sender
Dim objBoundsInScreen As Object = Jo1.RunMethod("getBoundsInLocal", Null)
Dim joLocalToScreen As JavaObject = Jo1.RunMethod("localToScreen", Array As Object(objBoundsInScreen))
Dim objMinX As Object = joLocalToScreen.RunMethod("getMinX", Null)
Dim objMinY As Object = joLocalToScreen.RunMethod("getMinY", Null)
Dim objMaxX As Object = joLocalToScreen.RunMethod("getMaxX", Null)
Dim objMaxY As Object = joLocalToScreen.RunMethod("getMaxY", Null)
Dim objWidth As Object = joLocalToScreen.RunMethod("getWidth", Null)
Dim objHeight As Object = joLocalToScreen.RunMethod("getHeight", Null)
objMinX & objMinY are the "Sender" position on screen (so, the Android getLocationOnScreen equivalent)
Sub ViewScreenPosition (view As B4XView) As Int()
Dim x, y As Int
Dim parent As B4XView = view
Do While parent.IsInitialized
x = x + parent.Left
y = y + parent.Top
parent = parent.Parent
Loop
Return Array As Int(x, y)
End Sub
This code will work in all three platforms. You need to properly test it if you are using "special containers".
I will post a "code module / library" with few methods, related to B4XView positions.
Only one of these methods is "problematic", just "location on screen". I will use the 2 specific codes for b4a & b4j, your function, Erel, for b4i, inside a nice Try-Catch block.
What do you think about this?
[It will mean that if some b4x member will not like my module, he will not pay for it (since nobody will pay for it, it will be free )]
Sub Process_Globals
Type tLocation(Left As Int, Top As Int)
' ...
B4X:
#If B4I
Public Sub GetLocation(Vw As B4XView) As tLocation
Dim Location As tLocation
Location.Initialize
Dim x, y As Int
Dim parent As B4XView = view
Try
Do While parent.IsInitialized
x = x + parent.Left
y = y + parent.Top
parent = parent.Parent
Loop
Location.Left = x
Location.Top = y
Catch
Log(LastException)
End Try
Return Location
End Sub
#End If