Wordwrap in Scrollview

noclass1980

Active Member
Licensed User
Longtime User
Hi, I want to load a Notepad produced txt file into a scrollview. The file loads ok and I can vertically scroll. However, the problem is that the text is not word wrapping so some words are lost on the right hand side. I tried two different txt files with the same result. I used Klaus' Long Text Simple example. Any suggestions? Thanks
 

stevel05

Expert
Licensed User
Longtime User
How have you sized the label, it's difficult to help without seeing the code. Can you zip and post it here?
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Hi, not able to zip the code. I followed Klaus's example as shown below.
the dimensions of the scrollview is set in the Designer.
B4X:
Sub LoadScrollText
Dim ht As Float
lblModel1_Text.Initialize("")
scv_Model1.Panel.AddView(lblModel1_Text,0,0,100%x,100%y)
lblModel1_Text.Color=Colors.RGB(0,0,0)
lblModel1_Text.TextColor=Colors.White
txt = File.GetText(File.DirAssets, "description.txt")   
lblModel1_Text.text = txt                    ' set the text string to the Label text property
ht =     strUtil.MeasureMultilineTextHeight(lblModel1_Text, txt)    ' measure Label height
scv_Model1.Panel.Height = ht        ' set the ScrollView internal Panel height to the measured height
lblModel1_Text.Height = ht                    ' set the Label height to the measured height
scv_Model1.ScrollPosition = 0    ' set the scroll position to the top of the text
DoEvents                                        ' needed to execute the previous line
End Sub

I've solved the problem by changing

B4X:
scv_Model1.Panel.AddView(lblModel1_Text,0,0,100%x,100%y)
to
B4X:
scv_Model1.Panel.AddView(lblModel1_Text,0,0,480,100%y)

The actual width of the Scrollview is 540 but I had to try different values to determine that 480 was correct width for the label. Does this mean that there is an effective margin of 30 on left and right sides of scrollviews?
A follow on question: Erel has been clear in other posts that we should use the dip unit so that it displays correctly on different screens. Do you think this approach would work on a different screen ok. I'm currently developing for a Galaxy 10" tab and would like it to work on a 7" screen as well.

Thanks for the help. Do you know anything about panel orientations? If so, could you check my other post? Thanks :)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You should use Dip or percentages where ever possible to specify drawing sizes, using absolute values will all but guarantee to look odd on different devices unless you scale it yourself.

Does Klaus's example run OK on your device? It does on mine. If so, there must be a difference between the two somewhere.

You say that the scrollview width is 540, is that the width of your device. Did you add the scrollview using Activity.Addview(scv,0,0,100%x,100%y)? I don't see a margin on Klaus's example.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
No the scrollview is added in the Designer (together with an imageview and 3 buttons) and is placed in the lower right quadrant of the portrait screen taking up ~25% of the screen area.Its properties are
Left = 220
Top = 620
Width = 540
Height = 460

I made the comment about the margin of 30 as this was half the difference between the scrollview width (540) and the label width (480) which displayed the text correctly.

Would it be better to add these views at runtime rather than loading a Layout? It seems sensible because then I can relate the width to %x and use dip units for the position. The designer doesn't seem to allow the use of dip units. Is this correct or have I missed something? Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can do the same thing with the designer scripts, although I'm not sure how the Scrollview internal panel will react when sized (or if you can in the designer script), and what the value of %x and %y will be when you add to it in code after adding the scroll view via designer. You'll need to experiment a bit to see what the values actually are.

Sometimes it is just easier to think about doing it in code.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi, not able to zip the code.
Why not ?
The project I posted works OK.
So you need to look at the changes you made.
And tell us exactly what you changed.
The BEST solution is to post your project.
What width is you device ?
You set Left = 220 and Width 540.
So the right border is 760.
You set the width of the Label to 100%x the whole screen with but the ScrollView width seems not to cover the whole screen !?
Without the answers to these questions impossible to give you concrete advices.
If there is no wordwrap the problem is probably a hidden part on the screen.
But as already said, without the project difficult to help.

Best regards.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Hi, thanks for the reply. I've attached the zip. The label width is set in line 283. Its currently 100%x and I have to change it to 480dip in order to display the text correctly. Suggestions to solve welcome. Thanks. I'm using a Galaxy 10.1 Tab, width 800?
 

Attachments

  • TestScrollView.zip
    58.4 KB · Views: 319
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I Can't run the code as I don't have the AHPage libraries loaded, but I can see what you are doing in there.

I've just done a test loading a scrollview with the designer and the ScrollView.Panel.Width is reported as -1, which as you know is Fill_Parent. If you test this with your app you will probably find the same. I seem to remember having seen this on the Forum somewhere but can't find it at the moment.

So when you add the Label to the scrollview panel using 100%x it's actually setting it to the width of the screen.

You could set the label width to the ScrollView.width. Interestingly, even if you set the Scrollview.Panel.width before adding the label, it still gets the width of the screen.

I am not certain about the 60 pixels unless perhaps you didn't use DIP with the absolute size?

If you still have a problem, I'll try to load the library later today if I can, unless someone else already has it loaded.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem is what we suspected !

Replace line
B4X:
scv_Model1.Panel.AddView(lblModel1_Text,0,0,100%x,100%y)
by this one and it works as you expect it.
B4X:
scv_Model1.Panel.AddView(lblModel1_Text,0,0,scv_Model1.Width,scv_Model1.Height)
Best regards.
 
Last edited:
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Wordwrap now working

:sign0142:
Brilliant, works perfectly, many many thanks!
 
Upvote 0
Top