This snippet works for me but it's just a workaround. Are there any updates about measuring the needed width to show the complete text (single line)?
Note: I need to use TextFields in a TableView to let the user edit the data fast.
B4X:
Private Sub MeasureTextWidth (Text As String, Font As Font) As Float
Dim lbl As Label
lbl.Initialize("")
lbl.Text = Text
lbl.Font = Font
MainForm.RootPane.AddNode(lbl, 0, 0, -1, -1) 'mBase is a B4XView panel.
lbl.Snapshot
lbl.RemoveNodeFromParent
Return lbl.Width
End Sub
Not sure if this is accurate enough for you to use (it doesn't take into account font etc unless you set them)
B4X:
Dim txt As JavaObject
...
txt.InitializeNewInstance("javafx.scene.text.Text",Array(st)) ' st is the string to measure
Log(txt.RunMethodJO("getLayoutBounds",Null).runmethod("getWidth",Null))
Private Sub MeasureTexWidthNoWrap(Text As String,ThisFont As Font) As Double
Dim TB,Bounds As JavaObject
TB.InitializeStatic("javafx.scene.text.TextBuilder")
Bounds = TB.RunMethodJO("create",Null).RunMethodJO("text",Array(Text)).RunMethodJO("font",Array(ThisFont)).RunMethodJO("build",Null).RunMethodJO("getLayoutBounds",Null)
Return Bounds.RunMethod("getWidth",Null)
End Sub
Private Sub MeasureTexWidth(Text As String,ThisFont As Font) As Double
Dim TextObj As JavaObject
TextObj.InitializeNewInstance("javafx.scene.text.Text",Array(Text))
TextObj.RunMethod("setFont",Array(ThisFont))
TextObj.RunMethod("applyCss",Null)
Dim Bounds As JavaObject
Bounds = TextObj.RunMethodJO("getLayoutBounds",Null)
Return Bounds.RunMethod("getWidth",Null)
End Sub