Best view (control) for showing a large text file

MDEnt

Member
Licensed User
So I quickly noticed by hardcoding a number such as -50 is a problem as it isn't perfect for every resolution.

Is there an easy way to get the true height of the label?

I've placed several message boxes throughout the code to try to grab the label height after text is loaded:

Msgbox(lblText.Height,"")

what I always get is a -2

I'm guessing from this line:

scvText.Panel.AddView(lblText, 15, 10 ,95%x, -2) ' add the Label on the ScrollView internal Panel -2 to automatically adapt the Label height to the text height

(I'm using your exact code in LongTextSimple)
 

peacemaker

Well-Known Member
Licensed User
What's wrong with a code:

B4X:
Dim p As Label
p.Initialize("")
p.Text = DocText
sv.Panel.AddView(p, 0, 0, sv.Panel.Width, sv.Panel.Height)

Dim a As StringUtils
b = a.MeasureMultilineTextHeight(p,p.Text)
p.Height = b
sv.Panel.Height = b
where
b = a.MeasureMultilineTextHeight(p,p.Text)
gives
"java.lang.IllegalArgumentException: Layout: -1 < 0"

?
 

klaus

Expert
Licensed User
Hi MDEnt,
Attached you find a modified version of the LongText example with a button at the bottom.
Sorry for the late answer, yesterday I was on a party and today I was hiking in the mountains.

Best regards.
 

Attachments

MDEnt

Member
Licensed User
Oh no problem - and thanks for the help. I'll take a look at the file to see... but what I discovered while tinkering around is that if I modify your line where the ht=panel height (in the set text sub - I got it to work!
 

MDEnt

Member
Licensed User
Is there a property to change the color of the vertical "handle" or "bar" that appears when the text scrolls? Only see the obvious Color propoerty for the background from the list of options.
 

mjtaryan

Active Member
Licensed User
A sort of general aside for all of our expert B4a people out there...

The experience level both in outright software development and using B4a appears to range from virtually 0 on up, I think it would be very helpful if, whenever we develop something new, we also include an in depth step by step tutorial that doesn't assume prior programming knowledge. Those of us who have such knowledge can skim over the baby steps. Just a thought.
 

mjtaryan

Active Member
Licensed User
The inner panel width is set to -1. This is a special value that causes the panel to fill the containing ScrollView.
You should instead use:
B4X:
sv.Panel.AddView(p, 0, 0, sv.Width, sv.Height)
This is related to yours and Peacemaker's post. I seem to have discovered a minor problem with MeasureMultilineTextHeight. In determining the height for the label and sv.panel it seems to work well until the label.textsize is increased above 20. I initially set the textsize = 20, but decided I wanted it more readable so increased it first to 22 and then to 24 but continue to use the height determined by MeasureMultilineTextHeight. At 22 a portion of the last line was hidden and at 24, all of the last line and the bottom portion of the line above were hidden. I resolved the problem by the following lines

Dim Ht As Int
Dim SU As StringUtils
Ht = SU.MeasureMultilineTextHeight(Label1, Label1.Text) + (Label1.TextSize * 3)
sv.Panel.Height = Ht
Label1.Height = Ht

However, I don't know if this will work for larger values.
 

dexMilano

Active Member
Licensed User
For future user I'm adding my 2 pences to this very useful tread.

You can create you scrollview (scv) and label (lbl) in designer too (so you can easily control the layout if you need more views)
You can reuse this code with few adjustment
- do not initialize scv and lbl (they already are in the layout)
- when adding the lbl to the scv, you before have to remove lable's parent
B4X:
lbl_info.RemoveView
scv_info.Panel.AddView(lbl_info, 0, 0 ,100%x, 100%y)
Thanks a lot guys.
you saved many hours of my life

dex
 
Last edited:

GMan

Well-Known Member
Licensed User
Hoi Klaus,
how can i set the control to display the ugly german special chars (like ü,ä,ö,ß - you know ;) )
 

klaus

Expert
Licensed User
The Label shows the special characters if the original text is encoded with UTF-8 encoding which is the standard encoding in Android.
Where does your text come from ?
You might have a look at chapter 14.10.6 Text encoding in the Beginner's Guide.
 

GMan

Well-Known Member
Licensed User
The txt is a plain texfile - generated with Notepad (PC)
 

GMan

Well-Known Member
Licensed User
Jo, find the solution - was that ANSI / UTF-8 Option.
Looong long time is gone that i used that "feature" the last time
 

GMan

Well-Known Member
Licensed User
The older i get the more i need updates (i think) :rolleyes:
 

GaryK4

Member
Licensed User
I would suggest you a ScrollView view with a Label.

Attached a small test program that shows the principle.

Best regards.
My program uses a StdActionBar with (4) tabs.
I am trying to add long text to the last tab. However, I am not having any luck

I want lb_help.Text to scroll in the last tab.
The text displays, but does not scroll.
What am I doing wrong?
Below is partial code.

B4X:
vp.Initialize("vp", 5, 100%x, height)
Activity.AddView(vp.AsView, 0, 0, 100%x, height)
vp.Panels(3).LoadLayout("3")
bar.Initialize("bar")
bar.AddTab("Help")

sv_help.Initialize(0)
sv_help.Panel.Color=Colors.Black
lb_help.Text = File.GetText(File.DirAssets,"help.txt")
  
vp.Panels(3).AddView(sv_help,0,90dip,100%x,50%y)
 

klaus

Expert
Licensed User
You need to set the height of the scrollviews internal panel and the label.
You need the StringUtils library.
B4X:
Dim stu As StringUtils
lb_help.Height = stu.MeasureMultilineTextHeight(lb_help, lb_help.Text)
sv_help.Panel.Height = lb_help.Height
Perhaps you need to add a DoEvents after sv_help.Panel.Height = lb_help.Height
 

GaryK4

Member
Licensed User
You need to set the height of the scrollviews internal panel and the label.
You need the StringUtils library.
B4X:
Dim stu As StringUtils
lb_help.Height = stu.MeasureMultilineTextHeight(lb_help, lb_help.Text)
sv_help.Panel.Height = lb_help.Height
Perhaps you need to add a DoEvents after sv_help.Panel.Height = lb_help.Height
Klaus,
That did the trick with a couple of other tweaks. I added the label with the designer, then removed the parent in the code.
It now works.

Thanks!!

B4X:
sv_help.Initialize(0)
sv_help.Panel.Color=Colors.Black
vp.Panels(3).AddView(sv_help,0,50dip,100%x,90%y)

lb_help.RemoveView
sv_help.Panel.AddView(lb_help,0,0,100%x,100%y)
lb_help.Text = File.GetText(File.DirAssets,"help.txt")
Dim stu As StringUtils
lb_help.height = stu.MeasureMultilineTextHeight(lb_help, lb_help.Text)
Log(lb_help.height)

sv_help.Panel.height = lb_help.height
 
Top