Beginning android 2
Murphy Android 2 Beginning Companion eBook Available Beginning Android 2 Trim: 7.5 x 9.25 spine = 0.78125" 416 page count Begin the journey toward your own successful Android 2 applications Mark L. Murphy COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION US $44.99 Shelve in Mobile Computing User level: Beginner www.apress.com SOURCE CODE ONLINE BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® ISBN 978-1-4302-2629-1 9 781430 226291 5 44 9 9 this print for content only—size & color not accurate CYAN MAGENTA YELLOW BLACK PANTONE 123 C SPOT MATTE F irst, welcome to the world of Android! As you have probably noticed, Android is rapidly growing; with the release of Android 2, many devices from various manufacturers are dotting the landscape. With millions of de- vices in the wild, and millions more on the way, those who get into the latest version of Android early will reap the benets as the ecosystem con- tinues to grow. Second, welcome to the new edition of this book, which now covers Android 2! Its purpose is to start you on your way to building compelling Android 2 ap- plications, and to help you master the learning curve. Android 2 is a rich frame- work, which means that there is a lot of cool stu for you to pick up along your journey in order to create the slickest, most useful apps you can imagine. Android 2 is an important release of the framework, so it’s important to get o to the best possible start when learning the ins and outs of Android 2 development. This book guides you on your journey to learning Android 2, pointing out what is possible and showing you how to turn those concepts into reality. From there, building practical and popular apps is a matter of time and creativity. Android 2 ISBN 978-1-4302-2629-1 9 781430 226291 5 44 9 9 i Beginning Android 2 ■ ■ ■ Mark L. Murphy ii Beginning Android 2 Copyright © 2010 by Mark L. Murphy All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2629-1 ISBN-13 (electronic): 978-1-4302-2630-7 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Matthew Moodie Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Fran Parnell Copy Editor: Marilyn S. Smith Compositor: MacPS, LLC Indexer: John Collin Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- ny@springer-sbm.com , or visit www.springeronline.com . For information on translations, please e-mail rights@apress.com , or visit www.apress.com . Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales . The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. iii iv Contents at a Glance ■Contents at a Glance . iv ■Contents . v ■About the Author . xiii ■Acknowledgments .xiv ■Preface xv ■Chapter 1: The Big Picture . 1 ■Chapter 2: Projects and Targets 5 ■Chapter 3: Creating a Skeleton Application . 17 ■Chapter 4: Using XML-Based Layouts 23 ■Chapter 5: Employing Basic Widgets . 29 ■Chapter 6: Working with Containers 39 ■Chapter 7: Using Selection Widgets . 59 ■Chapter 8: Getting Fancy with Lists . 75 ■Chapter 9: Employing Fancy Widgets and Containers . 95 ■Chapter 10: The Input Method Framework 117 ■Chapter 11: Applying Menus 125 ■Chapter 12: Fonts . 137 ■Chapter 13: Embedding the WebKit Browser . 141 ■Chapter 14: Showing Pop-Up Messages 149 ■Chapter 15: Dealing with Threads . 155 ■Chapter 16: Handling Activity Life Cycle Events 167 ■Chapter 17: Creating Intent Filters 171 ■Chapter 18: Launching Activities and Subactivities 177 ■Chapter 19: Handling Rotation . 185 ■Chapter 20: Working with Resources 197 ■Chapter 21: Using Preferences 213 ■Chapter 22: Managing and Accessing Local Databases 225 ■Chapter 23: Accessing Files . 237 ■Chapter 24: Leveraging Java Libraries 245 ■Chapter 25: Communicating via the Internet . 253 ■Chapter 26: Using a Content Provider 259 ■Chapter 27: Building a Content Provider . 265 ■Chapter 28: Requesting and Requiring Permissions . 275 ■Chapter 29: Creating a Service 279 ■Chapter 30: Invoking a Service 285 ■Chapter 31: Alerting Users via Notifications 289 ■Chapter 32: Accessing Location-Based Services . 295 ■Chapter 33: Mapping with MapView and MapActivity . 301 ■Chapter 34: Handling Telephone Calls . 311 ■Chapter 35: Development Tools . 315 ■Chapter 36: Handling Multiple Screen Sizes 331 ■Chapter 37: Dealing with Devices 353 ■Chapter 38: Handling Platform Changes 359 ■Chapter 39: Where Do We Go from Here? 367 ■Index 369 v Contents ■Contents at a Glance . iv ■Contents v ■About the Author xiii ■Acknowledgments .xiv ■Preface .xv ■Chapter 1: The Big Picture . 1 Challenges of Smartphone Programming .1 What Androids Are Made Of 2 Stuff at Your Disposal .3 ■Chapter 2: Projects and Targets 5 Pieces and Parts .5 Creating a Project .6 Project Structure .6 Root Contents 6 The Sweat Off Your Brow .7 And Now, the Rest of the Story 7 What You Get Out of It 8 Inside the Manifest .8 In the Beginning, There Was the Root, And It Was Good .9 Permissions, Instrumentations, and Applications (Oh My!) .9 Your Application Does Something, Right? .10 Achieving the Minimum .11 Version=Control .12 Emulators and Targets 12 Virtually There 12 Aiming at a Target .14 ■ CONTENTS vi ■Chapter 3: Creating a Skeleton Application . 17 Begin at the Beginning 17 Dissecting the Activity 18 Building and Running the Activity .20 ■Chapter 4: Using XML-Based Layouts . 23 What Is an XML-Based Layout? 23 Why Use XML-Based Layouts? .24 OK, So What Does It Look Like? 24 What’s with the @ Signs? .25 And How Do We Attach These to the Java? 25 The Rest of the Story 26 ■Chapter 5: Employing Basic Widgets . 29 Assigning Labels .29 Button, Button, Who’s Got the Button? .30 Fleeting Images 31 Fields of Green. Or Other Colors. 32 Just Another Box to Check 33 Turn the Radio Up .35 It’s Quite a View 37 Useful Properties .37 Useful Methods 38 Colors .38 ■Chapter 6: Working with Containers . 39 Thinking Linearly 39 LinearLayout Concepts and Properties 40 LinearLayout Example 42 All Things Are Relative 46 RelativeLayout Concepts and Properties .47 RelativeLayout Example .49 Tabula Rasa 51 TableLayout Concepts and Properties .52 TableLayout Example .53 Scrollwork .54 ■Chapter 7: Using Selection Widgets 59 Adapting to the Circumstances .59 Lists of Naughty and Nice .60 Spin Control 63 Grid Your Lions (or Something Like That .) 66 Fields: Now with 35% Less Typing! 69 Galleries, Give or Take the Art 72 ■Chapter 8: Getting Fancy with Lists 75 Getting to First Base .75 A Dynamic Presentation 77 Better. Stronger. Faster. .80 Using convertView .80 Using the Holder Pattern 81 ■ CONTENTS vii Making a List . .84 .And Checking It Twice .89 Adapting Other Adapters .94 ■Chapter 9: Employing Fancy Widgets and Containers . 95 Pick and Choose .95 Time Keeps Flowing Like a River 99 Making Progress .101 Seeking Resolution .101 Put It on My Tab 102 The Pieces .103 The Idiosyncrasies .103 Wiring It Together 104 Adding Them Up .106 Intents and Views 109 Flipping Them Off 109 Manual Flipping .110 Adding Contents on the Fly 112 Automatic Flipping .113 Getting in Someone’s Drawer .114 Other Good Stuff .116 ■Chapter 10: The Input Method Framework 117 Keyboards, Hard and Soft .117 Tailored to Your Needs 118 Tell Android Where It Can Go 121 Fitting In 123 Unleash Your Inner Dvorak .124 ■Chapter 11: Applying Menus . 125 Menus of Options 125 Creating an Options Menu .125 Adding Menu Choices and Submenus .126 Menus in Context 127 Taking a Peek .128 Yet More Inflation 133 Menu XML Structure 133 Menu Options and XML 134 Inflating the Menu 135 ■Chapter 12: Fonts 137 Love the One You’re With 137 More Fonts 138 Here a Glyph, There a Glyph .140 ■Chapter 13: Embedding the WebKit Browser 141 A Browser, Writ Small .141 Loading It Up .143 Navigating the Waters .144 Entertaining the Client 145 Settings, Preferences, and Options (Oh My!) 147 ■ CONTENTS viii ■Chapter 14: Showing Pop-Up Messages 149 Raising Toasts .149 Alert! Alert! 150 Checking Them Out .151 ■Chapter 15: Dealing with Threads . 155 Getting Through the Handlers .155 Messages .156 Runnables 158 Running in Place .159 Where Oh Where Has My UI Thread Gone? .159 Asyncing Feeling .159 The Theory .159 AsyncTask, Generics, and Varargs .160 The Stages of AsyncTask .160 A Sample Task .161 And Now, the Caveats .164 ■Chapter 16: Handling Activity Life Cycle Events 167 Schroedinger’s Activity .167 Life, Death, and Your Activity 168 onCreate() and onDestroy() 168 onStart(), onRestart(), and onStop() .169 onPause() and onResume() 169 The Grace of State 169 ■Chapter 17: Creating Intent Filters 171 What’s Your Intent? .171 Pieces of Intents 172 Intent Routing 172 Stating Your Intent(ions) .173 Narrow Receivers .174 The Pause Caveat .175 ■Chapter 18: Launching Activities and Subactivities 177 Peers and Subs .177 Start ’Em Up 178 Make an Intent .178 Make the Call .178 Tabbed Browsing, Sort Of .182 ■Chapter 19: Handling Rotation 185 A Philosophy of Destruction 185 It’s All the Same, Just Different 185 Now with More Savings! .189 DIY Rotation 191 Forcing the Issue 193 Making Sense of It All .195 ■Chapter 20: Working with Resources 197 The Resource Lineup 197 String Theory 198