The Busy Coder's Guide to Android Development
Total Page:16
File Type:pdf, Size:1020Kb
The Busy Coder's Guide to Android Development by Mark L. Murphy The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008 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: Jul 2008: Version 1.0 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!..................................................................................xiii Preface..........................................................................................................................xv Welcome to the Book!...........................................................................................................xv Prerequisites..........................................................................................................................xv Warescription.......................................................................................................................xvi Book Bug Bounty.................................................................................................................xvii Source Code License..........................................................................................................xviii Creative Commons and the Four-to-Free (42F) Guarantee............................................xviii The Big Picture................................................................................................................1 What Androids Are Made Of.................................................................................................3 Activities...........................................................................................................................3 Content Providers...........................................................................................................4 Intents..............................................................................................................................4 Services.............................................................................................................................4 Stuff At Your Disposal.............................................................................................................5 Storage..............................................................................................................................5 Network............................................................................................................................5 Multimedia.......................................................................................................................5 GPS...................................................................................................................................5 Phone Services.................................................................................................................6 Project Structure............................................................................................................7 Root Contents..........................................................................................................................7 The Sweat Off Your Brow.......................................................................................................8 iii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition And Now, The Rest of the Story.............................................................................................8 What You Get Out Of It.........................................................................................................9 Inside the Manifest........................................................................................................11 In The Beginning, There Was the Root, And It Was Good.................................................11 Permissions, Instrumentations, and Applications (Oh, My!).............................................12 Your Application Does Something, Right?..........................................................................13 Creating a Skeleton Application...................................................................................17 Begin at the Beginning...........................................................................................................17 The Activity............................................................................................................................18 Dissecting the Activity...........................................................................................................19 Building and Running the Activity.......................................................................................21 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?..........................................................................................25 What's With the @ Signs?....................................................................................................26 And We Attach These to the Java...How?...........................................................................26 The Rest of the Story.............................................................................................................27 Employing Basic Widgets.............................................................................................29 Assigning Labels....................................................................................................................29 Button, Button, Who's Got the Button?..............................................................................30 Fleeting Images......................................................................................................................31 Fields of Green. Or Other Colors..........................................................................................31 Just Another Box to Check....................................................................................................34 Turn the Radio Up.................................................................................................................37 It's Quite a View....................................................................................................................39 Useful Properties...........................................................................................................39 Useful Methods..............................................................................................................39 Working with Containers.............................................................................................41 Thinking Linearly..................................................................................................................42 Concepts and Properties...............................................................................................42 Example..........................................................................................................................45 All Things Are Relative.........................................................................................................50 iv Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Concepts and Properties...............................................................................................50 Example..........................................................................................................................53 Tabula Rasa............................................................................................................................56 Concepts and Properties...............................................................................................56 Example..........................................................................................................................59 Scrollwork..............................................................................................................................60 Using Selection Widgets...............................................................................................65 Adapting to the Circumstances............................................................................................65 Using ArrayAdapter......................................................................................................66 Other Key Adapters.......................................................................................................67 Lists of Naughty and Nice....................................................................................................68