App Inventor Create Your Own Android Apps
Total Page:16
File Type:pdf, Size:1020Kb
App Inventor Create Your Own Android Apps App Inventor Create Your Own Android Apps David Wolber, Hal Abelson, Ellen Spertus & Liz Looney Beijing · Cambridge · Farnham · Köln · Sebastopol · Tokyo App Inventor by David Wolber, Hal Abelson, Ellen Spertus & Liz Looney Copyright © 2011 David Wolber, Hal Abelson, Ellen Spertus & Liz Looney. All rights reserved. Printed in Canada. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti- tutional sales department: 800-998-9938 or [email protected]. Editors: Courtney Nash and Brian Jepson Indexer: Denise Getz Production Editor: Holly Bauer Cover Designer: Mark Paglietti Copyeditor: Rachel Monaghan Interior Designer: Ron Bilodeau Proofreader: Holly Bauer Illustrator: Robert Romano Printing History: April 2011: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. App Inventor and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the informa- tion contained herein. 978-1-4493-9748-7 [TI] For Tomás, who reinvents me every day. Contents Foreword......................................................... xiii Preface.......................................................... xv 1. Hello Purr......................................................... 1 What You’ll Learn 1 The App Inventor Environment 2 Designing the Components 3 Adding Behaviors to the Components 8 Packaging the App for Downloading 14 Sharing the App 15 Variations 15 Summary 16 Part I. 12 Customizable Apps 2. PaintPot......................................................... 19 What You’ll Learn 20 Getting Started 20 Designing the Components 20 Adding Behaviors to the Components 24 The Complete App: PaintPot 35 Variations 35 Summary 36 3. MoleMash........................................................ 37 What You’ll Build 37 What You’ll Learn 38 Getting Started 38 Adding Behaviors to the Components 41 viii Contents The Complete App: MoleMash 49 Variations 49 Summary 50 4. No Texting While Driving........................................... 51 What You’ll Learn 52 Getting Started 53 The Complete App: No Texting While Driving 66 Variations 66 Summary 68 5. Ladybug Chase.................................................... 69 What You’ll Build 69 What You’ll Learn 69 Designing the Components 70 Getting Started 71 Animating the Ladybug 71 Displaying the Energy Level 74 Adding an Aphid 78 Adding a Restart Button 82 Adding the Frog 83 Adding Sound Effects 86 Variations 86 Summary 87 6. Paris Map Tour.................................................... 89 What You’ll Learn 89 Designing the Components 90 Setting the Properties of ActivityStarter 90 Adding Behaviors to the Components 91 Setting Up a Virtual Tour 94 Variations 98 Summary 98 7. Android, Where’s My Car?........................................... 99 What You’ll Learn 99 Getting Started 100 Designing the Components 100 Contents ix Adding Behaviors to the Components 102 The Complete App: Android, Where’s My Car? 111 Variations 111 Summary 111 8. Presidents Quiz................................................... 113 What You’ll Learn 113 Getting Started 114 Designing the Components 114 Adding Behaviors to the Components 116 Making the Quiz Easy to Modify 122 Switching the Image for Each Question 124 Checking the User’s Answers 126 The Complete App: The Presidents Quiz 129 Variations 130 Summary 130 9. Xylophone....................................................... 131 What You’ll Build 131 What You’ll Learn 132 Getting Started 132 Designing the Components 132 Creating the Keyboard 133 Recording and Playing Back Notes 138 Variations 145 Summary 145 10. MakeQuiz and TakeQuiz........................................... 147 What You’ll Learn 148 Getting Started 148 Designing the Components 149 Adding Behaviors to the Components 150 The Complete App: MakeQuiz 163 TakeQuiz: An App for Taking the Quiz in the Database 164 TakeQuiz: Modifying the Blocks to Load the Quiz from the Database 164 The Complete App: TakeQuiz 166 Variations 166 Summary 168 x Contents 11. Broadcast Hub................................................... 169 What You’ll Learn 170 Getting Started 170 Designing the Components 171 Adding Behaviors to the Components 172 The Complete App: Broadcast Hub 184 Variations 186 Summary 186 12. NXT Remote Control............................................... 187 What You’ll Learn 188 Getting Started 188 Designing the Components 189 Adding Behaviors to the Components 192 Variations 201 Summary 201 13. Amazon at the Bookstore.......................................... 203 What You’ll Learn 203 What Is an API? 204 Designing the Components 207 Designing the Behavior 208 Customizing the API 215 Variations 216 Summary 216 Part II. Inventor’s Manual 14. Understanding an App’s Architecture................................ 219 Components 220 Behavior 221 Summary 227 15. Engineering and Debugging an App................................. 229 Software Engineering Principles 229 Debugging an App 236 Summary 240 Contents xi 16. Programming Your App’s Memory................................... 241 Named Memory Slots 241 Properties 242 Defining Variables 243 Setting and Getting a Variable 244 Setting a Variable to an Expression 245 Summary 248 17. Creating Animated Apps........................................... 249 Adding a Canvas Component to Your App 249 The Canvas Coordinate System 250 Animating Objects with Timer Events 251 High-Level Animation Functions 253 Interactive Animation 256 Specifying Sprite Animation Without a Clock Timer 257 Summary 258 18. Programming Your App to Make Decisions: Conditional Blocks........... 259 Testing Conditions with if and ifelse Blocks 260 Programming an Either/Or Decision 261 Programming Conditions Within Conditions 262 Programming Complex Conditions 263 Summary 266 19. Programming Lists of Data......................................... 267 Creating a List Variable 268 Selecting an Item in a List 269 Using an Index to Traverse a List 269 Creating Input Forms and Dynamic Lists 273 Lists of Lists 277 Summary 280 20. Repeating Blocks: Iteration........................................ 281 Controlling an App’s Execution: Branching and Looping 281 Repeating Functions on a List Using foreach 282 A Second foreach Example: Displaying a List 284 Repeating Blocks with while 286 Summary 289 xii Contents 21. Defining Procedures: Reusing Blocks................................ 291 Eliminating Redundancy 293 Defining a Procedure 294 Calling a Procedure 295 The Program Counter 296 Adding Parameters to Your Procedure 296 Returning Values from a Procedure 299 Reusing Blocks Among Apps 301 A Second Example: distanceBetweenPoints 301 Summary 303 22. Working with Databases........................................... 305 Storing Persistent Data in TinyDB 306 Retrieving Data from TinyDB 307 Storing and Sharing Data with TinyWebDB 308 Storing Data with TinyWebDB 309 Requesting and Processing Data with TinyWebDB 310 GetValue-GotValue in Action 311 Setting Up a Web Database 315 Summary 317 23. Reading and Responding to Sensors................................. 319 Creating Location-Aware Apps 319 Using the Orientation Sensor 324 Using the Accelerometer 328 Summary 332 24. Communicating with Web APIs..................................... 333 Talking to Web APIs That Generate Images 335 Talking to Web Data APIs 341 Creating Your Own App Inventor–Compliant APIs 345 Summary 348 Index........................................................... 349 Foreword Our consumer culture gives us all sorts of opportunities for entertainment, pleasure and sometimes even learning. However, by and large, these are passive activities. That’s OK—we all like to kick back sometimes and be entertained—but it shouldn’t be the whole picture. In addition to the appeal of consuming, there’s the satisfaction of producing—that is, of creating. It’s the joy and pride that results when we draw a picture, build a model airplane, or bake some bread. The high-tech objects (like cell phones, tablet computers, TVs, etc.) that we use today to consume entertainment and information are black boxes to most of us. Their work- ings are incomprehensible and, while there are capabilities in some of them that en- able the user to draw pictures, make videos, etc., they are not, in and of themselves, creative media. In other words, most people can’t create the apps that run on these gadgets. What if we could change that? What if we could take creative control of our everyday gadgets, like cell phones? What if building an app for your cell phone was as easy as drawing a picture or baking a loaf of bread? What if we could close the gap between the objects of our consumer culture and the media of our creative lives? For one, it could demystify those objects. Rather than being black boxes, impenetrable to our sight, they become objects that can