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-11 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: Dec 2011:Version 2.2 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!..........................................................xxi Preface................................................................................................xxiii Welcome to the Book!.............................................................................xxiii Prerequisites............................................................................................xxiii Getting Help.............................................................................................xxiv Warescription............................................................................................xxv Errata and the Book Bug Bounty.............................................................xxv Source Code............................................................................................xxvii Creative Commons and the Four-to-Free (42F) Guarantee...............xxviii Lifecycle of a CommonsWare Book.....................................................xxviii 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.............................................................................................73 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 Preferring Your Own Preferences, Preferably.........................................132 The Constructor..................................................................................133 Creating the View................................................................................133 Dealing with Preference Values.........................................................134 Using the Preference...........................................................................136 Advanced Fragments and the Action Bar...........................................139 About the Sample App..............................................................................139 What the User Sees............................................................................140 The Data Model (Such As It Is and What There Is Of It)...............148 Dynamic Fragments..................................................................................150 Fragments and Panes..........................................................................150 Fragments and Activities....................................................................152 Running a FragmentTransaction.......................................................153 Action Bar Navigation Options................................................................162 Tabs Mode...........................................................................................162 "List" Mode..........................................................................................167 vi Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Dialog Fragments......................................................................................168 "Lights Out"...............................................................................................172 Leveraging the Home Icon........................................................................173 And All The Rest........................................................................................175 PersistentListFragment.......................................................................175 ContentFragment................................................................................178 ItemsActivity........................................................................................181 Other Bits of Goodness.............................................................................182 Custom Navigation Mode..................................................................182 Dynamic Menus..................................................................................183 More On Retained Instances....................................................................184 The Activity.........................................................................................184 The Fragment......................................................................................185 What We Really Should Be Doing....................................................186 Action Modes.......................................................................................189 Saying Goodbye to Context Menus.........................................................190 Manual Action Modes..............................................................................190 Choosing Your Trigger........................................................................191 Starting the Action Mode...................................................................192 Implementing the Action Mode........................................................193 Multiple-Modal-Choice Action Modes...................................................196 Animating Widgets..............................................................................203 It's Not Just For Toons Anymore.............................................................203 A Quirky Translation...............................................................................204 Mechanics of Translation..................................................................204 Imagining a Sliding Panel..................................................................205 The Aftermath....................................................................................205 vii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Introducing SlidingPanel..................................................................206 Using the Animation.........................................................................208 Fading To Black. Or Some Other Color.................................................208 Alpha Numbers..................................................................................209 Animations in XML............................................................................209 Using XML Animations......................................................................210 When It's All Said And Done...................................................................210 Loose Fill.....................................................................................................211 Hit The Accelerator...................................................................................212 Animate. Set. Match..................................................................................213 Active Animations.....................................................................................214 Using the Camera.................................................................................217 Sneaking a Peek.........................................................................................217 The Permission and the Feature........................................................218 The SurfaceView.................................................................................219 The Camera.........................................................................................220 Image Is Everything..................................................................................224 Asking for a Camera. Maybe.............................................................224 Getting the Camera............................................................................225 Asking for a Format...........................................................................226 Taking a Picture..................................................................................227 Using AsyncTask................................................................................228 Maintaining Your Focus...........................................................................230 All the Bells and Whistles.........................................................................231 Playing Media.......................................................................................233 Get Your Media On...................................................................................233 Making Noise............................................................................................234 viii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Streaming Limitations.......................................................................240 Moving Pictures........................................................................................240 Other Ways to Make Noise.....................................................................244 SoundPool...........................................................................................244 AudioTrack.........................................................................................245 ToneGenerator...................................................................................246 Handling System Events.....................................................................249 Get Moving, First Thing...........................................................................249 The Permission...................................................................................250 The Receiver Element........................................................................250 The Receiver Implementation............................................................251 New Behavior With Android 3.1........................................................252 I Sense a Connection Between Us...........................................................253 Feeling Drained.........................................................................................255 Sticky Intents and the Battery..........................................................260 Battery and the Emulator...................................................................261 Other Power Triggers........................................................................262 Advanced Service Patterns..................................................................263 Remote Services........................................................................................263 When IPC Attacks!.............................................................................264 A Consumer Economy.......................................................................266 Service From Afar...............................................................................268 Servicing the Service..........................................................................274 The Bind That Fails............................................................................280 If the Binding Is Too Tight.................................................................281 AlarmManager: Making the Services Run On Time..............................283 The WakefulIntentService Pattern...................................................283 ix Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition
Description: