Label in ScrollView

rossati

Active Member
Licensed User
Longtime User
Hello

I have a little problem on show a label in a ScrollView i.e. I had to increase the width of the scroll Panel to see all text. Here the fragments:


...
Dim scvText As ScrollView
Dim lblText As Label

...
scvText.Initialize(250) ' initialize the Scrollview
Activity.AddView(scvText, 0, 280, 100%x, 100%y) ' add the Scrollview on the Activity
lblText.Initialize("") ' initialize the Label for the text, without an EventName
scvText.Panel.AddView(lblText, 0, 0 ,100%x, 100%y) ' add the Label on the ScrollView internal Panel
...


Sub SetText(txt As String)
Dim ht As Float
'lblText.TextSize = 12 ' set the Label text size
lblText.Text = txt ' set the text string to the Label text property
ht = StrUtil.MeasureMultilineTextHeight(lblText, txt) ' measure Label height
scvText.Panel.Height = ht * 1.3 ' set the ScrollView internal Panel height to the measured height
lblText.Height = ht ' set the Label height to the measured height
scvText.ScrollPosition = 0
End Sub


Where is my mistake?

Best regards

John Rossati
 

klaus

Expert
Licensed User
Longtime User
Try this code:
B4X:
...
scvText.Initialize(250) ' initialize the Scrollview
Activity.AddView(scvText, 0, 280dip, 100%x, 100%y - 280dip) ' add the Scrollview on the Activity
lblText.Initialize("") ' initialize the Label for the text, without an EventName
scvText.Panel.AddView(lblText, 0, 0 ,scvText.Width, scvText.Height) ' add the Label on the ScrollView internal Panel
...
The ScrollView height is the screen height but the top edge is at 280 pixel that means the bottom edge is 280 pixels below the screen !
You should use dip values instead of 'absolute' pixels.
B4X:
Sub SetText(txt As String)
  Dim ht As Int
  'lblText.TextSize = 12 ' set the Label text size
  lblText.Text = txt ' set the text string to the Label text property
  ht = StrUtil.MeasureMultilineTextHeight(lblText, txt) ' measure Label height
  scvText.Panel.Height = ht     ' set the ScrollView internal Panel height to the measured height
  lblText.Height = ht ' set the Label height to the measured height
  scvText.ScrollPosition = 0
End Sub
There is no need to multiply the scvText.Panel.Height by 1.3.
I suspect that you needed to mutiply scvText.Panel.Height by 1.3 because the ScrollView bottom was below the screen by 280 pixels.

Best regards.
 
Upvote 0
Top