A few days later, i'm faced with another issue:
I'm having troubles understangin
when the text is autosized. From my tests it seems it's being autosized only when the object is in a layout and it's visible.
The old iterative method did not care about the object being visibile, at all, it just had to be initialized.
What i'm trying to do: I'm trying to make B4X Dialogs of the appropriate size.
I set a default width/height for a label, text is applied, text size is changed so it fits the label, the label (and underlying panel) size is adjusted and only then the dialog is shown.
This is how i resize the label
Private Sub AdjustLblSize(lbl As Label)
Dim w As Int
Dim h As Int
Dim su As StringUtils
Dim cvs As B4XCanvas
Dim lines() As String = Regex.Split(CRLF, lbl.Text)
Dim fnt As B4XFont = xui.CreateFont(lbl.Typeface, lbl.TextSize)
h = su.MeasureMultilineTextHeight(lbl, lbl.Text) + lbl.Padding(1) + lbl.Padding(3)
cvs.Initialize(lbl)
w = 0
For idx = 0 To lines.Length - 1
Dim wLine As Int = cvs.MeasureText(lines(idx), fnt).Width + lbl.Padding(0) + lbl.Padding(2) + 5dip
If w < wLine Then w = wLine
Next
cvs.Release
lbl.Width = w
lbl.Height = h
End Sub
And works beautifully for every label in an active layout (though sometimes i still need a Sleep(0) between adjusting text size with code above and adjusting label size)
The problem i'm getting now is that it seems that the text size changes only after the B4XDialog.show method
** I tried already making the panel not visible, adding it to the B4XDialog Parent, which in this case is the activity itself, doesn't change the text size even if sleeping for 10 seconds.
** Another cause could be that there is no layout yet, This particular dialog i'm looking at happens before everything. It happens right after Activity Create, before i load any other layout and resource (the dialog contains a disclaimer before asking for permissions. Application data is loaded after permissions are granted/checked)