Mobile apps with App Inventor
Doodle Pad
Make a drawing app: drag a finger to draw, tap for dots, pick a colour, and wipe it clean.
Today's app turns the phone into a sketch pad. Drag your finger to draw, tap to make dots, choose red, blue or green, and clear the screen to start again. It takes only six events.
You will learn
- Arrange components in a row
- Rename components so blocks are easy to read
- Use an event's own information, such as where a finger is
- Draw lines and circles on a Canvas
You will need
- A computer with a web browser
- A phone or tablet with the MIT AI2 Companion app
- Days 1 and 2, for the Designer and blocks
Words to know
- Canvas
- A component to draw on, with lines, circles and text.
- parameter
- Information an event gives your blocks, such as the finger's currentX.
- arrangement
- A Layout component that puts things side by side or one above another.
- PaintColor
- The colour the Canvas draws with.
Step by step
Step 1: A row for the buttons
Start a new project called
DoodlePadand set Screen1's Title toDoodle Pad. From the Layout drawer, drag a HorizontalArrangement onto the phone. It is a row: whatever you put in it sits side by side. Set its Width to Fill parent and AlignHorizontal to Center.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceLayoutHorizontalArrangement?HorizontalScrollArrangement?TableArrangement?VerticalArrangement?VerticalScrollArrangement?MediaDrawing and AnimationMapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Doodle PadComponentsScreen1HorizontalArrangement1RenameDeletePropertiesHorizontalArrangement1 (HorizontalArrangement)AlignHorizontalCenter : 3AlignVerticalTop : 1BackgroundColorDefaultHeightAutomaticWidthFill parentImageNone...VisibleA HorizontalArrangement across the top of the screen. Step 2: Four buttons, with names
Drag four Buttons into the arrangement, side by side. A good name tells you what a component is for, so rename each one: choose it in Components, click Rename and type its new name. Call them
RedButton,BlueButton,GreenButtonandClearButton. Set each one's Text, and give the colour buttons their own BackgroundColor with white TextColor.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:41Doodle PadComponentsScreen1HorizontalArrangement1RedButtonBlueButtonGreenButtonClearButtonRenameDeletePropertiesClearButton (Button)BackgroundColorDefaultEnabledFontBoldFontItalicFontSize14.0FontTypefacedefaultHeightAutomaticWidthAutomaticImageNone...ShapedefaultShowFeedbackTextClearTextAlignmentcenter : 1TextColorDefaultVisibleFour buttons in a row, renamed. Step 3: A canvas to draw on
From Drawing and Animation, drag a Canvas under the buttons. Set its Width and Height to Fill parent, so it covers the rest of the screen, and its LineWidth to
6, for a thick pen.App InventorMy ProjectsGuidePaletteSearch Components...User InterfaceLayoutMediaDrawing and AnimationBall?Canvas?ImageSprite?MapsSensorsSocialStorageConnectivityLEGO® MINDSTORMS®ExperimentalExtensionViewerDisplay hidden components in Viewer9:41Doodle PadComponentsScreen1HorizontalArrangement1RedButtonBlueButtonGreenButtonClearButtonCanvas1RenameDeletePropertiesCanvas1 (Canvas)BackgroundColorWhiteBackgroundImageNone...FontSize14.0HeightFill parentWidthFill parentLineWidth6.0PaintColorBlackTapThreshold15TextAlignmentcenter : 1VisibleA Canvas filling the rest of the screen. Step 4: Draw with your finger
Go to Blocks. From Canvas1, drag when Canvas1.Dragged. As your finger moves, this event happens again and again, and each time it tells you where the finger was a moment ago (prevX, prevY) and where it is now (currentX, currentY).
Drag call Canvas1.DrawLine inside it. To get the get prevX block, point at prevX on the event block and drag the orange block that appears. Fill all four sockets: a short line from where the finger was to where it is. Thousands of short lines make a smooth drawing.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1HorizontalArrangement1RedButtonBlueButtonGreenButtonClearButtonCanvas1Any componentRenameDeleteShow Warningswhen Canvas1.DraggedstartXstartYprevXprevYcurrentXcurrentYdraggedAnySpritedocall Canvas1.DrawLinex1get prevXy1get prevYx2get currentXy2get currentYDraw a line from the finger's last position to where it is now. Step 5: Choose a colour
Each colour button changes the pen's colour, the Canvas's PaintColor. Build one block for RedButton, using set Canvas1.PaintColor to with a red block from the Colors drawer. Then right click it, choose Duplicate, and change the copy for blue, and again for green.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1HorizontalArrangement1RedButtonBlueButtonGreenButtonClearButtonCanvas1Any componentRenameDeleteShow Warningswhen RedButton.Clickdoset Canvas1.PaintColor towhen BlueButton.Clickdoset Canvas1.PaintColor towhen GreenButton.Clickdoset Canvas1.PaintColor toThree buttons, three colours. Tip Duplicate saves a lot of dragging. Remember to change both the button and the colour in the copy.Step 6: Clear and dots
ClearButton wipes the canvas with call Canvas1.Clear. And when Canvas1.Touched, a tap that does not move, draws a dot with call Canvas1.DrawCircle where the finger touched.
App InventorMy ProjectsGuideBlocksBuilt-inControlLogicMathTextListsDictionariesColorsVariablesProceduresScreen1HorizontalArrangement1RedButtonBlueButtonGreenButtonClearButtonCanvas1Any componentRenameDeleteShow Warningswhen ClearButton.Clickdocall Canvas1.Clearwhen Canvas1.TouchedxytouchedAnySpritedocall Canvas1.DrawCirclecenterXget xcenterYget yradius8filltrueClear wipes the canvas, and a tap draws a dot. Step 7: Doodle on your phone
Connect the Companion and draw with your finger. Try every colour, tap for dots, then Clear.
9:41Doodle PadA doodle in three colours.
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.
Pen size
Add a Slider to change how thick the pen is.
Show a hint
Add a Slider from User Interface. In when Slider1.PositionChanged, set Canvas1.LineWidth to the thumbPosition it gives you.
A rubber
Add a rubber that draws in the background colour.
Show a hint
A RubberButton that sets PaintColor to white, the same as the canvas.
Save your picture
Save the drawing as a picture on the phone.
Show a hint
call Canvas1.Save saves the canvas and gives back where the file went. Show it in a Label.
Notes for the teacher
Timing
- 15 minutes: steps 1 to 3, the Designer.
- 20 minutes: step 4. Show how to get the orange get prevX blocks by pointing at the event's parameters.
- 15 minutes: steps 5 to 7.
Mistakes to look out for
- Dots instead of lines, or lines from the top corner: the DrawLine sockets are filled with the wrong parameters. They must be prevX, prevY, then currentX, currentY.
- A duplicated colour block that still says RedButton: both blocks then do the same thing, and App Inventor marks one with an error.
- The canvas too small to draw on: check its Height is Fill parent.
Going further
An event's parameters are information the event hands to your blocks, just like a function's parameters in Python.