Best view (control) for showing a large text file

moster67

Expert
Licensed User
In my application I need to show some instructions (nearly 200 lines). I created a new activity and added the text in an edittext-control. It works but the scrolling of the lines is not the best.

Perhaps there is a better control for showing large text-files? Any ideas?
 

klaus

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

Attached a small test program that shows the principle.

Best regards.
 

Attachments

moster67

Expert
Licensed User
That's really nice Klaus. :icon_clap:

I just love it with that kind of papirus-roll effect and that slight shadow at the top and at the end of the text.

I already modified it slightly to fit my needs but only in a minor way.

Thanks again Klaus. I owe you a beer.



I would suggest you a ScrollView view with a Label.

Attached a small test program that shows the principle.

Best regards.
 

klaus

Expert
Licensed User
Hi moster67,

I'm glad that you like it, it was just a suggestion, but I find it quite convenient.
I used the Scrollview with Labels the first time in the FFTdemo program, for the help with some additional features, that comes with the Fast Fourier Transform library.
http://www.basic4ppc.com/forum/addi...ates/6989-fast-fourier-transform-library.html
In this program I removed the views from the avctivity and added them to the ScrollView. In the example in my previous post I used a separate Layout which is much more convenient.

Best regards.
 

Attachments

susu

Well-Known Member
Licensed User
I also got problem with large text file. In your example, you set lblText.Height = 3200 to display full text. What if we don't know how long the text file is? If the text file is too long and we set the lblText.Height is too small, it won't be display completely.
Otherwise, we need to consider the Inner Height of ScrollView, if it doesn't match with lblText.Height, it won't be display correctly.

I just wish B4A had a method like "ScrollView.Text" to insert the text to ScrollView automatically.
 

klaus

Expert
Licensed User
Hi susu,

Attached you find a workaround to display a long text with a Label on a ScrollView.
The number of lines is parsed from the text and the heights of the Label and the ScrollView.Panel are calculated.
It doesn't work too bad with different TypeFaces and TypeSizes.
The problem at the moment is the result of the MeasureStringHeight function. As soon as we get the total line height it should work OK.

Best regards.
 

Attachments

JesseW

Active Member
Licensed User
Klaus, I'm wondering if a simpler solution might be to remove the panel from the scrollview and use agrahams reflection library to add a textview directly to the scrollview (if its possible) and set the textviews height & width to WRAP_CONTENT, which is a const value of -2. This way the textview will automatically size itself to the content, no matter the size, and being directly on the scrollview, its not limited to the panels size, and the panels size doesn't have to be calculated. I've seen it done this way in java. We should all get together and ask Erel for a naked scrollview :)
 

klaus

Expert
Licensed User
I fully agree with you :).

I didn't know that a Height or Width value of -2 does adjust the height or width of a TextView to the content, this works with the Designer.

I haven tried agraham's Reflector library yet, I haven't enough knowledge on Android (yet?) to use it.

Best regards.
 
Last edited:

agraham

Expert
Licensed User
Here you are :)
B4X:
   Dim lbl As Label
   Dim o As Reflector
   ...
   o.Target = svw ' a ScrollView
   Dim args(3) As Object
   args(0) = lbl
   args(1) = -1
   args(2) = -2
   Dim types(3) As String
   types(0) = "android.view.View"
   types(1) = "java.lang.int"
   types(2) = "java.lang.int"
   o.RunMethod("removeAllViews")
   o.RunMethod4("addView", args, types)
 
Last edited:

JesseW

Active Member
Licensed User
Andrew, you're the man again :) I tried for hours to do that with no luck. Thanks for stepping in and helping :)
 

agraham

Expert
Licensed User
Thanks for the kind thought but there is no need. I am a retired Systems Engineer (you would never have guessed would you :)) and I've sort of "adopted" Erel and his products as a hobby as they let me get my hands dirty doing the interesting stuff I would have liked to do at work but had to leave to minions as I was a senior manager in the companies I worked for.

It wastes/productively passes a lot of time, especially in winter when we can't/don't want to get out and do the gardening (and you should see the size of our "garden"!).
 

Attachments

klaus

Expert
Licensed User
Thank's Andrew, you are the Master ! :)

Attached the modified version of post #6 with agraham's soloution.

I have also begun trying to 'play' with the Reflection library but had no success either.

Best regards.

PS. Quite a huge garden !
 

Attachments

JesseW

Active Member
Licensed User
Andrew, your too kind! And you're a credit to this community :)

Here's a twist: I replaced the label with an edittext, then had to runmethod a setFillViewpirt on the scrollview to get the edittext full screen, then set the edutrext gravity to top for proper cursor position. Then all was well till I pasted in a large text and brought up the keyboard. The bottom of the sv aligns properly to the keybd, but the top of the sv and the title lavle are both pushed way up above and behind the notification bar. Any ideas?

Thanks :)
 

agraham

Expert
Licensed User
The bottom of the sv aligns properly to the keybd, but the top of the sv and the title lavle are both pushed way up above and behind the notification bar
I am afraid that I don't understand this. Where is the title label located? As a Scrollview can only have one child I assume it must be located on the Activity above the ScrollView. On both my phone and the emulator nothing moves when the keyboard pops up. :confused:
 

Attachments

JesseW

Active Member
Licensed User
Here is the code:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim sv As ScrollView
   Dim ed As EditText
   Dim o As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
   sv.Initialize(0)
   activity.AddView(sv, 0, 0, -1, -1)
   ed.Initialize("")
   ed.Gravity = Gravity.TOP
   o.Target = sv
   Dim args(3) As Object
   args(0) = ed
   args(1) = -1
   args(2) = -2
   Dim types(3) As String
   types(0) = "android.view.View"
   types(1) = "java.lang.int"
   types(2) = "java.lang.int"
   o.RunMethod("removeAllViews")
   o.RunMethod4("addView", args, types)
   o.RunMethod2("setFillViewport", "true", "java.lang.boolean")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
attached is the apk
 

JesseW

Active Member
Licensed User
Wow, this is blowing my mind. I ran the apk on my DX and it is backwards from how the 1.6 hvga emulator is. the edittext aligns to the top of the screen and goes below the keyboard. I tried on a wvga854 2.2 emulator and same thing until I did a long click on the menu button to drop the keyboard, but it didn't drop, but then the edittext aligned to the keyboard and goes up off the screen. this is madenning... the emulators and the device are all working in different ways, but it does seem that no matter if the hidden part of the edit text is above the notificatio bar or below/under the keyboard, the edittext is always the same size, the size it is fullscreen without the keyboard, and when the keyboard comes up, it causes problems..

I hope you can see what I'm talking about. Appreciate your help...
 

klaus

Expert
Licensed User
Coming back to the display of long texts.

In the attached program I still use the default ScrollView.Panel with a Label with it's height set to -2.
After setting the Label's Text property I read the Label's real Height with MeasureMultilineTextHeight from the StringUtils Library and set the ScrollView.Panel.Height to this height.

The program displays different texts with different fonts and different font sizes.

Best regards.

EDIT: 2012.05.03
Replaced the height calculation with MeasureMultilineTextHeight instead with the Reflection library.
 

Attachments

Last edited:

Qal3awy

New Member
Licensed User
thats great code klaus

as a VEEERY BIGGENER with zero experience in programing and all ... i am trying to understand the code but too many things get me confused

so can u post the previous example in a simpe way ? without spinners and without loading text files ...

only one label with a text on it , and one scrollview ... and the code which makes scrollview adjust to the size of text of the label ...


can u do that for me ?

with big thanks :)
 
Top