Best view (control) for showing a large text file

moster67

Expert
Licensed User
Longtime 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?
 

moster67

Expert
Licensed User
Longtime 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.
 
Upvote 0

klaus

Expert
Licensed User
Longtime 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.b4x.com/forum/additional...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

  • FFTHelp.jpg
    FFTHelp.jpg
    37.3 KB · Views: 2,323
Upvote 0

susu

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

klaus

Expert
Licensed User
Longtime 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

  • LongText.zip
    277.9 KB · Views: 2,437
Upvote 0

JesseW

Active Member
Licensed User
Longtime 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 :)
 
Upvote 0

klaus

Expert
Licensed User
Longtime 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:
Upvote 0

agraham

Expert
Licensed User
Longtime 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:
Upvote 0

JesseW

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

agraham

Expert
Licensed User
Longtime 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

  • OurGarden.jpg
    OurGarden.jpg
    27.4 KB · Views: 1,277
Upvote 0

klaus

Expert
Licensed User
Longtime 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

  • LongText.zip
    276.9 KB · Views: 1,507
Upvote 0

JesseW

Active Member
Licensed User
Longtime 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 :)
 
Upvote 0

agraham

Expert
Licensed User
Longtime 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

  • device.jpg
    device.jpg
    16.5 KB · Views: 1,277
Upvote 0

JesseW

Active Member
Licensed User
Longtime 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
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime 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...
 
Upvote 0

klaus

Expert
Licensed User
Longtime 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

  • LongText.zip
    278.2 KB · Views: 2,050
  • LongText.jpg
    LongText.jpg
    41.5 KB · Views: 25,840
Last edited:
Upvote 0

Qal3awy

New Member
Licensed User
Longtime 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 :)
 
Upvote 0
Top