









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










App Inventor lets you develop applications for Android phones using a web browser and either a connected phone or an on-screen phone emulator. The MIT App Inventor servers store your work and help you keep track of your projects. You build apps by working with:
The App Inventor development environment is supported for Mac OS X, GNU/Linux, and Windows operating systems, and several popular Android phone models. Applications created with App Inventor can be installed on any Android phone. Before you can use App Inventor, you need to set up your computer and install the App Inventor Setup package on your computer. See: http://explore.appinventor.mit.edu/content/setup
You can build many different types of apps with App Inventor. Often people begin by building games like MoleMash or games that let you draw funny pictures on your friend's faces. You can even make use of the phone's sensors to move a ball through a maze based on tilting the phone. But app building is not limited to simple games. You can also build apps that inform and educate. You can create a quiz app to help you and your classmates study for a test. With Android's text-to- speech capabilities, you can even have the phone ask the questions aloud. To use App Inventor, you do not need to be a professional developer. This is because instead of writing code, you visually design the way the app looks and use blocks to specify the app's behavior.
Most event handlers are in green color and stored at the top part of each drawer. Here are the example of event handlers. When an event occurs on a phone, the corresponding event handler is said to fire, which means it is executed. Commands and Expressions When an event handler fires, it executes a sequence of commands in its body. A command is a block that specifies an action to be performed on the phone (e.g., playing sounds). Most command blocks are in purple or blue color. Here are some sample commands available in HelloPurr: Some commands require one or more input values (also known as parameters or arguments) to completely specify their action. For example, call Sound1.Vibrate needs to know the number of milliseconds to vibrate, set Label1.BackgroundColor needs to know the new background color of the label, and set Label1.text needs to know the new text string for the label. The need for input values is shown by sockets on the right edge of the command. These sockets can be filled with expressions, blocks that denote a value. Expression blocks have leftward-pointing plugs that you can imagine transmit the value to the socket. Larger expressions can be built out of simpler ones by horizontal composition. E.g., all of the following expressions denote the number 500:
Commands are shaped so that they naturally compose vertically into a command stack, which is just one big command built out of smaller ones. Here's a stack with four commands: When this stack of commands are placed in a body of an event handler (e.g., the when.Button1.Click event handler), the command will be executed from the top to the bottom. If the stack of command above is executed, then the phone will first play the sound, then vibrate, then change the label's color to be green, and then label will show the text "CS117 rocks!" However, the execution works very fast: you would see all the actions happen at the same time. Control Flow When an event handler fires, you can imagine that it creates a karaoke-like control dot that flows through the command stack in its body. The control dot moves from the top of the stack to the bottom, and when it reaches a command, that command is executed -- i.e, the action of that command is performed. Thinking about control "flowing" through a program will help us understand its behavior. The order of the commands, or the control flow is important when you make an app. You need to make sure which action should come first.
Getter blocks Setter blocks Getter blocks are expressions that get the current value of the property. Setter blocks are commands that change the value associated with the property. Some Label properties cannot be manipulated by blocks. Which ones? As an example of manipulating Label properties, open the LabelSize program, which has 4 event handlers. What do these do?
Predict what happens if we change the when TallerButton.Click handler as follows: Modification 1: Modification 2: Modification 3:
This app illustrates the two kinds of event handlers for canvases. The first is the when Touched handler: Notes: Canvas1.DrawCircle draws a filled-in circle in the current paint color of the canvas (which is set to red by set Canvas1.PaintColor). The first two arguments of Canvas1.DrawCircle (annotated x and y) are the x and y coordinates of the upper left corner of the square enclosing the circle. X coordinates grow from 0 (left edge) rightward. Y coordinates grow from 0 (top edge) downward. Coordinates are measure in pixels. Get a sense for the coordinate system by touching the canvas in different spots and observing the x and y coordinates that are reported above the canvas in the CanvasTest2 app. The last argument of Canvas1.DrawCircle (annotated r) is the radius of the circle. Although RadiusLabel is technically a text string, App Inventor automatically treats any string of digits as a number. The three occurrences of each of x and y have different meanings:
be used whereever a value is expected inside the body of the event handler or procedure in which it was defined. However, any attempt to use this value block in a different event handler will result in marking the block with a yellow exclamation point, indicating an error (try it and see).
current value in a global variable within the BlocksEditor window by right-clicking (Control-clicking on a Mac) on a global variable block and selecting Watch. Rather than using RadiusLabel or the global variable radius, a third possibility is to use the LineWidth property of Canvas1. How would we change the CanvasTest2 program to do this? Clocks and Timer Events Begin by uploading the TimerTest app attached at the bottom of these notes. The Clock component has a timer feature that can perform actions at a specified rate. When a Clock's TimerEnabled property is set to true, its when ... Timer event will fire every TimerInterval milliseconds. For example, in TimerTest, clicking the BellButton enables the BellClock's timer, and that timer plays a sound and vibrates the phone. The TimerInterval in this case is controlled by the value (1000 milliseconds = 1 second) specified in the Properties pane fo the BellClock. The timer can be disabled by setting the TimerEnabled property to false. In TimerTest, the StopButton disables all timers: Balls, Sprites, and Simple Animations A sprite is an creature that can move around the canvas in which it lives. In App Inventor, a ball is a simple sprite that appears as a circle.
We can get the effect of an inflating red balloon by having a red ball whose radius grows every time an associated timer fires: We can specify that the balloon "pops" when it hits the edge of the canvas by resetting it to its initial radius. We can add a bird sprite that "flies" from the lower left to upper right corner of the canvas. Here are the blocks that specify the flight: When the bird hits an edge, we reset it to its initial position: