Android Question Is there a sample program for displaying textlines from a file?

Tjitte Dijkstra

Member
Licensed User
Longtime User
I am working on an educational game. I want to give the adult users (parents / teachers) the possibility to enter some lines (e.g. tips or peptalks etc) for the new kids who work with the game.
What I need is an EditText (single line = false) and with TextWriter & TextReader I save and load it.
So far, so good, but what next?
1. Now I want to print the loaded tekst line for line on a label or something. But how?
2. Is there in the sample files some sort of a textprocessor?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim f As List = File.ReadList(File.DirAssets,"AbstractLayouts.txt")
    For i = 0 To f.Size-1
        Log(f.Get(i))
    Next
 
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
Thank you so far.
That is the example that I found in the Wyken Seagrave guide. But then you only have it in the Log and I managed to do that already.
I want to display it on the tablet, preferably on a label.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I want to display it on the tablet, preferably on a label.
B4X:
    Activity.LoadLayout("main")
    Dim f As String = File.ReadString(File.DirAssets,"AbstractLayouts.txt")
  Label1.Text = f
 

Attachments

  • readstring.zip
    7.1 KB · Views: 111
Upvote 0

Tjitte Dijkstra

Member
Licensed User
Longtime User
Thnx again. See my code. It works for only one line and then the next line replaces the former, etc.
I want to display a lot of lines, maybe even with a scrollbar?

B4X:
Sub OPEN_Click
Dim TextReader1 As TextReader
    TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "Test.txt"))
    Dim line As String
    line = TextReader1.ReadLine   
    Do While line <> Null
        Log(line) 'write the line to LogCat
        line = TextReader1.ReadLine
        Label1.Text = line
    Loop
    TextReader1.Close   
End Sub
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
These will be the steps involved.
  1. Add a ScrollView to the Activity.
  2. Add a Label to the ScrollView. Use any height for the Label. We will set it correctly later.
  3. Read the contents of the text file to Label using File.ReadString (or TextReader.ReadAll)
  4. Add StringUtils library to the project and use StringUtils.MeasureMultilineTextHeight to calculate the minimum height needed to display the text you read from the file. Set this value as the Label's height.
  5. Set the ScrollView.Panel.Height to a value >= the Label's height you got in Step 4.
 
Upvote 0
Top