Mobile apps with App Inventor

True or False Quiz

Build a quiz app that asks questions from a list, checks each answer, keeps score and knows when it is over.

Day 4 50 to 60 minutes 6 steps Key Stage 3
Start step 1
Question 3, with two right so far.

A quiz app is a great way to revise, and a great way to learn how real apps keep data. The questions live in a list, the app works through them one by one, and it keeps your score. Swap in your own questions and you have a revision app for any subject.

You will learn

  • Keep questions and answers in lists
  • Make variables that keep their value
  • Make your own blocks with procedures
  • Check answers with if then else and keep score

You will need

  • A computer with a web browser
  • A phone or tablet with the MIT AI2 Companion app
  • Days 1 to 3, for components and blocks

Words to know

list
Many values in order. App Inventor counts a list's items from 1.
global variable
A variable every block in the app can use, which keeps its value.
procedure
Your own block, made from other blocks, that you can call by name.
compare texts
Checks whether two pieces of text are the same.

Step by step

  1. Step 1: A label for the question

    Start a new project called TrueFalseQuiz. Set Screen1's Title to True or False? and AlignHorizontal to Center. Add a Label and rename it LabelQuestion. Set its FontSize to 22, Width to Fill parent and TextAlignment to center.

    LabelQuestion will show each question.
  2. Step 2: Buttons and labels

    Add a HorizontalArrangement with two buttons in it, renamed ButtonTrue and ButtonFalse: green and red, with white text. Under the row, add two more Labels, LabelFeedback, with its Text empty, and LabelScore, with the Text Score: 0.

    True and False side by side, then the feedback and the score.
  3. Step 3: The questions and answers

    Go to Blocks. Variables made with initialize global keep their value for as long as the app runs. Make four: a list of questions, a list of the answers in the same order (true or false), the number of the question you are on, and the score.

    make a list is in the Lists drawer. Use its blue gear to give it five sockets.

    Five questions, their five answers, and two numbers to keep track.
  4. Step 4: Show a question

    A procedure is your own block, made from other blocks: like a function in Python. From the Procedures drawer, drag to procedure do and rename it showQuestion. Inside, set LabelQuestion's text to item number number of the questions list, with select list item, and show the score.

    when Screen1.Initialize happens once, when the app starts: make it call showQuestion.

    showQuestion puts the current question on the screen, and runs when the app starts.
  5. Step 5: Check the answer

    When True is tapped, compare the right answer for this question with true, using compare texts from the Text drawer. If they match, add 1 to the score and say Correct!, otherwise say Wrong!. Then call nextQuestion.

    nextQuestion adds 1 to number. If that goes past the length of the list, the quiz is over, so it switches the buttons off. Otherwise it shows the next question. Duplicate the True block for False, and change both ButtonTrue and true.

    Check the answer, score it, then move on.
  6. Step 6: Take the quiz

    Connect the Companion and play. Then swap phones with a friend and take each other's quizzes.

    Question 3, with two right so far.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Your own questions

    Change the questions and answers to a subject you are revising.

    Show a hint

    Keep the two lists in the same order, and the same length. Use the gear to add more sockets.

  2. Play again

    Add a Play again button that starts the quiz from the beginning.

    Show a hint

    Set number to 1 and score to 0, switch both buttons back on with Enabled true, and call showQuestion.

  3. Speak the questions

    Make the app read each question out loud.

    Show a hint

    Add a TextToSpeech, and call its Speak block at the end of showQuestion.

Notes for the teacher

Timing

  1. 15 minutes: steps 1 and 2.
  2. 15 minutes: step 3. Lists in App Inventor count from 1, unlike Python, which counts from 0. Worth pointing out to anyone who did the Python track.
  3. 20 minutes: steps 4 to 6. Procedures are the big idea: one showQuestion, used in two places.

Mistakes to look out for

  1. The lists of questions and answers in different orders, so right answers are marked wrong.
  2. true typed with a capital T in one list: compare texts is case sensitive.
  3. A procedure renamed after it is called: the call blocks update themselves, but a typo in a new name is easy to miss.
  4. Variables initialised inside an event instead of with initialize global: they reset every time.

Going further

This is the same quiz as the Python track's Quiz Master, built a different way. Comparing the two is a strong lesson in how ideas carry across languages.