Search code examples
app-inventor

Loading file random text to label


I want to load a file from my computer into the MIT App Inventor. The file should have these lines. "Nice", "Well Done", "Good Job".

When a user press a button, a label will have the text "Nice", "Well Done" or "Good Job". I want it to choose random. How can i do this? I also have a file with over 200 quotes in it, that I want to load, and not write them as text manually.

How can i do this?

enter image description here

When i press randomtextButton on phone, i get error:

Error 2102: The file "myText.txt" could not be found

The file is uploaded in the designer screen -> Media -> "Upload File..." What is wrong?


Solution

  • I'm assuming, you stored each item in a separate line in the file like this:

    Nice
    Well Done
    Good Job

    To read a file from the assets, use // together with the file name. In the File.GotText event you have to convert the text first into a list using the split block (just split at \n, which is new line) to get a list and then use the pick a random item block.

    Btw. it helps to read the tooltips of the File.ReadFrom block or the documentation, how the file component works...

    enter image description here

    A very good way to learn App Inventor is to read the free Inventor's Manual here in the AI2 free online eBook http://www.appinventor.org/book2 ... the links are at the bottom of the Web page. The book 'teaches' users how to program with AI2 blocks. There is a free programming course here http://www.appinventor.org/content/CourseInABox/Intro and the aia files for the projects in the book are here: http://www.appinventor.org/bookFiles
    How to do a lot of basic things with App Inventor are described here: http://www.appinventor.org/content/howDoYou/eventHandling .

    Also do the tutorials http://appinventor.mit.edu/explore/ai2/tutorials.html to learn the basics of App Inventor, then try something and follow the Top 5 Tips: How to learn App Inventor