Android Question Need a scrollable label (or some other form of text display)

maxh2815

Member
Hi, at the moment I have a label containing text. The text is updated periodically (every 2 seconds or so). Sometimes the text can be quite long, and it goes out of the width and height of the label. What do I need to do to make a label which:
  • Resizes itself based on the updated content and
  • Any text that goes outside the bounds makes the label scrollable instead of pushing the text onto a new line. I don't want it to automatically scroll, only when the user scrolls it.
  • Can be scrolled both vertically and horizontally (preferably at the same time).
I've tried using the ScrollView2D library, but I am not having much luck as it does not scroll.
 

klaus

Expert
Licensed User
I've tried using the ScrollView2D library, but I am not having much luck as it does not scroll.
What have you tried ?

I use ScrollView2D and I can assure that it scrolls.
Did you set correctly the size of the inner Panel, ScrollView2D1.Panel.Width and ScrollView2D.Height ?

How do you distinct between the horizontal and vertical space needed.
Is the horizontal space needed the length of the longest sentence ?
If yes, you need to split the text into sentences, Regex.Split.
Measure the length of the longest sentence with Canvas.MeasureStringWidth.
Set the width of the Label and the ScrollView2D.Panel to that value.
Then measure the height of the multiline text with StringUtils MeasureMultilineTextHeight.
Set the height of the Label and the ScrollView2D.Panel to that value.

You may have a look at this thread: Best view (control) for showing a large text file.
It uses a ScrollView, only for vertical scrolling, but it could inspire you.
 

Star-Dust

Expert
Licensed User
Do you like this?
 

maxh2815

Member
I don't like it.
I don't want it to automatically scroll
is what I said in the question. Thanks for trying to help though 👍.

As for the other answer...
klaus thanks for the information. I didn't know you had to set the dimensions of the scroll view's panel. Once I did that, everything worked fine. I do have one more quick question about measuring the height and width of the text. The height is fine, but when I try and get the width of the text, it gives me a massive value that goes way further than the text needs. Is this the result of a common mistake?

B4X:
Dim c As Canvas
'Root is what you get from the parameter of B4XPage_Created
c.Initialize(Root)
Dim su As StringUtils

'logsLabel is the label that contains large amounts of text (held in the result variable)
Dim textHeight As Float = su.MeasureMultilineTextHeight(logsLabel, result)

'This returns a very large value
Dim textWidth As Float = c.MeasureStringWidth(result, logsLabel.Typeface, logsLabel.TextSize)
Maybe I need to initialize the canvas on `logsLabel` instead of `Root`?
 

klaus

Expert
Licensed User
Unfortunately you give not enough information.

First, you have not answered my question:
Is the horizontal space needed the length of the longest sentence ?
Then you haven't read carefully enough my post.

You can use this code to declare the Canvas, it minimizes the need of memory:
B4X:
    Private bmp As Bitmap
    Private cvs As Canvas
    bmp.InitializeMutable(2dip, 2dip)  
    cvs.Initialize2(bmp)
What is the content of result?

Then it seems that you are using B4XPages, then you might use a B4XPanel and a B4XCanvas to measure the text dimensions.
 
Top