Android Question [ANSWERED] Scrolling text

Discussion in 'Android Questions' started by Lee Gillie CCP, May 29, 2015.

  1. Lee Gillie CCP

    Lee Gillie CCP Active Member Licensed 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?
     

    Attached Files:

    mansour-ghanaei likes this.
  2. Lee Gillie CCP

    Lee Gillie CCP Active Member Licensed 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
     
    mansour-ghanaei likes this.
  3. klaus

    klaus Expert Licensed 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.
     
  4. Lee Gillie CCP

    Lee Gillie CCP Active Member Licensed 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​
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice