In today's tutorial, we are going learn how to work with event handlers. We are going to make a simple painting program.
Parts of this tutorial was written by Google so its language will be different than our first tutorials. This tutorial was written for adults. PLEASE go slowly and don't skip directions. Let a teacher know if you need help.
Using meaningful names makes your projects more readable to yourself and others.
You should now have three buttons, one above the other. The next step is to make them line up horizontally. You do this using a HorizontalArrangement component.
In the Blocks Editor:
The blocks for the buttons should look like this:
Add Touch-event Handlers
Now for the next step: drawing on the Canvas. You'll arrange things so that when you touch the canvas, you get a dot at the spot where you touch. If you drag your finger slowly along the canvas, it draws a line.
The X and Y are the places on the canvas where you touch.
On the right side of the call DrawingCanvas.DrawCircle block are three sockets where you must specify values for the x and y coordinates where the circle should be drawn, and r, which is the radius of the circle (how big it is). For x and y, you'll use values found inside the when DrawingCanvas.Touched bubbles.
Here's how the touch event handler should look:
Try out what you have so far on the phone. Touch a color button. Now touch the canvas, and your finger should leave a spot at each place you touch. Touching the Wipe button should clear your drawing.
Add Drag Events
Finally, add the drag event handler. Here's the difference between a touch and a drag:
When you drag your finger across the screen, it appears to draw a giant, curved line where you moved your finger. What you're actually doing is drawing hundreds of tiny straight lines: each time you move your finger, even a little bit, you extend the line from your finger's immediate last position to its new position.
A drag event comes with 6 arguments. These are three pairs of x and y coordinates that show:
There's also a sprite, which we'll ignore for this tutorial.
Now make dragging draw a line between the previous position and the current position by creating a drag handler:
Here's the result:
Test your work by trying it on the phone: drag your finger around on the screen to draw lines and curves. Touch the screen to make spots. Use the Wipe button to clear the screen.
Here are some hard things to try to do. If you don't want to try any of these, or are finished:
Regular Challenge: Add two more colors.
Super Hard Challenge: Allow users to make and set their own colors. Let users change the thickness of the lines they draw.
Developers Challenge: Turn the Day5 app into a make-up app. Add your picture to the canvas and turn the colors into make-up colors. Rename the app, add the ability to pick set the canvas picture to a user selected photos.
This tutorial was modified for middle school students from one of the original app inventor tutorials
Middle School Android App Inventor Tutorials by Richard Incorvia is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.