'shows a toast message just above the view (or below if there's no room above)
Sub showTooltip(viewArg As View, text As String)
Dim const verticalOffset As Int = 60dip
Dim position(2) As Int
position = getScreenPosition(viewArg)
Dim toastTop As Int = position(1) - verticalOffset 'normally show hint above the view
If toastTop < 0 Then 'no room at top, so move toast below the view
toastTop = position(1) + verticalOffset
End If
showToastAt(position(0), toastTop, text, False)
End Sub
'Show standard toast at given x/y position relative to activity.
'Toast will always appear within the screen boundaries, even if x or y is off-screen.
Sub showToastAt(x As Int, y As Int, text As String, longDuration As Boolean)
Dim duration As Int
If longDuration = True Then
duration = 1
Else
duration = 0
End If
Dim r As Reflector
r.Target = r.GetActivity
Dim toastJO As JavaObject
toastJO.InitializeStatic("android.widget.Toast")
Dim toastJO2 As JavaObject
toastJO2 = toastJO.RunMethod("makeText", Array As Object(r.GetContext, text, duration))
toastJO2.RunMethod("setGravity", Array As Object (Bit.Or(Gravity.TOP, Gravity.LEFT), x, y))
toastJO2.RunMethod("show", Null)
End Sub
'Calculate the position of the view relative to the activity (not to the view's parent).
'Returns an integer array where index 0 is left, index 1 is top
Sub getScreenPosition(viewArg As View) As Int()
Dim position(2), parentPosition(2) As Int
If viewArg.parent Is ScrollView Then
'the scrollview's panel has no layout, so ignore
Dim sv As ScrollView = viewArg.Parent
position(0) = sv.left
position(1) = sv.Top - sv.ScrollPosition
Return position
else If viewArg.parent Is AHViewPager Then 'you can comment out this part if you don't use AHViewPager
'the pager's container has no layout, so ignore
Dim pager As AHViewPager = viewArg.Parent
position(0) = pager.left
position(1) = pager.Top
Return position
else if viewArg.parent = getCurrentActivity Then
position(0) = viewArg.Left
position(1) = viewArg.Top
Return position
Else
parentPosition = getScreenPosition(viewArg.parent)
position(0) = viewArg.Left + parentPosition(0)
position(1) = viewArg.top + parentPosition(1)
Return position
End If
End Sub
Sub getCurrentActivity As Activity
Dim r As Reflector
r.Target = r.GetActivityBA
Return r.GetField("vg")
End Sub