Android Code Snippet [B4X] MeasureMultilineTextHeight

I was looking for a way to determine the height of a textview when it has multiline text. This function now makes that possible across platforms.
From B4J I come from this thread:
Private Sub MeasureMultilineTextHeight(xLabel As B4XView) As Double
    #If B4J
    Dim jo As JavaObject = Me
    Return jo.RunMethod("MeasureMultilineTextHeight", Array(xLabel.Font, xLabel.Text, xLabel.Width))
    #Else if B4A
    Dim su As StringUtils
    Return su.MeasureMultilineTextHeight(xLabel,xLabel.Text)
    #Else if B4I
    Dim tmpLabel As Label
    tmpLabel.Font = xLabel.Font
    tmpLabel.Width = xLabel.Width
    tmpLabel.Text = xLabel.Text
    tmpLabel.Multiline = True
    Return tmpLabel.Height
    #End IF
End Sub

#If B4J
#if Java
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.text.Font;
import javafx.scene.text.TextBoundsType;
public static double MeasureMultilineTextHeight(Font f, String text, double width) throws Exception {
  Method m = Class.forName("").getDeclaredMethod("computeTextHeight",
  Font.class, String.class, double.class, TextBoundsType.class);
  return (Double)m.invoke(null, f, text, width, TextBoundsType.LOGICAL_VERTICAL_CENTER);
#End If
#End If
Last edited: