Search code examples
androidfileapp-inventor

Show .txt file line by line in App Inventor 2


I would like to read a txt file and show line by line in App Inventor 2, but I don't know which components/blocks to use.

Something like these

choose .txt for reading > show only first line > press button > show second line

This is Blocks that I try to use and still no idea Sorry, If it didn't help


Solution

  • Ploy, you can do as below:

    enter image description here

    Firstly, you need to create global empty list. Next, global LineIndex variable which indicate the number of lines that will be shown. Lastly, you have to configure the MaxIndex number that refer to maximum lines in your file.

    Then, at Screen1.Initialize just call File1.ReadFrom function. You can specify your own file name. Yet, please make sure it is saved in .csv format. The contain of the file will be something such below:

    enter image description here

    In this example, I'm using two different buttons. One button to read the file and another button to reset the global LineIndex countdown. From first image, you can see in when Read_Button.Click part, I have set the global LineIndex countdown to increase automatically by 1 after Read_Button is clicked.

    Then, for safety reason, I have add another check "If and Else" statement. The Result_Label will only show the contain in the file if the LineIndex countdown is not exceed the maximum line in the file.

    That's it.