App Inventor Create Your Own Android Apps David Wolber, Hal Abelson, Ellen Spertus & Liz Looney Beijing Cambridge Farnham Koln O'REILLY Sebastopol Tokyo
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 Parti. 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. MakeQuizandTakeQuiz 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 fortaking 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 ClockTimer 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