Learning MIT AppInventor PDF
Learning MIT AppInventor PDF
App Inventor
Learning MIT
App Inventor
A Hands-On Guide to Building
Your Own Android Apps
Derek Walter
Mark Sherman
Editor-in-Chief
Mark Taub
Executive Editor
Laura Lewin
Senior Development
Editor
Chris Zahn
Managing Editor
Kristy Hart
Project Editor
Elaine Wiley
Copy Editor
Krista Hansing
Editorial Services, Inc.
Indexer
Lisa Stumpf
Proofreader
Debbie Williams
Technical Reviewers
Tom Stokke
Arta Szathma
Janet BrownSederberg
Editorial Assistant
Olivia Basegio
Cover Designer
Chuti Prasertsith
Compositor
Nonie Ratcliff
from Derek
This book is dedicated to my incredible wife, Candy.
from Mark
This book is dedicated to Stacy (depending on what she says).
Contents
Table of Contents
Preface xiv
1 An Introduction to Programming
Operating Systems 2
User Interface 4
Android Strengths 6
Extending App Capabilities 8
Google Services 9
Applications 10
Programming Languages 11
Summary
13
Exercises 14
17
20
21
23
25
25
29
31
31
31
32
Android Quiz 32
Uploading to Google Play 32
Summary
33
Exercises 34
26
27
23
vii
viii
Contents
35
37
Viewer
44
Components 45
Properties 46
Media
46
54
Exercises 54
4 Variables
55
60
Programming Blocks 62
Additional Exercises 64
Scope: Global and Local Variables 64
Global Variables 66
Example App: Up/Down Counter 67
Local Variables
68
72
5 Procedures
75
What Is a Procedure? 75
Types of Procedures 76
Why Use Procedures? 79
Arguments 79
72
69
Contents
Exercise: Flick 81
Additional Exercises 83
Summary
84
85
112
113
113
ImageSprite 114
Ball
115
Canvas
116
123
ix
Contents
125
136
9 Using Media
Audio
139
140
Images
141
The ImagePicker
The Camera
Video
141
144
145
10 Sensors
147
149
150
152
153
155
158
161
160
159
Contents
172
11 Databases
TinyDB
173
174
181
182
187
12 Distributing an App
Live Mode
189
189
Security Settings
190
191
200
200
Index
205
207
196
198
201
192
xi
Acknowledgments
from Derek
I want to thank my amazing wife, Candy, who supported me during the crucible of writing a
book. Your strength and encouragement kept me going through the late nights, exhausting
weekends, and challenges that came with this project.
I would like to thank the MIT App Inventor team members for their support and for continuing
such an important project that is democratizing computer programming. Also, thank you to
Laura Lewin and the Pearson team for their guidance with this first-time author.
from Mark
I want to thank the AI team at MIT, the AI Master Trainers program organizers, Lyn Turbak,
and especially Fred Martin. All of you helped me get to this point, and all of this accrued
knowledge is thanks to you.
I want to thank my close friends, all of whom endured my writing and working through many
events, and often were kind enough to pull the laptop off my sleeping face. I appreciate it. I
especially want to thank Stacy for taking care of me every step of the way. Stacy, will you
marry me?
Preface
The smartphone is the ultimate personal computer. Mobile devices are always with us and
have become an essential part of personal productivity and lifestyle needs. We use them for
messaging, social media, Google searches, games, picture taking, and, of course, phone calls.
The Android operating system powers most of the worlds smartphones, bringing an extensive
app catalog to these devices. According to Google, more than 1 billion active devices are
currently running Android.
Perhaps you have reached the point at which using mobile apps on your smartphone isnt
enoughit is time to create one. You might just want to tinker and program a simple app, or
maybe you have thought of a new concept that doesnt exist yet. Whatever the case, MIT App
Inventor is an excellent place to start.
App Inventor is an easy-to-use tool for building both simple and complex Android applications.
The apps can easily be ported to your phone, shared with others, or even sent to the Google
Play Store for distribution to all Android devices worldwide.
For those looking to learn a programming language, MIT App Inventor can serve as an
excellent bridge to acquiring more complex coding skills. Instead of presenting new users with
frustrating messages and unfamiliar commands, App Inventor has a visually friendly interface
that uses the methods of dragging, dropping, and connecting puzzle pieces to program
applications (see Figure P.1).
Figure P.1
The MIT App Inventor Blocks Editor. The visual programming is designed to help
beginners learn the ropes of building mobile applications.
Preface
Even though App Inventor does not require using code, it builds on the same kinds of
principles that successful programmers need to write good applications. Whether you go no
further with programming or you use App Inventor to launch a new career, using App Inventor
can be a highly engaging and challenging experience. Additionally, the open and flexible
nature of Android makes it the perfect place to start.
Figure P.2
The MIT App Inventor home page. This is where the app-creation journey begins.
App Inventor is actively managed and developed by MITs Mobile Learning Lab (the project
was originally built by Google). App Inventor is growing in popularity among educators as a
way to introduce those with no programming experience to the principles of computer science
and app development. It also serves as a great first step for those dabbling with programming
or looking to increase their knowledge of how smartphone apps work.
xv
xvi
Preface
The work takes place in two key sections of App Inventor: the Designer and the Blocks Editor.
In the Designer, you decide what actions the app will perform and how it will look (see
Figure P.3).
Figure P.3
In the App Inventor Designer, you design the app and add its key functionality.
The programming takes place in the Blocks Editor. There you tell the app what it should do
and give specific instructions for making that happen.
The specific capabilities are programmed through connecting puzzle pieces. Over time, you will
learn what each block does and find multiple ways for them to interact with one another. The
pieces that do not interact will not connect with each otheranother helpful way for beginners
to get a sense of introductory programming principles.
MIT released App Inventor 2 in December 2013, creating a more powerful and easier-to-use
tool. The most significant improvement is that all the work takes place within the browser (the
previous version required a software download for some of the capabilities).
This improvement most impacts the onscreen emulator, which places a virtual Android device
screen on your computer. Using this emulator provides a perspective on how the app will look
and function when put to use. This is especially useful for those without an Android device or
anyone in an education setting who wants to monitor student progress by viewing app builds
on computer screens.
App Inventor also offers a method for using the app in real time while performing work on it:
the AI Companion app (see Figure P.4). With this free download from Google Play, you can see
Preface
your app change and develop while working on it. The Companion app also works wirelessly,
so you dont need to physically connect your phone to a computer while working in App
Inventor.
Figure P.4
An App Inventor app as viewed through the AI2 Companion. This lets you see both
how the app is performing and how it looks while it is still being developed.
Why Android?
Android is not only the most popular operating systemit also is the most extendable. It is
found on a wide variety of flagship devices from major handset makers, such as Samsung, HTC,
LG, and Motorola. App Inventor is built to take advantage of the customization and flexibility
that Android offers.
App Inventor is also a tool that is designed with those who have little to no programming
experience in mind. Other platforms have a fairly high barrier of entry, but with App Inventor,
you can more easily learn the essential skills for building an app with the worlds most popular
mobile platform.
xvii
xviii
Preface
Although many apps you create are likely to be used for practice or to share with others (see
Figure P.5), MIT App Inventor is capable of creating apps that can be uploaded for distribution
in the Google Play Store. With only a one-time fee of $25, anyone can put his or her skill set to
work and become a registered Android developer. Chapter 12, Distributing an App, discusses
this process and walks through how to accomplish it.
Figure P.5
Preface
Chapter 4: Variables
Variables are one of the key pieces of App Inventor; almost any app that you build will use
them in some way. Chapter 4 covers the essentials of variables and provides some strategies for
their effective use, particularly in the context of building the sample app for the chapter.
Chapter 5: Procedures
With some basic app building under your belt, it is time to take the next step and use
procedures. Procedures make your life easier when it comes to building larger, more
complicated apps because they enable you to group pieces of code together and recall it later.
xix
xx
Preface
Preface
Next Steps
Using App Inventor is an excellent way to build an Android app (see Figure P.6). As with many
other skills in computer science, building a mobile application is an exercise in both creativity
and logical thinking. You need to solve rational, complex problems while simultaneously
building out a creative vision. Although you can learn App Inventors basics rather quickly, you
can build more powerful and complex applications with additional time and practice.
Figure P.6
Chapter 1 begins with some essential computer science principles. Understanding how an
operating system functions and what developers are actually doing when building software will
give you a solid foundation in effective programming. With this established, you will be ready
to build a variety of sample Android apps as you follow along in the book and then extend this
skill set to your own Android apps.
xxi
2
Building with
MIT App Inventor
Understanding the functionality of an app is only one part of programming. The programmer
also has to focus on specific features and how to implement them.
Before the serious work of building apps begins, a brief overview of how applications perform
is useful. Lets get beyond the pretty screen and graphics that you interact with and start to
look at what is really happening and how to make an app perform the way you envision. After
doing this, you will be able to understand how apps can request information, pull in data from
the Internet, and interact with other applications.
18
Chapter 2
Figure 2.1
Signing In
To begin a session with App Inventor, click the Create button at the top of the home page (see
Figure 2.2).
Figure 2.2
Next, App Inventor asks permission to connect to your Google account. This can be a personal
Google account (one that ends with an @gmail.com address) or a Google apps account
managed by a university, business, or other type of organization (see Figure 2.3).
Figure 2.3
After signing in with your Google account, you must authorize App Inventor to access your
Google account so that it can verify your login information. If you select Remember This
Approval for the Next 30 Days, then you will not need to repeat this step when you return to
work on apps (see Figure 2.4). At the end of the 30 days, you simply need to reauthorize access.
Figure 2.4
19
20
Chapter 2
The next screen is the file system where App Inventor projects are stored. As you create more
projects over time, you can find them there, just as a folder on your computer holds a list of all
your documents saved to that location.
Next, click New Project and then type CatWatch into the box (spaces are not allowed). This
takes you to the Designer screen.
Designer
App building begins in the Designer. Here you create the user interface, or the look and feel
of the app. You also add the components needed to receive input from the user, as well as the
components needed to display output or information to the user. The Designer also is where
you specify which nonvisible components the app will use, such as the dialer, GPS, or SMS.
Notice that because we are in Designer, the Designer button in Figure 2.5 is slightly grayed out
in the top-right corner of the screen. This button, along with the one next to it, labeled Blocks,
indicates which editor you are using.
The left side of the screen features the Palette (see Figure 2.5), which, as the name implies, is
the space for all the creation tools (the next chapter details the full suite).
Figure 2.5
Blocks Editor
The Blocks Editor is where you will be programming an apps behavior (see Figure 2.6). Here
you will add the commands that do the work of the app. As just noted, you access it from the
Blocks button at the top right.
Figure 2.6
MIT App Inventor uses the metaphor of drawers containing puzzle pieces for programming.
Each item in the Blocks palette under Built-in is considered a drawer. The drawers contain the
puzzle-looking pieces. The programming is accomplished by connecting the puzzle-looking
pieces. Despite its seeming simplicity, App Inventor has many powerful capabilities that enable
the user to build complex applications.
To better understand what programming an app entails, it is useful to understand what is going
on inside an application.
21
22
Chapter 2
Figure 2.7
Figure 2.8
You can then type in a six-digit code or scan the QR code with your device (see Figure 2.9),
using the App Inventor app. Doing so brings up a live view of your app. As you add elements
to it with the MIT App Inventor software, those changes are reflected in real time on your
device.
Figure 2.9
23
24
Chapter 2
Figure 2.10
Windows PC.
Figure 2.11
Event Handlers
All of your blocks, the pieces that make your app perform tasks, will be connected with an
event handler. Events are created whenever something in the real world happens to the app,
such as when the user clicks a button, the phones location changes, or the phone receives a
text message. Blocks exist for just about everything you want to do in an app; taking a picture,
checking the GPS location, displaying text, changing the color of a component, finding out
what the user entered into a text box, and so on. You can add (or remove) these blocks from an
event handler, allowing the programming to determine the precise set of actions to take when
the user presses a button.
When any event happens, App Inventor runs whatever blocks are inside the event handler
block for that event. For example, Figure 2.12 shows a button labeled Speak; the block when
Speak.Click do is the event handler for when that button is clicked.
Imagine that we wanted to write an app to speak written text. We would need a button to
start the process. To do this, we would have to drag the action block into the event handler.
Whenever the button is clicked, that block will be called, and the device will move to whatever blocks have been placed inside the event handler. The event handler will grow and shrink
as needed to accommodate whatever blocks are placed inside it (in Figure 2.13, this involves
speaking some text). The way it is right now, however, the app has a button and a text-tospeech component, but it wont do anything because the event handler is empty. After we drag
in some action blocks, it will do those actions whenever the button is clicked. Note that the
component used to initiate the action (such as a button) is usually different than the component that does the requested action (such as taking a picture or sending a text message).
25
26
Chapter 2
Figure 2.12
Figure 2.13
An event handler.
Events that take place while a handler is already running are put in a queue and will run when
it is their turn. Most event handlers run much faster than events are generated, so this is often
not an issue. The most common actions, such as updating the text in a label or looking at the
state of a check box, occur nearly instantly. However, other actions, such as working through
a large collection of data, might take a long time, and the app will appear frozen until the
process finishes.
While an event handler is running, the display isnt updated. Again, this isnt an issue most of
the time, but if you have an event handler that is taking long enough for a user to notice, the
display will appear frozen until the event handler is complete.
You might notice that some features in App Inventor take time, such as playing sounds or
music. Other features have to wait for something on the Internet to respond, which can cause
an unpredictable amount of delay time. But the display doesnt freeze when you play music,
and the app continues to work while waiting for a web page to load. Android provides the
means to allow some tasks, such as playing music, to run in the background without disrupting
the normal actions your app performs. Android provides other means of dealing with actions
that could take a long time, or that might never finish, such as loading a web page or waiting
for the user to take a picture, without affecting the normal functioning of your app. The
actions for music and sound are made possible by the Android system. Your app simply hands
off the sound file to the phones operating system and tells the phone to handle it. The music
plays while the app continues to work.
Later in the text, we provide more details on how App Inventor switches tasks. You can use
that knowledge to make better apps.
27
28
Chapter 2
Figure 2.14
The My Projects button is for creating new projects and accessing existing ones.
Figure 2.15
4. The button shows with the following default text: Text for Button1. Lets change the
name of the button. Click the label in either the Viewer or the Components tree and
then click the Rename button. In the box that launches, give it another name, such as
Meow.
5. Notice that changing the name of the button does not change the buttons text. To
change the displayed text, click the button and then find the box labeled Text inside
the Properties pane. Then highlight the text and type Meow. The text will change in the
Viewer.
Adding an Image
Images are an effective way to add some visual polish to an app. Next, we insert an image into
the app.
1. If you have not done so already, download the CatIsWatching image from the books
InformIT page.
2. Find the box labeled Media, which is just below the Components box. Click the Upload
File button (see Figure 2.16) and then upload the CatIsWatching file. This adds the image
to the app, making it available to any component that uses the image. Note that the
filename appears in the media box.
Figure 2.16
Uploading an image.
3. Click User Interface in the Palette. Then click and hold the image and drag it onto the
Android home screen in the Viewer. A blue bar shows where you can place the image.
By moving the mouse, you can place the image above or below the Button. When
the blue bar is below the Button, drop the image. Next click the Image1 button in the
Components box. The Properties pane updates to display the properties associated with
the image.
29
30
Chapter 2
4. Click the box labeled Picture. All available images in your app are listed; select the
CatIsWatching image. Click OK to see the image appear in the Viewer (see Figure 2.17).
Figure 2.17
Uploading an image.
5. The image of Sherlock the cat needs to be tweaked to properly fill the screen. Click in the
width box and then click the box that reads Pixels. Type 320 pixels.
6. The procedure is the same for the height: Click the height box and then type 400 pixels.
7. This app requires one more element. From User Interface, click and drag the Label
component. Again, watching the blue insertion bar, drop the Label between the button
and the Image components. Next, select Label1 in the Components box. Click the button
underneath the BackgroundColor label, which is currently set to None. Then choose
Green.
8. It is time to write some text inside the box. Go to the Text box and type Sherlock is
watching. Choose TextAlignment and change this selection to Center. Notice that
this does not change the position of the labelit changes only the text inside it (see
Figure 2.18).
9. Click Blocks in the upper-right corner. You should see Meow, Label1, and Image1
underneath Screen1. This is where you would drag these components into the Blocks
Editor for further programming.
This first app is relatively simple, but it should make you feel more familiar with the core pieces
of App Inventor. Later apps will make more extensive use of the interface elements and how
they can be programmed. We will also explore how to put this app on your own device and
interact with it.
Figure 2.18
Speak, Android!
Give your Android device a voice. This simple app (see Figure 2.19) teaches you how to enable
an image to respond to touch and speak on command. You can also use it to explore other
ways to work with images.
Pollock
Named after Jackson Pollock, the American artist who helped popularize abstract art, this app
turns an Android device into a canvas for color (see Figure 2.20). You will learn how to turn
buttons into paint and use the Canvas component.
31
32
Chapter 2
Figure 2.19
Figure 2.20
Fore
MIT App Inventor has some surprisingly powerful tools for creating games. Various motionenabled commands enable you to create some powerful games. The game you will be building
will show how you can use the canvas and various sprites to create a game field and objects
that can be manipulated while playing (see Figure 2.21).
Android Quiz
Games can be fun to create, but imagine being able to use an app for your own productivity.
Android Quiz (see Figure 2.22) demonstrates that you can create an actual assessment app.
Summary
Figure 2.21
Figure 2.22
Android Quiz.
Summary
MIT App Inventor is a powerful tool that beginning coders or anyone dabbling with mobile
technology can use to build Android apps. In this chapter, we looked at the kind of apps that
are possible with this cloud-based tool.
Computer science is applied reasoning using both art and science. It requires the ability to
communicate ideas through a combination of language and powerful technology. Hopefully
this first app has demonstrated that App Inventor is an excellent starting point for anyone
looking to create with computer technology, whether professionally or recreationally.
In the next chapter, we build an app that includes both an image and sound, and we look at
how to see the app on your own device.
33
34
Chapter 2
Exercises
1. Add another button to the Viewer. Change the text, trying different configurations to see
how they fit in the Viewer.
2. Upload a different image. Download one from the Web or upload an image on your
computer. Try different configurations for size in relation to the rest of the app. Decide
on the optimal size for images in the context of the rest of the content.
3. Change the text and color scheme in the label. Try labels in different locations of the
app. Pay attention to how the Designer works in terms of the placement of components
and how you can customize the components.
Index
A
abstraction
lists, 98-99
procedures, 98-99
acceleration, 158
accelerometer, 41
Accelerometer Sensor, 158-160
accelerometers, 158
detecting tilt, 159-160
Action, 154
actions, 27
Activity Starter, 43, 154
adding
animations, 113-114
ball, 116
canvas, 116
ImageSprite, 114-116
images, 29-31
ball, 116
canvas, 116
ImageSprite, 114-116
208
animations
examples, 117-118
Fore, 119-123
additional exercises, 64
downloading
interfaces, 61-62
to computers, 192-196
WriteMore, 182-186
apps, 10-12
Android Quiz, 33
building
distributing
scorekeeping, 122-123
location-aware apps
component properties
Blocks Editor
event handlers, 26
scorekeeping, 122-123
BluetoothClient, 44
location-aware apps
BluetoothServer, 44
built-in variables, 56
interfaces, 61-62
extensions, 58
getters, 57-58
setters, 57-58
Camera, 39
canvas, 41, 59
animations, 116
WriteMore, 182-186
extensions, 58
arguments, 79-80
audio, 140-141
B
backing up your work, 133-134
ball, 41, 116
animations, exercises, 117-118
209
210
components
components, 35
Designer, 36
Components, 45-46
computer science, 33
Media box, 47
connectivity components, 43
Contact Picker, 42
layout, 38-39
media, 39-40
sensors, 41
D
data
social components, 42
storage components, 43
User Interface element, 37-38
lists, 85
location, 152-153
Viewer, 45
databases
do block, 79
DataUri, 154
Do It, 131-132
debugging, 130
do procedure, 77
commenting, 130
Do It, 131-132
names, 132-133
testing, 131
E
edges, animations, 119
Email Picker, 42
exercises
Fore, 119-123
FusionTables, 177-180
lists, 112
blocks, 178-179
media, 146
additional exercises, 64
interfaces, 61-62
multiple screens, 134-136
Get, 181-182
procedures, 83
getters, 57-58
WriteMore, 182-186
211
212
ListPicker, 89-91
lists, 85, 87
gravity, 159
abstraction, 98-99
colors, 92
creating
HasAccuracy, 166
headings, ImageSprite, 115
home screen launchers, 6
I
Image, User Interface element, Palette
(Designer), 38
ImagePicker, 40, 141-144
images, 141
adding, 29-31
ListPicker, 89-91
ImagePicker, 141-144
L
Label, User Interface element, Palette
(Designer), 38
languages, programming languages, 12-13
launchers
home screen launchers, 6
switching between, 6
location scanners, 41
Location Sensor, 151-152, 165
location-aware apps
GPS (Global Positioning System),
151-152
saving, 155-158
Maps app, intents, 153-155
LocationChanged, 152-153
M
Maps app, intents, 153-155
N
names, 132-133
naming, projects, 36
near field, 41
Notifier, User Interface element, Palette
(Designer), 38
media
audio, 140-141
exercises, 146
P
Palette (Designer), 37
connectivity components, 43
drawing and animation, 40-41
layout, 38-39
LEGO MINDSTORMS, 44-45
media, 39-40
sensors, 41
social components, 42
storage components, 43
User Interface element, 37-38
Phone Call, 42
exercises, 136
213
214
pinning
pinning
locations, Pushpin app, 170-171
locations to remember later,
Pushpin app, 168
PUT, 182
additional exercises, 64
interfaces, 61-62
POST, 182
abstraction, 98-99
arguments, 79-80
defined, 75-76
exercises, 83
properties, 56
component properties, built-in
variables. See built-in variables
saving
checkpoints, 133-134
location data, 155-158
types of
sensors, 149-150
Accelerometer Sensor, 158-160
accelerometers, 158
sprites, 114
ball, 116
ImageSprite, 114-116
StatusChanged, 152
Palette (Designer), 41
strengths of Android, 7
setters, 57-58
Sharing, 42
switching
between launchers, 6
screens, 128-129
testing, 131
skin, 6
Text to Speech, 40
Texting, 42
social components, 42
Sound, 140-141
sound, 40
Sound Recorder, 40
Speak, Android!, 31-32, 47-53
connecting your device, 48-50
seeing your app on connected
devices, 50-53
Twitter, 42
types of
Speech Recognizer, 40
procedures, 76-78
215
216
UI (user interfaces)
W-X
V
variables, 55-56
Y-Z
Yandex Translate, 40