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.

Day 3 50 to 60 minutes 7 steps Key Stage 3
Start step 1
A doodle in three colours.

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

  1. Step 1: A row for the buttons

    Start a new project called DoodlePad and set Screen1's Title to Doodle 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.

    A HorizontalArrangement across the top of the screen.
  2. 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, GreenButton and ClearButton. Set each one's Text, and give the colour buttons their own BackgroundColor with white TextColor.

    Four buttons in a row, renamed.
  3. 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.

    A Canvas filling the rest of the screen.
  4. 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.

    Draw a line from the finger's last position to where it is now.
  5. 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.

    Three buttons, three colours.
    Tip Duplicate saves a lot of dragging. Remember to change both the button and the colour in the copy.
  6. 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.

    Clear wipes the canvas, and a tap draws a dot.
  7. Step 7: Doodle on your phone

    Connect the Companion and draw with your finger. Try every colour, tap for dots, then Clear.

    A 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.

  1. 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.

  2. 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.

  3. 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

  1. 15 minutes: steps 1 to 3, the Designer.
  2. 20 minutes: step 4. Show how to get the orange get prevX blocks by pointing at the event's parameters.
  3. 15 minutes: steps 5 to 7.

Mistakes to look out for

  1. 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.
  2. A duplicated colour block that still says RedButton: both blocks then do the same thing, and App Inventor marks one with an error.
  3. 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.