Can anyone see what I'm doing wrong in the following code, based on an example by Klaus? I'm trying to ensure that the Label view EntryBox is always large enough for the text it contains, which can vary from a few hundred characters to more than 3000. The code below seems to work fine for short texts but not for ones over about 1500 characters. I can't get EntryBox to expand sufficiently, even specifying a stupidly large size by manually adding units. (My commented out code starting "If EntryHeight > 1500") Am I confusing the size measures? I'm often not sure in B4A what the default unit of measurement is, or what suffix I should add to a number when specifying a size for a view.
Any help would be gratefully accepted.
Any help would be gratefully accepted.
B4X:
Sub ShowTableInFullView(OurQry As String)
Dim i As Int, EntryHeight As Int, PanelHeight As Int, PanelTop As Int, Hits As Int, HitStr As String, Place As String
G.SQL1.Initialize(G.DBFileDir, G.DBFileName, True)
Try
G.SQL1.BeginTransaction
G.ListTable = DBUtils.ExecuteMemoryTable(G.SQL1, OurQry, Null, 0)
PanelTop = 0
PanelHeight = 110%y ' Must be greater than the screen size to scroll?
Hits = G.ListTable.Size
If Hits = 1 Then
HitStr = Hits & " record found"
Else
HitStr = Hits & " records found"
End If
HitBox.Text = HitStr
If Hits = 0 Then
Msgbox("No results for query " & OurQry, G.AppName)
Else
For i = 0 To Hits - 1
Dim Row () As String
Row = G.ListTable.Get(i) ' This fills the row
' We have a set of labels for each record shown
Dim DtBox As Label
Dim WthrBox As Label
Dim EntryBox As Label
' Add inner panel - once for each hit!
Dim InnerPnl As Panel
InnerPnl.Initialize("")
pnlScroll.AddView(InnerPnl, 0, PanelTop, 100%x, 100%y)
' Views on our panel
HitBox.Initialize("HitBox")
DtBox.Initialize("dtBox")
WthrBox.Initialize("WthrBox")
EntryBox.Initialize("EntryBox")
DtBox.Initialize("")
HitBox.TextColor = Colors.Black
' Params below are View name, Left, Top, Width, Height
InnerPnl.AddView(DtBox, 0, 60dip, 25%x - 2dip, 60dip - 2dip)
Dim JO As JavaObject = DtBox
JO.Runmethod("setPadding", Array As Object(10dip, 10dip, 10dip, 10dip))
DtBox.Color = Colors.White
DtBox.TextColor = Colors.Black
DtBox.Tag = "Date"
WthrBox.Initialize("")
InnerPnl.AddView(WthrBox, 25%x + 2dip, 60dip, 75%x - 2dip, 60dip - 2dip)
Dim JO As JavaObject = WthrBox
JO.Runmethod("setPadding", Array As Object(10dip, 10dip, 10dip, 10dip))
WthrBox.Color = Colors.White
WthrBox.TextColor = Colors.Black
WthrBox.Tag = "Wthr"
EntryBox.Initialize("")
InnerPnl.AddView(EntryBox, 0, 122dip, 100%x, 100%y-WthrBox.Height)
Dim JO As JavaObject = EntryBox
JO.Runmethod("setPadding", Array As Object(10dip, 10dip, 10dip, 10dip))
EntryBox.Color = Colors.White
EntryBox.TextColor = Colors.Black
EntryBox.TextSize = 12
' Even if EntryBox is an EditText, EntryBox.Typeface can only be used to set bold, italic etc
EntryBox.Tag = "Entr"
EntryBox.Height = 4000dip ' Higher than possible text length initially to make sure text can be measured
For f = 0 To NumOfColumns - 1
Select f
Case 0
DtBox.Text = G.TextDate(Row(f))
Case 1
WthrBox.Text = Row(f)
Case 2
EntryBox.Text = Row(f)
' The line below is evidently not measuring the text height correctly,
' even when the size is at its default
EntryHeight = SU.MeasureMultiLineTextHeight(EntryBox, EntryBox.Text) * 1.5
'If EntryHeight > 1500 Then EntryHeight = EntryHeight + 2000
' EntryHeight doesn't expand when the above is added
'EntryBox.Text = "*** This box's height should be " & EntryHeight & " " & Row(f)
' PanelHeight below doesn't size bottom margin correctly
PanelHeight = EntryHeight + 100dip
Case 3 'Travelling
If Row(f).Trim = "" Then Place = "" Else Place = " ...Travelling: " & Row(f).Trim
End Select
Next 'f
EntryBox.Height = EntryHeight
EntryBox.Text = EntryBox.Text & Place & " (END)"
PanelTop = PanelTop + PanelHeight + 1dip
Next ' i = Hit
End If
pnlScroll.Height = PanelTop ' Height of OUTER panel
Catch
Msgbox ("Error executing query: " & LastException, G.AppName)
End Try
G.SQL1.EndTransaction
End Sub
Last edited: