B4J Question getLocationOnScreen equivalent

LucaMs

Expert
Licensed User
Longtime User
B4A:
B4X:
Dim leftTop(2) As Int
Dim JO As JavaObject = SomeView
JO.RunMethod("getLocationOnScreen", Array As Object(leftTop))


B4J
?

I found:
Bounds boundsInScene = node.localToScene(node.getBoundsInLocal());

but I'm not able to use it (Javaobject is enough? I think that I will need inline java code, because of the Bounds type variable)




[there is also:
Bounds boundsInScreen = node.localToScreen(node.getBoundsInLocal());

but I think that the first one is more similar to the Android function.
]
 

LucaMs

Expert
Licensed User
Longtime User
My first steps:
B4X:
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   ^_^
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This is how I use bounds
B4X:
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
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User





... thinking... but before... smoking
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
thought

Thank you, @Daestrum, I now understand how to get those values.

The problem now is that I get:

MinX:-1.399999976158142
MinY:-1.399999976158142
MaxX:101.4000015258789
MaxY:41.400001525878906
Width:102.80000150203705
Height:42.80000150203705

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
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could try
B4X:
log("X : "&Jo1.RunMethod("getLayoutX",null)&", Y : "&Jo1.RunMethod("getLayoutY",null))
…
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You could try
B4X:
log("X : "&Jo1.RunMethod("getLayoutX",null)&", Y : "&Jo1.RunMethod("getLayoutY",null))
…
Unfortunately, those methods return the position related to the parent.


Changed the code in #5:
B4X:
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)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
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".
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This code will work in all three platforms
For b4a, this works too:
B4X:
Dim leftTop(2) As Int
Dim JO As JavaObject = SomeView
JO.RunMethod("getLocationOnScreen", Array As Object(leftTop))

and for b4j what I wrote in my previous post.

B4i... I don't know

The advantage would be that you don't need...
You need to properly test it if you are using "special containers".
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Idea (bad idea, as always ).

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 )]
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
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

It's ok so?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…