Byte by Byte › Book 7: Networks, Programming and Machine Learning › Chapter 11: Building Mobile Apps with MIT App Inventor
Chapter 11: Building Mobile Apps with MIT App Inventor
You will learn: what an app is and the main types of mobile app, the stages of app development, how to plan an app with a wireframe, the Designer and Blocks editors in MIT App Inventor, visible and non-visible components, event-driven programming, variables, lists, procedures and decisions in blocks, saving data with TinyDB, testing on a phone with the AI Companion, and app permissions, privacy and publishing.
🔎 The Case
The question of the videos would have to wait one more day, because the clips were spreading faster than anyone could explain them.
By morning they had been forwarded more than twenty thousand times. Not on websites, which people visited when they chose to, but phone to phone, family group to family group. Hop's grandmother had received the same clip from four different relatives before breakfast, each with a message: Is this true?? Forward to everyone!
"People don't come to the Truth Desk," said Quill gloomily. "The lies come to them. Into their pockets. By the time they think of checking our site, they have already pressed Forward."
Luna, who had stayed at Snowpeak to help, was looking at her phone. "Then the Truth Desk has to be in their pockets too," she said. "A little app. Something you open in the two seconds before you forward. Type what the message is about, and it tells you if we've already checked it."
Memoon had built games, websites and Python programs, but never an app for a phone. "Do I need to learn a whole new language?"
"Not to start with," said Nimo. "Remember Scratch? There's a tool that builds real phone apps out of blocks. You'll recognise it in about four seconds."
💡 The Concepts
What is an app? An app (application) is software designed to do particular tasks for its user (Book 6). Mobile apps run on phones and tablets, which bring special abilities that desktop computers usually lack: touchscreens, cameras, microphones, GPS location, motion sensors, notifications, and being with you all the time. In India, many people's first and only computer is a smartphone, so mobile apps are how they bank, learn, shop, travel and get the news.
Types of mobile app.
| Type | How it is made | Strengths | Example |
|---|---|---|---|
| Native app | Built for one operating system (Android or iOS) using its own tools and languages, such as Kotlin or Swift | Fastest; full use of the phone's hardware | Camera apps, most games |
| Web app | A responsive website (Chapter 10) that behaves like an app, used in the browser | Works on any device; no installation | Many news and shopping sites |
| Hybrid / cross-platform app | Written once, then packaged to run on both Android and iOS | One code base for both | Many business and banking apps |
Apps are downloaded from an app store: the Google Play Store for Android and the Apple App Store for iPhone.
The app development life cycle. Professional apps are built in stages. It is the same problem-solving cycle you have used throughout this series, with one difference: it repeats, because real apps are improved and updated for years.
- Idea and research: what problem does it solve, and for whom? Do similar apps already exist?
- Plan and design: decide the features; sketch every screen as a wireframe, a simple box drawing showing where each element goes; plan how the user moves between screens.
- Build: create the user interface and write the code.
- Test: try it on real devices, with real users, including normal, boundary and invalid inputs.
- Release: publish it, or share it with its users.
- Maintain: fix bugs, respond to feedback, add features, release updates.
MIT App Inventor. MIT App Inventor is a free, browser-based tool from the Massachusetts Institute of Technology for building real Android (and iPhone) apps with blocks. It is used in schools all over the world, including in Indian Class 8 computer courses. You reach it at ai2.appinventor.mit.edu and sign in with a Google account (a grown-up should help). Your projects are saved automatically in the cloud, so it is a SaaS and PaaS tool in one (Chapter 2).
App Inventor has two main views, switched with buttons at the top right.
- The Designer is where you build what the app looks like.
| Panel | What it holds |
|---|---|
| Palette (left) | All the components you can add, in groups: User Interface, Layout, Media, Sensors, Storage and more |
| Viewer (centre) | A preview of the phone screen; drag components onto it |
| Components (right of centre) | A tree of everything on the screen, where you can rename components |
| Properties (right) | Settings for the selected component: text, colour, size, font, image and so on |
A component is any building block of an app. Visible components appear on the screen: Button , Label , TextBox , Image , ListView , Slider , CheckBox . Non-visible components work behind the scenes and appear below the Viewer: Clock (timers), TinyDB (storage), TextToSpeech , Notifier (pop-up messages), Sound , AccelerometerSensor , LocationSensor .
Layout components such as HorizontalArrangement and VerticalArrangement hold other components in a row or a column, the App Inventor version of Flexbox (Chapter 10).
Good habit: rename every component as soon as you add it. ButtonCheck and LabelResult are much easier to find in the Blocks editor than Button3 and Label7 . Programmers call these meaningful names.
- The Blocks editor is where you make the app do things. It looks very like Scratch: you
drag blocks from drawers on the left and snap them together. Each component you added in the Designer has its own drawer of blocks in three colours:
- Event handlers (gold):
when ButtonCheck.Click do. These start code running when something happens.
- Property getters and setters (green):
set LabelResult.Text to,TextBoxClaim.Text. - Methods (purple): actions a component can perform, such as
call TextToSpeech1.Speakorcall TinyDB1.StoreValue.
The built-in drawers hold general blocks: Control (if, loops), Logic, Math, Text, Lists, Variables and Procedures.
Event-driven programming. Python programs usually run from top to bottom. Apps are different: they sit waiting until something happens, such as a tap, a timer tick, or a message arriving, and then respond. This is event-driven programming, exactly like the hat blocks in Scratch (Book 4). Every piece of code in an App Inventor app lives inside an event handler. The first event in every app is when Screen1.Initialize , which runs once when the app opens.
Programming ideas in blocks. Everything you learned in Python exists in App Inventor, as blocks. In this book, App Inventor scripts are written as text, using the same notation as the Scratch scripts in Book 5: block names in normal text, drop-down choices in [square brackets], values typed or plugged in shown in (round brackets), and blocks inside other blocks indented.
| Python | App Inventor block |
|---|
score = 0 (a global initialize global [score] to (0) variable)
score = score + 1 | set global [score] to ((get global score) + (1)) |
if ... elif ... else if / else if / else (click the blue gear to add branches)
"Hello " + name | join ("Hello ") (TextBoxName.Text) |
claims = ["a", "b"] | make a list ("a") ("b") |
claims.index(x) + 1 index in list thing (x) list (claims) , which gives 0 if not
found
claims[i - 1] | select list item list (claims) index (i) |
def check(): | to procedure [check] do |
for item in claims: | for each [item] in list (claims) |
One important difference: App Inventor lists count from 1, not from 0 like Python. The first item is at index 1. Watch for this whenever you move between the two.
Procedures. A procedure is App Inventor's name for a function (Chapter 6). Use to procedure do for one that just does something, and to procedure result for one that returns a value. Give them parameters with the blue gear. As in Python, they stop you repeating code.
Storing data with TinyDB. When an app closes, its variables are lost, like RAM (Book 6). TinyDB is a non-visible component that stores data permanently on the phone, under a name called a tag, like a key in a Python dictionary (Chapter 7).
call TinyDB1.StoreValue tag ("checks") valueToStore (get global checks)saves a value.call TinyDB1.GetValue tag ("checks") valueIfTagNotThere (0)reads it back, with a default for the first time the app runs, just like.get()with a default.
TinyDB data stays on that one phone. Sharing data between users needs a cloud database such as CloudDB or Firebase, which App Inventor also offers.
Testing on a real phone. App Inventor can run your app live on a phone while you build it.
- A grown-up installs the free MIT AI2 Companion app on an Android phone or tablet (it is also available for iPhone) from its app store.
- The phone and the computer must be on the same Wi-Fi network.
- In App Inventor, click Connect → AI Companion. A six-letter code and a QR code appear.
- In the Companion app on the phone, scan the QR code or type the code.
- Your app appears on the phone. Every change you make in the Designer or Blocks editor appears on the phone within a second or two. This is called live testing.
If you do not have a phone available, Connect → Emulator shows a phone on your computer screen instead (it needs extra software that a grown-up can install from the App Inventor website).
When the app is finished, Build → Android App (.apk) creates an installable file. An APK (Android Package) is the file format for Android apps.
Permissions and privacy. Apps must ask permission before using the camera, microphone, location, contacts or files. Before you tap Allow on any app, ask the questions from Chapter 3 and the CBSE privacy checklist: Why does it need this? Who can see it? How long is it kept? A torch app that wants your contacts is a warning sign. When you build apps, request only the permissions your app really needs.
Publishing. Publishing to the Google Play Store needs a developer account with a fee, and Google requires developers to be adults, so student apps are usually shared as APK files with family, friends or a school, always with a grown-up's permission. Installing an APK from outside the Play Store needs the phone's setting for unknown sources to be allowed, which is also how malware gets onto phones (Book 6), so only ever install APKs from people you know and trust.
Real World: India's government has built many widely used apps: UMANG (hundreds of government services in one app), DigiLocker, mAadhaar, BHIM for UPI payments, and Sachet, which sends official disaster and weather alerts. The Bhashini project (Book 6) helps apps work in many Indian languages.
Career Spotlight: Mobile app developer. Mobile developers build the apps on our phones, from games to banking. They think about small screens, slow networks, battery life and privacy, and test on many different phones. Many started by building a tiny app for a problem they had themselves.

🛠️ Build It
You will build Stop Check Share: type a word from a message you have received, tap Check, and the app tells you whether the Truth Desk has already checked a claim about it. It also counts how many checks you have done, even after the app is closed.
Step 1: Plan. On paper, draw a wireframe of one phone screen with: a title, a text box with a hint, a Check button, a large result label, a "Read it aloud" button, and a small label at the bottom showing "Checks done: 0". Mark which parts the user touches.
Step 2: Start the project.
- Go to ai2.appinventor.mit.edu and sign in (with a grown-up).
- Click Projects → Start new project, and name it
StopCheckShare(no spaces allowed). - Select Screen1 in the Components panel. In Properties, set Title to
Stop Check Share, AppName toStop Check Shareand BackgroundColor to a colour you like.
Step 3: Design the screen. Drag these components onto the Viewer in order, renaming each one (select it and click Rename below the Components tree) and setting its properties:
Component (Palette
| Rename to | Properties to set |
|---|
group)
Label (User Interface) LabelTitle Text: Before you forward, check it. ;
FontSize: 20; FontBold: ticked
TextBox (User TextBoxCla Hint: Type one word from the message ;
| Interface) | im | Width: Fill parent |
Button (User ButtonChec Text: Check ; FontSize: 18; Width: Fill parent
| Interface) | k |
| Label (User Interface) | LabelResul t | Text: (empty); FontSize: 22; FontBold: ticked |
| Button (User Interface) | ButtonSpea k | Text: Read it aloud |
Label (User Interface) LabelCount Text: Checks done: 0 ; FontSize: 12
| TextToSpeech (Media) | Speaker | (non-visible) |
| TinyDB (Storage) | Storage | (non-visible) |
Step 4: Connect a phone. Click Connect → AI Companion and scan the code with the Companion app. Your screen should appear on the phone. Keep it connected while you build.
Step 5: Program it. Click Blocks. Build these scripts. (Look in the drawers: Variables for initialize global , Lists for make a list , Control for if , Text for join and "" , and each component's own drawer for its blocks.)
First, three global variables: the claims the Truth Desk has checked, their verdicts in the same order, and the count.
initialize global [claims] to (make a list ("guardian") ("exam") ("echo")
("water"))
initialize global [verdicts] to (make a list ("FALSE: the photo is fake")
("TRUE: results on Monday") ("FALSE: it is a scam") ("FALSE: the water is
safe"))
initialize global [checks] to (0)
When the app opens, load the saved count:
when [Screen1].Initialize
set global [checks] to (call [Storage].GetValue tag ("checks")
valueIfTagNotThere (0))
set [LabelCount].Text to (join ("Checks done: ") (get global checks))
A procedure that looks up the claim and shows the result:
to procedure [checkClaim]
initialize local [position] to (index in list thing (downcase (trim
(TextBoxClaim.Text))) list (get global claims))
in
if ((get position) > (0))
then
set [LabelResult].Text to (select list item list (get global
verdicts) index (get position))
else
set [LabelResult].Text to ("Not checked yet. Don't forward it.
Report it to the Truth Desk.")
( trim and downcase are in the Text drawer. They work like .strip() and .lower() in Python, so Guardian still matches guardian . initialize local ... in is in the Variables drawer.)
When Check is tapped, run the procedure, add one to the count, save it and show it:
when [ButtonCheck].Click
call [checkClaim]
set global [checks] to ((get global checks) + (1))
call [Storage].StoreValue tag ("checks") valueToStore (get global
checks)
set [LabelCount].Text to (join ("Checks done: ") (get global checks))
When Read it aloud is tapped, speak the result:
when [ButtonSpeak].Click call [Speaker].Speak message (LabelResult.Text)
Step 6: Test. On the phone, try: guardian , GUARDIAN , exam (with spaces), rainbow (not in the list), and an empty box. Write down what happens each time. Close the app completely and reopen it: is the count still there? That is TinyDB at work.
Step 7: Improve it. Choose two:
- If the text box is empty, show "Please type a word first" and do not add to the count. (Hint:
is emptyin the Text drawer.) - Colour the result: set
LabelResult.TextColorto red when the verdict starts with FALSE and green when it starts with TRUE. (Hint: the Text drawer'scontainsblock, or compare the first five characters usingsegment.) - Add a third list of source links, and a button that opens the source. (Hint: an ActivityStarter component, or just show the link in a label.)
- Add a
Clockcomponent and a "Pause 10 seconds before sharing" button that counts down from 10 in a label, one second at a time, usingwhen Clock1.Timer.
Step 8: Build and share. With a grown-up, click Build → Android App (.apk) and install it on a family phone. Ask two people to use it without any help from you, and watch where they get confused. Write down one change you would make.
Unplugged: Event cards. One person is the app; they hold a set of cards, each with an event on the front ( when ButtonCheck.Click , when Screen1.Initialize , when Clock1.Timer ) and the actions on the back. The rest of the group are "users" and the "phone": they cause events in any order ("I tap Check!", "One second passes!", "The app opens!"). The app must find the right card and act out the back. Notice that the app never knows what will happen next. That is event-driven programming.
✅ Case Closed
By the evening, Stop Check Share was on four hundred phones across Memunic. Luna had shared it with every family in Moonbrook. Hop had shown it to his grandmother, who used it eleven times before dinner and then started showing it to her friends.
The number that mattered most was one that Frost could see at the hub: the clips were still being forwarded, but far more slowly. People were pausing. Two seconds of checking was enough to break the chain.
But the app could only check claims the Truth Desk had already investigated. For the videos, the honest verdict was still "Not checked yet". Nobody had proved they were fake.
That night Memoon played one of the clips over and over, frame by frame. Simbha's mouth opened. His mane rippled as he turned. His eyes blinked. On the twelfth viewing she noticed something: just as the clip began, for a fraction of a second, the edge of his mane seemed to flicker and slide, as if it was not quite attached to his head.
"Nimo," she said, "how many pictures make one second of video?"
❓ Test Yourself
- Name three abilities phones have that most desktop computers do not, and one app that uses each.
- What is the difference between a native app, a web app and a hybrid app?
- List the six stages of the app development life cycle. Why does it repeat?
- What is a wireframe, and why do designers make one before building?
- What is the difference between the Designer and the Blocks editor in App Inventor?
- Sort into visible and non-visible components: Button, Clock, TinyDB, Label, TextToSpeech, Image, TextBox, Notifier.
- What is event-driven programming? Name two events that could start code in an app.
- In App Inventor, what index does the first item in a list have? How is that different from Python?
- What does TinyDB do? Why is it needed if the app already has variables?
- How do you test an App Inventor app on a real phone while you build it?
- Before allowing an app to use your location, what three questions should you ask?
- Challenge: Design (on paper) an app that helps your class remember homework. Draw a wireframe, list every component with a meaningful name, and write the event-handler scripts in block notation. Which data must be saved with TinyDB?
📚 Key Terms
- Mobile app: an application designed for phones and tablets.
- Native app: an app built for one operating system with its own tools.
- Web app: a website that behaves like an app in a browser.
- Hybrid (cross-platform) app: an app written once to run on several operating systems.
- App store: an online shop for downloading apps, such as Google Play.
- App development life cycle: the stages of planning, building, testing, releasing and maintaining an app.
- Wireframe: a simple sketch of a screen's layout.
- MIT App Inventor: a free block-based tool for building mobile apps.
- Designer / Blocks editor: App Inventor's view for building the screen / for programming behaviour.
- Component: a building block of an App Inventor app.
- Visible / non-visible component: one that appears on the screen / one that works behind the scenes.
- Property: a setting of a component, such as its text or colour.
- Event handler: code that runs when a particular event happens.
- Event-driven programming: programming in which code runs in response to events.
- Procedure: App Inventor's name for a function.
- TinyDB: an App Inventor component that stores data permanently on the device.
- Tag: the name under which TinyDB stores a value.
- AI Companion: the app that runs App Inventor projects live on a phone.
- Live testing: seeing changes to an app immediately on a real device.
- APK: the installable file format for Android apps.
- App permission: an app's request to use a phone feature such as the camera or location.
Keep learning with Byte by Byte
Explore Book 7: Networks, Programming and Machine Learning. Find the ebook, purchase details and teaching slides on MemuniCity.
Comments
Post a Comment