The Busy Coder's Guide to Android Development
by Mark L. Murphy
The Busy Coder's Guide to Android Development
by Mark L. Murphy
Table of Contents
Headings formatted in bold-italic have changed since the last version.
• Preface
◦ Welcome to the Book! xxv
◦ The Book’s Structure xxv
◦ The Trails xxvi
◦ Warescription xxx
◦ Getting Help xxxi
◦ Book Bug Bounty xxxi
◦ Source Code And Its License xxxii
◦ Creative Commons and the Four-to-Free (42F) Guarantee xxxii
◦ Acknowledgments xxxiii
• KeyAndroidConcepts
◦ Android Applications 1
◦ Android Devices 7
◦ Don’t Be Scared 10
• ChoosingYourIDE
◦ Eclipse 11
◦ Alternative IDEs 12
◦ IDEs… And This Book 13
◦ About App Inventor 13
• Tutorial#1-InstallingtheTools
◦ Step #1 - Checking Your Hardware Requirements 15
◦ Step #2 - Setting Up Java 16
◦ Step #3 - Install the Android SDK 16
◦ Step #4 - Install the ADT for Eclipse 18
◦ Got the Picture? 64
◦ Dimensions 66
◦ The Resource That Shall Not Be Named… Yet 67
• Tutorial#4-AdjustingOurResources
◦ Step #1: Changing the Name 69
◦ Step #2: Changing the Icon 72
◦ In Our Next Episode… 80
• TheTheoryofWidgets
◦ What Are Widgets? 81
◦ Size, Margins, and Padding 83
◦ What Are Containers? 83
◦ The Absolute Positioning Anti-Pattern 84
• TheAndroidUserInterface
◦ The Activity 87
◦ Dissecting the Activity 88
◦ Using XML-Based Layouts 89
• BasicWidgets
◦ Common Concepts 95
◦ Assigning Labels 97
◦ A Commanding Button 102
◦ Fleeting Images 106
◦ Fields of Green. Or Other Colors. 111
◦ More Common Concepts 114
◦ Visit the Trails! 116
iii
◦
Adapting to the Circumstances
165
◦
Lists of Naughty and Nice
Adding the Widget
196
• DebuggingCrashes
◦ Get Thee To a Stack Trace 120
◦ The Case of the Confounding Class Cast 123
◦ Point Break 123
• LinearLayoutandtheBoxModel
◦ Concepts and Properties 125
◦ Eclipse Graphical Layout Editor 129
• OtherCommonWidgetsandContainers
◦ Just a Box to Check 131
◦ Don’t Like Checkboxes? How About Toggles? 134
◦ Turn the Radio Up 136
◦ All Things Are Relative 138
◦ Tabula Rasa 145
◦ Scrollwork 149
◦ Making Progress with ProgressBars 152
◦ Visit the Trails! 153
• Tutorial#5-MakingProgress
◦ Step #1: Removing The “Hello, World” 155
◦ Step #2: Adding a ProgressBar 157
◦ Step #3: Seeing the Results 159
◦ In Our Next Episode… 160
• GUIBuilding,Continued
◦
Making Your Selection
161
◦
Including Includes
161
• Tutorial#6-AddingaLibrary
◦ Step #1: Downloading and Unpacking ActionBarSherlock 223
◦ Step #2: Adding the Library to Your Project 224
◦ In Our Next Episode… 227
• OptionsMenusandtheActionBar
◦ Terminology 229
◦ A Wee Spot O’ History 231
◦ Your Action Bar Options 232
◦ Setting the Target 235
◦ Minding Narrow 236
◦ Defining the Resource 236
◦ Applying the Resource 241
◦ Responding to Events 241
◦ Attaching to Action Layouts 242
◦ The Rest of the Sample Activity 242
◦ Visit the Trails! 250
• Tutorial#7-AddingtheActionBar
◦ Step #1: Setting Up the Target SDK Version 251
◦ Step #2: Setting the Theme and Splitting the Bar 253
◦ Step #3: Changing to SherlockFragmentActivity 255
◦ Step #4: Defining Some Options 256
◦ Step #5: Loading and Responding to Our Options 259
v
◦ In Our Next Episode… 262
• Android’sProcessModel
◦ When Processes Are Created 263
◦ BACK, HOME, and Your Process 264
◦ Termination 265
◦ Foreground Means “I Love You” 265
◦ You and Your Heap 266
• SwipingwithViewPager
◦ Swiping Design Patterns 321
◦ Paging Fragments 322
◦ Paging Other Stuff 327
vi
◦ Indicators 327
◦ Fragment-Free Paging 331
• Tutorial#10-RiggingUpaViewPager
◦ Step #1: Add a ViewPager to the Layout 333
◦ Step #2: Obtaining Our ViewPager 334
◦ Step #3: Creating a ContentsAdapter 335
◦ Step #4: Setting Up the ViewPager 336
◦ In Our Next Episode… 337
• ResourceSetsandConfigurations
◦ What’s a Configuration? And How Do They Change? 339
◦ Configurations and Resource Sets 340
◦ Coping with Complexity 341
◦ Default Change Behavior 343
◦ Your Options for Configuration Changes 345
◦ Blocking Rotations 357
• DealingwithThreads
◦ The Main Application Thread 359
◦ Getting to the Background 360
◦ Asyncing Feeling 361
◦ Alternatives to AsyncTask 369
◦ And Now, The Caveats 370
• RequestingPermissions
◦
Mother, May I?
372
◦ Step #2: Defining Our Model 406
◦ Step #3: Examining Our Model 408
◦ Step #4: Creating a ModelFragment 408
◦ Step #5: Examining the ModelFragment 411
◦ Step #6: Supplying the Content 412
◦ Step #7: Adapting the Content 413
◦ Step #8: Going Home, Again 415
◦ In Our Next Episode… 416
• UsingPreferences
◦
Getting What You Want
417
◦
Stating Your Preference
418
◦
Introducing PreferenceActivity
419
◦
Types of Preferences
431
◦
Intents for Headers or Preferences
434
• Tutorial#13-UsingSomePreferences
◦ Step #1: Adding a StockPreferenceFragment 438
◦ Step #2: Defining the Preference XML Files 439
◦ Step #3: Creating Our PreferenceActivity 441
◦ Step #4: Adding To Our Action Bar 442
◦ Step #5: Launching the PreferenceActivity 444
◦ What’s Your Intent? 507
◦ Stating Your Intent(ions) 509
◦ Responding to Implicit Intents 510
◦ Requesting Implicit Intents 512
◦ Broadcasts and Receivers 516
◦ Example System Broadcasts 518
◦ Downloading Files 525
◦ Keeping It Local 537
• Tutorial#15-SharingYourNotes
◦ Step #1: Adding a Share Action Bar Item 541
◦ Step #2: Sharing the Note 542
◦ Step #3: Tying Them Together 543
◦ Step #4: Testing the Result 543
◦ In Our Next Episode… 545
• ServicesandtheCommandPattern
◦ Why Services? 547
◦ Setting Up a Service 548
◦ Communicating To Services 550
◦ Scenario: The Music Player 552
◦ Communicating From Services 555
◦ Scenario: The Downloader 557
• Tutorial#16-UpdatingtheBook
◦ Step #1: Adding a Stub DownloadCheckService 564
◦ Step #2: Tying the Service Into the Action Bar 565
◦ Step #3: Adding a Stub DownloadCompleteReceiver 566
◦ Step #4: Completing the DownloadCheckService 567
◦ Step #5: Adding a Stub DownloadInstallService 571
◦ Step #6: Completing the DownloadCompleteReceiver 572
◦ Step #7: Completing the DownloadInstallService 572
◦ Step #8: Updating ModelFragment 575
◦ In Our Next Episode… 634
• Large-ScreenStrategiesandTactics
◦ Objective: Maximum Gain, Minimum Pain 635
◦ The Fragment Strategy 635
◦ Fragment Example: The List-and-Detail Pattern 644
◦ Showing More Pages 655
◦ Fragment FAQs 659
◦ Screen Size and Density Tactics 661
◦ Other Considerations 664
• Tutorial#19-SupportingLargeScreens
◦ Step #1: Creating Our Layouts 667
◦ Step #2: Loading Our Sidebar Widgets 671
x
◦ Step #3: Opening the Sidebar 672
◦ Step #4: Loading Content Into the Sidebar 672
◦ Step #5: Removing Content From the Sidebar 675
• BackwardsCompatibilityStrategiesandTactics
◦
Think Forwards, Not Backwards
677
◦
Aim Where You Are Going
679
◦
A Target-Rich Environment
679
◦
A Little Help From Your Friends
681
◦
695
◦
Trying to Have Some Rhythm
695
◦
Our Test App
696
◦
Replacing the Classics
698
◦
Implicit Rows and Columns
705
◦
Row and Column Spans
707
◦
Should You Use GridLayout?
710
• DialogsandDialogFragments
◦
Prerequisites
711
◦
DatePickerDialog and TimePickerDialog
711
◦
AlertDialog
718
◦
◦
Exercising Your Control
822
◦
Layers Upon Layers
823
◦ Tabs (And Sometimes List) Navigation 748
◦ Custom Navigation 754
• ActionModesandContextMenus
◦ Prerequisites 758
◦ Another Wee Spot O’ History 758
◦ Manual Action Modes 759
◦ Multiple-Modal-Choice Action Modes 764
◦ Split Action Modes 768
◦ What Came Before: Context Menus 771
• AdvancedUsesofWebView
◦ Prerequisites 775
◦ Friends with Benefits 775
◦ Turnabout is Fair Play 780
◦ Navigating the Waters 784
◦ Settings, Preferences, and Options (Oh, My!) 784
• TheInputMethodFramework
◦ Prerequisites 787
◦ Keyboards, Hard and Soft 787
◦ Tailored To Your Needs 788
◦ Tell Android Where It Can Go 792
◦ Fitting In 794
◦ Jane, Stop This Crazy Thing! 796
• Fonts
◦ Prerequisites 799
867
• Animators
◦
Prerequisites
879
◦
ViewPropertyAnimator
879
◦
The Foundation: Value and Object Animators
884
◦
Hardware Acceleration
886
◦
The Three-Fragment Problem
887
• LegacyAnimations
◦
Prerequisites
899
◦
It’s Not Just For Toons Anymore
899
◦
A Quirky Translation
900
◦
Fading To Black. Or Some Other Color.
904
◦ The Mysterious Case of the Missing Number 945
• HomeScreenAppWidgets
◦
Prerequisites
947
◦
East is East, and West is West…
948
◦
The Big Picture for a Small App Widget
948
◦
Crafting App Widgets
949
◦
Another and Another
956
◦
App Widgets: Their Life and Times
957
◦
Controlling Your (App Widget’s) Destiny
957
◦
Change Your Look
958
◦
One Size May Not Fit All
959
◦
995
◦
Using a Content Provider
995
◦
Building Content Providers
1001
◦
Issues with Content Providers
1008
• ContentProviderImplementationPatterns
◦
Prerequisites
1009
◦
The Single-Table Database-Backed Content Provider
1009
◦
The Local-File Content Provider
1017
• TheLoaderFramework
◦ Prerequisites 1023
◦ Cursors: Issues with Management 1024
◦ Introducing the Loader Framework 1024
◦ Honeycomb… Or Not 1026
◦ Using CursorLoader 1027
xiv
◦
Using SQLiteCursorLoader
1029
1055
• TheCalendarContractProvider
◦
Prerequisites
1062
◦
You Can’t Be a Faker
1062
◦
Do You Have Room on Your Calendar?
1062
◦
Penciling In an Event
1067
• EncryptedStorage
◦
Prerequisites
1070
◦
Scenarios for Encryption
1070
◦
Obtaining SQLCipher
1071
◦
Employing SQLCipher
1071
• PackagingandDistributingData
◦ Prerequisites 1077
◦ Packing a Database To Go 1077
◦ Prerequisites 1099
◦ Location Providers: They Know Where You’re Hiding 1100
◦ Finding Yourself 1100
◦ On the Move 11 02
xv
◦ Are We There Yet? Are We There Yet? Are We There Yet? 1103
◦ Testing… Testing… 1104
• WorkingwiththeClipboard
◦ Prerequisites 11 05
◦ Using the Clipboard on Android 1.x/2.x 1105
◦ Advanced Clipboard on Android 3.x 1109
• Telephony
◦ Prerequisites 1115
◦ Report To The Manager 1116
◦ You Make the Call! 1116
◦ No, Really, You Make the Call! 1119
• WorkingWithSMS
◦ Prerequisites 1121
◦ Sending Out an SOS, Give or Take a Letter 1121
◦ You Can’t Get There From Here 1128
• UsingtheCamera
◦ Prerequisites 1131
◦ Letting the Camera App Do It 1131
◦ Scanning with ZXing 1133
◦ Directly Working with the Camera 1135
◦ Being Specific About Features 1135
• NFC
◦ Prerequisites 1137
◦ What Is NFC? 1137
◦ To NDEF, Or Not to NDEF 1139
◦
What WakefulIntentService Does
1175
• PushN
otifications
withGCM
◦
Prerequisites
1177
◦
The Precursor: C2DM
1177
◦
The Replacement: GCM
1178
◦
The Pieces of Push
1178
◦
A Simple Push
1185
◦
Message Options and Advanced Features
1192
◦
Re-Registration
1194
◦
Considering Encryption
1194
1224
◦
Dealing with Device Bugs
1227
• RespondingtoURLs
◦ Prerequisites 1229
◦ Manifest Modifications 1229
◦ Creating a Custom URL 1231
◦ Reacting to the Link 1231
• PluginPatterns
◦ Plugins by Remote 1235
◦ ContentProvider Plugins 1245
• PackageManagerTricks
◦ Prerequisites 1255
◦ Asking Around 1255
◦ Preferred Activities 1259
◦ Middle Management 1264
• SearchingwithSearchManager
◦ Prerequisites 1267
◦ Hunting Season 1267
◦ Search Yourself 1269
◦ Searching for Meaning In Randomness 1276
◦ May I Make a Suggestion? 1277
◦ Putting Yourself (Almost) On Par with Google 1281
• HandlingSystemEvents
◦
Prerequisites
1287
◦
I Sense a Connection Between Us…
◦ All Grown Up 1349
◦ Following the Script 1350
◦ Going Off-Script 1351
• TheScriptingLayerforAndroid
◦
Prerequisites
1355
◦
The Role of SL4A
1355
◦
Getting Started with SL4A
1356
◦
Writing SL4A Scripts
1364
◦
Running SL4A Scripts
1369
◦
Potential Issues
1370
• JVMScriptingLanguages
◦
Prerequisites
1373
◦
Languages on Languages
1373
◦
◦
Getting Help
1403
• KindleFire
◦
Prerequisites
1405
◦
What Features and Configurations Does It Use?
1405
◦
What Is Really Different?
1407
◦
Getting Your Development Environment Established
1412
◦
How Does Distribution Work?
1416
• Barnes&NobleNOOKTablet
◦ Prerequisites 1419
◦ What Features and Configurations Does It Use? 1419
◦ What Is Really Different? 1420
◦ Getting Your Development Environment Established 1421
◦ How Does Distribution Work? 1424
• RIMBlackberryPlaybook
xix
◦ What Features and Configurations Does It Use? 1425
◦ What Is Really Different? 1426
◦ Getting Your Development Environment Established 1427
◦
What Can This Thing Really Do?
1465
◦
What Are You Really Writing?
1466
◦ Getting Your Development Environment Established 1467
◦
How Does Distribution Work?
1467
◦
Example: WatchAuth
1468
◦
Getting Help
1482
• JUnitandAndroid
◦ Prerequisites 1483
◦ You Get What They Give You 1483
◦ Your Test Cases 1486
◦ Your Test Suite 1491
◦ Running Your Tests 1492
• MonkeyRunnerandtheTestMonkey
◦
Prerequisites
1495
◦
MonkeyRunner
1495
◦
Viewing the View Hierarchy
1516
◦
ViewServer
1519
• UsingDDMS
◦ Prerequisites 1521
◦ Starting DDMS 1521
◦ File Push and Pull 1522
◦ Screenshots 1522
◦ Location Updates 1523
◦ Placing Calls and Messages 1524
• SigningYourApp
◦ Prerequisites 1527
◦ Role of Code Signing 1527
◦ What Happens In Debug Mode 1528
◦ Creating a Production Signing Key 1528
• Distribution
◦ Prerequisites 1535
◦ Get Ready To Go To Market 1535
• IssueswithSpeed
◦ Prerequisites 1541
◦ Getting Things Done 1541
◦ Your UI Seems… Janky 1542
◦ Not Far Enough in the Background 1542
◦ Playing with Speed 1543
• FindingCPUBottlenecks
◦ Prerequisites 1545
◦ Traceview 1546
◦ Other General CPU Measurement Techniques 1555
◦
Prerequisites
1615
◦
Bandwidth Savings
1615
◦
Bandwidth Shaping
1621
◦
Avoiding Metered Connections
1624
• IssueswithMemory
◦ Prerequisites 1627
◦ You Are in a Heap of Trouble 1627
◦ Warning: Contains Graphic Images 1628
◦ In Too Deep (on the Stack) 1629
• FindingMemoryLeakswithMAT
◦ Prerequisites 1631
◦ Setting Up MAT 1631
◦ Getting Heap Dumps 1632
◦ Basic MAT Operation 1637
◦ Some Leaks and Their MAT Analysis 1644
• IssueswithBatteryLife
◦ Prerequisites 1653
◦ You’re Getting Blamed 1654
◦ Stretching Out the Last mWh 1655
• FocusOn:MDPandTrepn
xxii
◦
◦
Support, Structure
1677
◦
Caveat Developer
1678
• HTML5
◦
Prerequisites
1679
◦
Offline Applications
1679
◦
Web Storage
1686
◦
Going To Production
1689
◦
Issues You May Encounter
1690
◦
HTML5: The Baseline
1693
• PhoneGap
◦ Prerequisites 1695
◦ What Is PhoneGap? 1695
◦ Using PhoneGap 1698
◦ PhoneGap and the Checklist Sample 1704