www.traintelco.com www.traintelco.com App Inventor Create Your Own Android Apps www.traintelco.com www.traintelco.com App Inventor Create Your Own Android Apps David Wolber, Hal Abelson, Ellen Spertus & Liz Looney Beijing · Cambridge · Farnham · Köln · Sebastopol · Tokyo www.traintelco.com 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/institutional sales department: 800-998-9938 or corporate@oreilly.com Editors: Courtney Nash and Brian Jepson Production Editor: Holly Bauer Copyeditor: Rachel Monaghan Proofreader: Holly Bauer Indexer: Denise Getz Cover Designer: Mark Paglietti Interior Designer: Ron Bilodeau 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 information contained herein 978-1-4493-9748-7 [TI] www.traintelco.com For Tomás, who reinvents me every day www.traintelco.com www.traintelco.com Contents Foreword xiii Preface xv Hello Purr What You’ll Learn The App Inventor Environment Designing the Components Adding Behaviors to the Components Packaging the App for Downloading Sharing the App Variations Summary 14 15 15 16 Part I. 12 Customizable Apps PaintPot 19 What You’ll Learn Getting Started Designing the Components Adding Behaviors to the Components The Complete App: PaintPot Variations Summary 20 20 20 24 35 35 36 MoleMash 37 What You’ll Build What You’ll Learn Getting Started Adding Behaviors to the Components www.traintelco.com 37 38 38 41 viii Contents The Complete App: MoleMash Variations Summary 49 49 50 No Texting While Driving 51 What You’ll Learn Getting Started The Complete App: No Texting While Driving Variations Summary 52 53 66 66 68 Ladybug Chase 69 What You’ll Build What You’ll Learn Designing the Components Getting Started Animating the Ladybug Displaying the Energy Level Adding an Aphid Adding a Restart Button Adding the Frog Adding Sound Effects Variations Summary 69 69 70 71 71 74 78 82 83 86 86 87 Paris Map Tour 89 What You’ll Learn Designing the Components Setting the Properties of ActivityStarter Adding Behaviors to the Components Setting Up a Virtual Tour Variations Summary 89 90 90 91 94 98 98 Android, Where’s My Car? 99 What You’ll Learn Getting Started Designing the Components 99 100 100 www.traintelco.com Creating Your Own App Inventor–Compliant APIs 347 Wrapping the Yahoo! Finance API The Yahoo! Stocks API for App Inventor used in this chapter was created by modifying the template code above with code found through a simple web search As the goal was wrapping the Yahoo! Stocks API for use by App Inventor, the developer (Wolber) did a web search for “Python Yahoo Stocks API” From the site http://www gummy-stuff.org/Yahoo-data.htm, he found that a URL in the form: http://download.finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.cs v&s=IBM would return a text file with a single comma-separated string of data The preceding URL returns this text string: "IBM",140.85,"10/15/2010","3:00pm",-0.65,142.10,142.10,140.60,4974553 He then found some Python code for accessing the Yahoo! Stocks API at http://www goldb.org/ystockquote.html With some quick cutting and pasting and a bit of editing, the App Inventor wrapper API was created by modifying the template in the following manner: def get_value(self, tag): # Need to generate a string or list and send it to WriteToPhone/ WriteToWeb # Multi-word strings should have quotes in front and back # e.g., # value = "\""+value+"\"" # call the Yahoo Finance API and get a handle to the file that is returned quoteFile=urllib.urlopen("http://download.finance.yahoo.com/d/quotes.csv?f= sl1d1t1c1ohgv&e=.csv&s="+tag) line = quoteFile.readline() # there's only one line splitlist = line.split(",") # split the data into a list # the data has quotes around the items, so eliminate them i=0 while i