Best view (control) for showing a large text file

klaus

Expert
Licensed User
Here you are.
B4X:
Sub Globals
    Dim scvText As ScrollView
    Dim lblText As Label
    
    Dim txt As String 
    Dim StrUtil As StringUtils
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' There is no layout file
    
    scvText.Initialize(100)        ' initialize the Scrollview
    Activity.AddView(scvText, 0, 0, 100%x, 100%y)    ' add the Scrollview on the Activity

    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
    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 Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub LoadText
    txt = File.GetText(File.DirAssets, "text1.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
Best regards.

EDIT: 2011.10.18
Updated the program, replaced the height calculation with MeasureMultilineTextHeight from the StringUtils library.
 

Attachments

Last edited:

Qal3awy

New Member
Licensed User
wow, that is nice example , with explinations for each code :sign0060:

cant thank you enough for that :)
 

klaus

Expert
Licensed User
I am running Windows XP SP3 and IE8.
I simply copy and paste the code from the IDE into the forums editor.
But, then comes the boring part, add missing blank characters.
Between some color changes blank characters are missing.
The Tab function has no effect, I add 2 blank characters for each identation.
Original code in the IDE:
B4X:
Sub Test(Val As Int) As Int
 Dim i As Int
 
 For i = 0 To 9
   If i = 5 Then
     a(i) = 2 * i * i
   Else
     a(i) = i * i
   End If
 Next
End Sub
Code in the forum editor after preview.

B4X:
Sub Test(Val As Int) As Int
 Dim i As Int
 
 For i = 0 To 9
   If i = 5 Then
     a(i) = 2 * i * i
   Else
     a(i) = i * i
   End If
 Next
End Sub
Then needs to be rearranged.

Erel, is there a hope that this could be improved, it's quite boring?

Best regards.

EDIT: 2012.05.03
This post has no meaning anymore because the display of code has changed.
 
Last edited:

reikhard

Member
Licensed User
just a suggestion or an idea...

Hi... i just have a simple question, what is the limit in characters or lines inside a edittext or a label? is there any way to use something like a webview but with the capability of editing? not only reading, im just thinking that the webview has a lot for reading controls like text wrap or zoom , thanks
 

jroriz

Active Member
Licensed User
After much trying, I came to a very very simple and effective solution to show large texts: webview!

Use YourWebview.LoadHtml ("<html> <body>" & YourLongFile & "</ body> </ html>")

Thed webview will do the trick... That simple.
 

jroriz

Active Member
Licensed User
WebView

The best and simple solution to me was using a webview.

The webview accepts html as content.

So, use YourWebView.LoadHtml("<html><body>" & YourLargeText & "</body></html>").

And the webview does the trick. And you still can play with html (colors, links, etc)
 

jroriz

Active Member
Licensed User
Use WebView instead

The best and simple solution to me was using a webview.

The webview accepts html as content.

So, use YourWebView.LoadHtml("<html><body>" & YourLargeText & "</body></html>").

And the webview does the trick. And you still can play with html (colors, links, etc)
 

MDEnt

Member
Licensed User
This is a great example! TY - I've started using ti to load various text files of varying length.

Question... How would a button be added to the view so that it is below the last line of text, and scrolls with the text - with each text file being of varying length?

I currently have the button added, but it doesn't scroll with the text. Ideally in my situation, I would want the button below the text for each file, but rather not have it fixed in place.


I should add that I am referencing the example by Klaus in post #21.
 
Last edited:

klaus

Expert
Licensed User
As the Label is on the ScrollView.Panel you can add the button onto the panel.
After loading the text to the Label, you know the height of the label.
Then you can set the Butons Top property according to the Label height and finaly set the ScrollView.Panel.Height to the Button.Top plus Button.Height plus a margin.

Best regards.
 

MDEnt

Member
Licensed User
In your example, would there be a recommended place to add the button?

I have placed it a few places on the scroll panel after the text loads. I can verify that a number is being pulled for the height with a messagebox, but the button is either missing, or it still at the top of the scroll panel.

The code I have added to your example is:

cbt.Initialize("ButtonPress") : cbt.Text="Press Me!"
Msgbox(scvText.Panel.Height,"")
scvText.Panel.AddView(cbt,20%x,scvText.Panel.Height,60%x,40dip)


An update: playing around with it - I found a solution based on your help - using the panel height above - but by also adding a -50, etc. (scvText.Panel.Height-50) value brings the button up off the bottom edge of the panel and back into view
 
Last edited:
Top