The Busy Coder's Guide to Android Development
Total Page:16
File Type:pdf, Size:1020Kb
A"$ BCD EFGGHIPQRPST UVWIHXWRY`a Eb cd !"# " # 0123456407289@ The Busy Coder's Guide to Android Development by Mark L. Murphy Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008-2011 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. CommonsWare books may be purchased in printed (bulk) or digital form for educational or business use. For more information, contact [email protected]. Printing History: Mar 2011:Version 3.6 ISBN: 978-0-9816780-0-9 The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress are trademarks of CommonsWare, LLC. All other trademarks referenced in this book are trademarks of their respective firms. The publisher and author(s) assume no responsibility for errors or omissions or for damages resulting from the use of the information contained herein. Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Table of Contents Welcome to the Warescription!........................................................xxiii Preface.................................................................................................xxv Welcome to the Book!..............................................................................xxv Warescription............................................................................................xxv Book Bug Bounty.....................................................................................xxvi Source Code And Its License.................................................................xxvii Creative Commons and the Four-to-Free (42F) Guarantee...............xxviii Acknowledgments...................................................................................xxix The Big Picture.........................................................................................1 What Androids Are Made Of.......................................................................3 Activities.................................................................................................3 Services...................................................................................................4 Content Providers..................................................................................4 Intents.....................................................................................................4 Stuff At Your Disposal..................................................................................5 Storage.....................................................................................................5 Network...................................................................................................5 Multimedia.............................................................................................5 GPS..........................................................................................................5 iii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Phone Services........................................................................................6 The Big Picture...Of This Book....................................................................6 How To Get Started.................................................................................7 Step #1: Java...................................................................................................7 Install the JDK........................................................................................7 Learn Java...............................................................................................8 Step #2: Install the Android SDK................................................................9 Install the Base Tools.............................................................................9 Install the SDKs and Add-Ons..............................................................9 Step #3: Install the ADT for Eclipse...........................................................13 Step #4: Install Apache Ant........................................................................15 Step #5: Set Up the Emulator.....................................................................16 Step #6: Set Up the Device.........................................................................23 Windows...............................................................................................24 OS X and Linux.....................................................................................25 Your First Android Project....................................................................27 Step #1: Create the New Project.................................................................27 Eclipse...................................................................................................27 Command Line......................................................................................31 Step #2: Build, Install, and Run the Application in Your Emulator or Device..........................................................................................................32 Eclipse....................................................................................................32 Command Line.....................................................................................33 Examining Your First Project................................................................37 Project Structure.........................................................................................37 Root Contents.......................................................................................37 The Sweat Off Your Brow....................................................................38 iv Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition And Now, The Rest of the Story..........................................................39 What You Get Out Of It......................................................................40 Inside Your Manifest..................................................................................40 In The Beginning, There Was the Root, And It Was Good...............41 An Application For Your Application.................................................42 A Bit About Eclipse................................................................................45 What the ADT Gives You...........................................................................45 Coping with Eclipse....................................................................................46 How to Import a Non-Eclipse Project................................................46 How to Get To DDMS...........................................................................51 How to Create an Emulator.................................................................53 How to Run a Project...........................................................................54 How Not to Run Your Project.............................................................55 Alternative IDEs..........................................................................................55 More on the Tools......................................................................................56 IDEs...And This Book.................................................................................57 Enhancing Your First Project................................................................59 Supporting Multiple Screens.....................................................................59 Specifying Versions....................................................................................60 Rewriting Your First Project.................................................................65 The Activity.................................................................................................65 Dissecting the Activity...............................................................................66 Building and Running the Activity...........................................................68 About the Remaining Examples................................................................69 Using XML-Based Layouts.....................................................................71 What Is an XML-Based Layout?.................................................................71 Why Use XML-Based Layouts?..................................................................72 v Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition OK, So What Does It Look Like?...............................................................73 What's With the @ Signs?.........................................................................74 And We Attach These to the Java...How?................................................74 The Rest of the Story..................................................................................75 Employing Basic Widgets......................................................................79 Assigning Labels.........................................................................................79 Button, Button, Who's Got the Button?...................................................80 Fleeting Images...........................................................................................81 Fields of Green. Or Other Colors..............................................................83