Android Question Make a Text with scrollview

Douglas Farias

Expert
Licensed User
Longtime User
Hi all, i m trying to hours to find a good solution for this problem *-*
i m making a app based on text.

ok the problem for now is
1° i need make it with scroll
2° i need calculate the textsize and make the textsize = inner of scrollview

how can i make this?

for example:
I have one scrollview and i add on this scrollview my label.

B4X:
scletra.Panel.AddView(lbtextoletra,0%x,0%y,100%x, 100%y)
Its ok

the inner is 500 perfect

now i open one text, 50 lines ok works fine.

now i open another text 5000 lines *-* = problem

how can i know and make my text auto ajustable?
if the text have 5000 lines the 100%y is go change and the inner of scrollview need change too, but how to make it automatic?

note: i never know the text size, can have 50 - 10.000 lines *-*

if i set
B4X:
scletra.Panel.AddView(lbtextoletra,0%x,0%y,100%x, 1000%y)
and inner 5000 for big text it work fine, but to small text is realy ugly, and i think this is not a good way

here is a image using 1000%y and inner 5000 *-*

thx to all
 

Attachments

  • 10620682_829410137089204_5397181958091371252_n.jpg
    10620682_829410137089204_5397181958091371252_n.jpg
    59.4 KB · Views: 169
Last edited:

DonManfred

Expert
Licensed User
Longtime User
It´s not a good idea to autosize the text.... You should use a textsize which is easy to read for the user....

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim scvText As ScrollView
    Dim lblText As Label

    Dim txt As String
    Dim StrUtil As StringUtils
    Dim pm As PackageManager

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
  scvText.Initialize(100)        ' initialize the Scrollview
  Activity.AddView(scvText, 0, 0, 100%x, 100%y)    ' add the Scrollview on the Activity
    Activity.Title = "Ver. "&pm.GetVersionName(Main.appname)&" B"&pm.GetVersionCode(Main.appname)

  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
  Activity.Color = Colors.RGB(250, 250, 210)                ' set the Label background color

    lblText.Color = Colors.RGB(250, 250, 210)                ' set the Label background color
  lblText.TextColor = Colors.Black                                ' set the Label text color
  
  LoadText                                    ' load the text
  SetText                                        ' set the text
End Sub
Sub LoadText
    'Dim tr As TextReader
  'tr.Initialize2(File.OpenInput(File.DirAssets, "version_history.txt"), "utf-8")
  'Dim s As String
  'txt = tr.ReadAll  
    txt = File.GetText(File.DirAssets, "version_history.txt")    ' load the text file into the string
End Sub

Sub SetText
  Dim ht As Float
  
  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    ' set the scroll position to the top of the text
  DoEvents                                        ' needed to execute the previous line
End Sub
 
Upvote 0
Top