Android Question [ANSWERED] Scrolling text

Lee Gillie CCP

Active Member
Licensed User
Longtime User
NUB's first post...

Making a scrolling text "logging" area for doing old-school logging of program activity while trying various technologies in B4A. I see scrolling text been hammered on a lot over the years, a lot of stuff with reflection, and a number of tacts that I did not fully understand. But I think I found a fairly simple solution with only one flaw that I can find.

The approach is to create a ScrollView, which is parent to a Label. Parenting established at runtime, rather than design time, because I don't see a way to do it at design time. The vertical height of text displayed in the label is computed via Canvas, and both the Label and ScrollView Panel height are grown based on that computed height. And THEN to attempt to keep the view scrolled to the bottom as new text lines are added....

That's the snag I found. There is always one text line that is scrolled out of view at the bottom. I can not seem to find any value for the ScrollPosition that reveals that last line. The user can scroll to it by dragging, but I can not seem to do it programmatically. Any clues for me?
 

Attachments

  • ScrollText.zip
    12.7 KB · Views: 368

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Something of a kludge it seems to me, but by ensuring a trailing line break in the displayed text, that next blank line is the one out of view. Also, found the ScrollToNow. Note (su is StringUtilities) Changing my posted code as:

Public Sub PLog( text As String )
Dim t As String
t = lblText.text
If t.Length > 0 Then
t = t & text & Chr(10)
Else
t = text & Chr(10)
End If
Dim c As Canvas
c.Initialize(lblText)
Dim hgt As Int
hgt = su.MeasureMultilineTextHeight(lblText,t)
svsScroll.Panel.Height = hgt
lblText.Height = hgt
lblText.text = t
svsScroll.ScrollToNow(hgt)
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Replace:
svsScroll.ScrollPosition = svsScroll.Panel.Height
by
svsScroll.ScrollPosition = svsScroll.Panel.Height
DoEvents
svsScroll.ScrollPosition = svsScroll.Panel.Height
DoEvents


ScrollPosition is executed, by the OS, before the new height is active.

A more elegant solution is:
Replace:
svsScroll.ScrollPosition = svsScroll.Panel.Height
by
CallSubDelayed2(Me, "ScrollTo", svsScroll.Panel.Height)

And add tis routine:
Sub ScrollTo(Position As Int)
svsScroll.ScrollPosition = Position
End Sub


You dim and initialize a Canvas in your routine, you should remove it because you don't use it.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Thank you Klaus, your reply is greatly appreciated.

Yes, the canvas was a residual of trying various methods to measure multiline text height. StringUtils seems to work well.

Using your tips my logging routine works perfectly as:
Public Sub PLog( text As String )
Dim t As String
t = lblText.text
If t.Length > 0 Then
t = t & Chr(10) & text​
Else
t = text​
End If
Dim hgt As Int
hgt = su.MeasureMultilineTextHeight(lblText,t)
svsScroll.Panel.Height = hgt
lblText.Height = hgt
lblText.text = t
DoEvents
svsScroll.ScrollToNow(hgt)​
End Sub​
 
Upvote 0
Top