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.
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
Step 1: A label for the question
Start a new project called
TrueFalseQuiz. Set Screen1's Title toTrue or False?and AlignHorizontal to Center. Add a Label and rename itLabelQuestion. Set its FontSize to22, Width to Fill parent and TextAlignment to center.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41True or False?QuestionComponentsScreen1LabelQuestionRenameDeletePropertiesLabelQuestion (Label)BackgroundColorNoneFontBoldFontItalicFontSize22FontTypefacedefaultHTMLFormatHasMarginsHeightAutomaticWidthFill parentTextQuestionTextAlignmentcenter : 1TextColorDefaultVisibleLabelQuestion will show each question. Step 2: Buttons and labels
Add a HorizontalArrangement with two buttons in it, renamed
ButtonTrueandButtonFalse: green and red, with white text. Under the row, add two more Labels,LabelFeedback, with its Text empty, andLabelScore, with the TextScore: 0.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceButton?CheckBox?DatePicker?Image?Label?ListPicker?ListView?Notifier?PasswordTextBox?Slider?Spinner?Switch?TextBox?TimePicker?WebViewer?LayoutMediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41True or False?QuestionScore: 0ComponentsScreen1LabelQuestionHorizontalArrangement1ButtonTrueButtonFalseLabelFeedbackLabelScoreRenameDeletePropertiesLabelScore (Label)BackgroundColorNoneFontBoldFontItalicFontSize16FontTypefacedefaultHTMLFormatHasMarginsHeightAutomaticWidthAutomaticTextScore: 0TextAlignmentleft : 0TextColorDefaultVisibleTrue and False side by side, then the feedback and the score. 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 (
trueorfalse), 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.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1LabelQuestionHorizontalArrangement1ButtonTrueButtonFalseLabelFeedbackLabelScoreAny componentRenameDeleteShow Warningsinitialize global questions to make a list“ The Sun is a star. ”“ A byte is 4 bits. ”“ Python is a programming language. ”“ A micro:bit has 30 lights. ”“ Bats are mammals. ”initialize global answers to make a list“ true ”“ false ”“ true ”“ false ”“ true ”initialize global number to 1initialize global score to 0Five questions, their five answers, and two numbers to keep track. 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 numbernumberof the questions list, with select list item, and show the score.when Screen1.Initialize happens once, when the app starts: make it call
showQuestion.App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1LabelQuestionHorizontalArrangement1ButtonTrueButtonFalseLabelFeedbackLabelScoreAny componentRenameDeleteShow Warningsto showQuestiondoset LabelQuestion.Text to select list itemlistget global questionsindexget global numberset LabelScore.Text to join“ Score: ”get global scorewhen Screen1.Initializedocall showQuestionshowQuestion puts the current question on the screen, and runs when the app starts. 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 callnextQuestion.nextQuestionadds 1 tonumber. 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 bothButtonTrueandtrue.App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1LabelQuestionHorizontalArrangement1ButtonTrueButtonFalseLabelFeedbackLabelScoreAny componentRenameDeleteShow Warningsto nextQuestiondoset global number to get global number + 1if get global number > length of list list get global questionsthenset LabelQuestion.Text to “ Quiz over! ”set ButtonTrue.Enabled to falseset ButtonFalse.Enabled to falseset LabelScore.Text to join“ Final score: ”get global scoreelsecall showQuestionwhen ButtonTrue.Clickdoif compare texts select list itemlistget global answersindexget global number = “ true ”thenset global score to get global score + 1set LabelFeedback.Text to “ Correct! ”elseset LabelFeedback.Text to “ Wrong! ”call nextQuestionCheck the answer, score it, then move on. Step 6: Take the quiz
Connect the Companion and play. Then swap phones with a friend and take each other's quizzes.
9:41True or False?Python is a programming language.Correct!Score: 2Question 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.
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.
Play again
Add a Play again button that starts the quiz from the beginning.
Show a hint
Set
numberto 1 andscoreto 0, switch both buttons back on with Enabled true, and callshowQuestion.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
- 15 minutes: steps 1 and 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.
- 20 minutes: steps 4 to 6. Procedures are the big idea: one
showQuestion, used in two places.
Mistakes to look out for
- The lists of questions and answers in different orders, so right answers are marked wrong.
truetyped with a capital T in one list: compare texts is case sensitive.- A procedure renamed after it is called: the call blocks update themselves, but a typo in a new name is easy to miss.
- 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.