' Call it from the xTable DataUpdated event.
' lstColumns: pass Null to resize all columns.
Sub B4XTableColumnResizesOnContent(xTable As B4XTable, lstColumns As List)
If Not(lstColumns.IsInitialized) Then
lstColumns = xTable.Columns
End If
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, 1dip, 1dip)
Dim xCanvas As B4XCanvas
xCanvas.Initialize(p)
Dim ShouldRefresh As Boolean
For Each column As B4XTableColumn In lstColumns
Dim MaxWidth As Int
For i = 0 To xTable.VisibleRowIds.Size - 1
Dim RowId As Long = xTable.VisibleRowIds.Get(i)
If RowId > 0 Then
Dim pnl As B4XView = column.CellsLayouts.Get(i + 1)
Dim lbl As B4XView = pnl.GetView(0)
Dim txt As String = xTable.GetRow(RowId).Get(column.Id)
MaxWidth = Max(MaxWidth, xCanvas.MeasureText(txt, lbl.Font).Width + 10dip)
End If
Next
If MaxWidth > column.ComputedWidth Or MaxWidth < column.ComputedWidth - 20dip Then
column.Width = MaxWidth
ShouldRefresh = True
End If
Next
If ShouldRefresh Then
xTable.Refresh
End If
End Sub