Version 2.6 ISBN: 978-0-9816780-5-4
Total Page:16
File Type:pdf, Size:1020Kb
The Busy Coder's Guide to Advanced 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 Advanced Android Development by Mark L. Murphy Copyright © 2009-12 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 2012: Version 2.6 ISBN: 978-0-9816780-5-4 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!.......................................................xxvii Preface................................................................................................xxix Welcome to the Book!.............................................................................xxix Prerequisites.............................................................................................xxix Getting Help..............................................................................................xxx Warescription..........................................................................................xxxi Errata and the Book Bug Bounty............................................................xxxi Source Code...........................................................................................xxxiii Creative Commons and the Four-to-Free (42F) Guarantee...............xxxiv Lifecycle of a CommonsWare Book.....................................................xxxiv WebView, Inside and Out........................................................................1 Friends with Benefits....................................................................................1 Turnabout is Fair Play..................................................................................7 Crafting Your Own Views.......................................................................13 Pick Your Poison..........................................................................................13 Colors, Mixed How You Like Them...........................................................15 The Layout.............................................................................................16 The Attributes.......................................................................................18 The Class................................................................................................19 iii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Seeing It In Use....................................................................................24 More Fun With ListViews......................................................................27 Giant Economy-Size Dividers....................................................................27 Choosing What Is Selectable...............................................................28 Introducing MergeAdapter.................................................................29 Lists via Merges....................................................................................30 How MergeAdapter Does It................................................................34 From Head To Toe......................................................................................37 Control Your Selection...............................................................................40 Create a Unified Row View..................................................................41 Configure the List, Get Control on Selection....................................42 Change the Row...................................................................................44 Stating Your Selection................................................................................46 Creating Drawables...............................................................................49 Traversing Along a Gradient.....................................................................49 State Law......................................................................................................53 A Stitch In Time Saves Nine......................................................................55 The Name and the Border...................................................................56 Padding and the Box............................................................................57 Stretch Zones........................................................................................57 Tooling..................................................................................................59 Using Nine-Patch Images....................................................................60 Home Screen App Widgets....................................................................65 East is East, and West is West...................................................................66 The Big Picture for a Small App Widget..................................................66 Crafting App Widgets................................................................................68 The Manifest.........................................................................................68 iv Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition The Metadata.......................................................................................69 The Layout............................................................................................70 The BroadcastReceiver.........................................................................71 The Result.............................................................................................74 Another and Another.................................................................................75 App Widgets: Their Life and Times..........................................................76 Controlling Your (App Widget's) Destiny................................................76 Change Your Look......................................................................................77 One Size May Not Fit All...........................................................................79 Advanced App Widgets on Android 3.x...................................................80 New Widgets for App Widgets...........................................................80 Preview Images.....................................................................................81 Adapter-Based App Widgets...............................................................83 Being a Good Host......................................................................................95 Interactive Maps....................................................................................97 Get to the Point..........................................................................................98 Getting the Latitude and Longitude...................................................98 Getting the Screen Position................................................................98 Not-So-Tiny Bubbles.................................................................................100 Options for Pop-up Panels.................................................................101 Defining a Panel Layout......................................................................101 Creating a PopupPanel Class.............................................................103 Showing and Hiding the Panel..........................................................103 Tying It Into the Overlay...................................................................104 Sign, Sign, Everywhere a Sign....................................................................111 Selected States......................................................................................111 Per-Item Drawables.............................................................................112 v Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Changing Drawables Dynamically.....................................................113 In A New York Minute. Or Hopefully a Bit Faster..................................117 A Little Touch of Noo Yawk.....................................................................120 Touch Events.......................................................................................120 Finding an Item...................................................................................122 Dragging the Item...............................................................................124 Creating Custom Dialogs and Preferences.........................................127 Your Dialog, Chocolate-Covered..............................................................127 Basic AlertDialog Setup......................................................................129 Handling Color Changes.....................................................................131 State Management...............................................................................131