You are previewing Beginning Android 2.
O'Reilly logo
Beginning Android 2

Book Description

The Android development platform, created by Google and the Open Handset Alliance, is a platform in its truest sense, encompassing hundreds of classes beyond the traditional Java classes and open source components that ship with the SDK.

With Beginning Android 2, you'll learn how to develop applications for Android 2.x mobile devices, using simple examples that are ready to run with your copy of the SDK. Author, Android columnist, writer, developer, and community advocate Mark L. Murphy will show you what you need to know to get started programming Android applications, including how to craft GUIs, use GPS, and access web services.

Table of Contents

  1. Copyright
  2. About the Author
  3. Acknowledgments
  4. Preface
    1. Welcome to the Book!
    2. Prerequisites
    3. Editions of This Book
    4. Source Code and Its License
  5. 1. The Big Picture
    1. 1.1. Challenges of Smartphone Programming
    2. 1.2. What Androids Are Made Of
    3. 1.3. Stuff at Your Disposal
  6. 2. Projects and Targets
    1. 2.1. Pieces and Parts
    2. 2.2. Creating a Project
    3. 2.3. Project Structure
      1. 2.3.1. Root Contents
      2. 2.3.2. The Sweat Off Your Brow
      3. 2.3.3. And Now, the Rest of the Story
      4. 2.3.4. What You Get Out of It
    4. 2.4. Inside the Manifest
      1. 2.4.1. In the Beginning, There Was the Root, And It Was Good
      2. 2.4.2. Permissions, Instrumentations, and Applications (Oh My!)
      3. 2.4.3. Your Application Does Something, Right?
      4. 2.4.4. Achieving the Minimum
      5. 2.4.5. Version=Control
    5. 2.5. Emulators and Targets
      1. 2.5.1. Virtually There
      2. 2.5.2. Aiming at a Target
  7. 3. Creating a Skeleton Application
    1. 3.1. Begin at the Beginning
    2. 3.2. Dissecting the Activity
    3. 3.3. Building and Running the Activity
  8. 4. Using XML-Based Layouts
    1. 4.1. What Is an XML-Based Layout?
    2. 4.2. Why Use XML-Based Layouts?
    3. 4.3. OK, So What Does It Look Like?
    4. 4.4. What's with the @ Signs?
    5. 4.5. And How Do We Attach These to the Java?
    6. 4.6. The Rest of the Story
  9. 5. Employing Basic Widgets
    1. 5.1. Assigning Labels
    2. 5.2. Button, Button, Who's Got the Button?
    3. 5.3. Fleeting Images
    4. 5.4. Fields of Green. Or Other Colors.
    5. 5.5. Just Another Box to Check
    6. 5.6. Turn the Radio Up
    7. 5.7. It's Quite a View
      1. 5.7.1. Useful Properties
      2. 5.7.2. Useful Methods
      3. 5.7.3. Colors
  10. 6. Working with Containers
    1. 6.1. Thinking Linearly
      1. 6.1.1. LinearLayout Concepts and Properties
        1. 6.1.1.1. Orientation
        2. 6.1.1.2. Fill Model
        3. 6.1.1.3. Weight
        4. 6.1.1.4. Gravity
        5. 6.1.1.5. Padding
      2. 6.1.2. LinearLayout Example
    2. 6.2. All Things Are Relative
      1. 6.2.1. RelativeLayout Concepts and Properties
        1. 6.2.1.1. Positions Relative to Container
        2. 6.2.1.2. Relative Notation in Properties
        3. 6.2.1.3. Positions Relative to Other Widgets
        4. 6.2.1.4. Order of Evaluation
      2. 6.2.2. RelativeLayout Example
    3. 6.3. Tabula Rasa
      1. 6.3.1. TableLayout Concepts and Properties
        1. 6.3.1.1. Putting Cells in Rows
        2. 6.3.1.2. Other Children of TableLayout
        3. 6.3.1.3. Stretch, Shrink, and Collapse
      2. 6.3.2. TableLayout Example
    4. 6.4. Scrollwork
  11. 7. Using Selection Widgets
    1. 7.1. Adapting to the Circumstances
    2. 7.2. Lists of Naughty and Nice
    3. 7.3. Spin Control
    4. 7.4. Grid Your Lions (or Something Like That...)
    5. 7.5. Fields: Now with 35% Less Typing!
    6. 7.6. Galleries, Give or Take the Art
  12. 8. Getting Fancy with Lists
    1. 8.1. Getting to First Base
    2. 8.2. A Dynamic Presentation
    3. 8.3. Better. Stronger. Faster.
      1. 8.3.1. Using convertView
      2. 8.3.2. Using the Holder Pattern
    4. 8.4. Making a List...
    5. 8.5. ...And Checking It Twice
    6. 8.6. Adapting Other Adapters
  13. 9. Employing Fancy Widgets and Containers
    1. 9.1. Pick and Choose
    2. 9.2. Time Keeps Flowing Like a River
    3. 9.3. Making Progress
    4. 9.4. Seeking Resolution
    5. 9.5. Put It on My Tab
      1. 9.5.1. The Pieces
      2. 9.5.2. The Idiosyncrasies
      3. 9.5.3. Wiring It Together
      4. 9.5.4. Adding Them Up
      5. 9.5.5. Intents and Views
    6. 9.6. Flipping Them Off
      1. 9.6.1. Manual Flipping
      2. 9.6.2. Adding Contents on the Fly
      3. 9.6.3. Automatic Flipping
    7. 9.7. Getting in Someone's Drawer
    8. 9.8. Other Good Stuff
  14. 10. The Input Method Framework
    1. 10.1. Keyboards, Hard and Soft
    2. 10.2. Tailored to Your Needs
    3. 10.3. Tell Android Where It Can Go
    4. 10.4. Fitting In
    5. 10.5. Unleash Your Inner Dvorak
  15. 11. Applying Menus
    1. 11.1. Menus of Options
      1. 11.1.1. Creating an Options Menu
      2. 11.1.2. Adding Menu Choices and Submenus
    2. 11.2. Menus in Context
    3. 11.3. Taking a Peek
    4. 11.4. Yet More Inflation
      1. 11.4.1. Menu XML Structure
      2. 11.4.2. Menu Options and XML
      3. 11.4.3. Inflating the Menu
  16. 12. Fonts
    1. 12.1. Love the One You're With
    2. 12.2. More Fonts
    3. 12.3. Here a Glyph, There a Glyph
  17. 13. Embedding the WebKit Browser
    1. 13.1. A Browser, Writ Small
    2. 13.2. Loading It Up
    3. 13.3. Navigating the Waters
    4. 13.4. Entertaining the Client
    5. 13.5. Settings, Preferences, and Options (Oh My!)
  18. 14. Showing Pop-Up Messages
    1. 14.1. Raising Toasts
    2. 14.2. Alert! Alert!
    3. 14.3. Checking Them Out
  19. 15. Dealing with Threads
    1. 15.1. Getting Through the Handlers
      1. 15.1.1. Messages
      2. 15.1.2. Runnables
    2. 15.2. Running in Place
    3. 15.3. Where Oh Where Has My UI Thread Gone?
    4. 15.4. Asyncing Feeling
      1. 15.4.1. The Theory
      2. 15.4.2. AsyncTask, Generics, and Varargs
      3. 15.4.3. The Stages of AsyncTask
      4. 15.4.4. A Sample Task
        1. 15.4.4.1. The AddStringTask Declaration
        2. 15.4.4.2. The doInBackground() Method
        3. 15.4.4.3. The onProgressUpdate() Method
        4. 15.4.4.4. The onPostExecute() Method
        5. 15.4.4.5. The Activity
    5. 15.5. And Now, the Caveats
  20. 16. Handling Activity Life Cycle Events
    1. 16.1. Schroedinger's Activity
    2. 16.2. Life, Death, and Your Activity
      1. 16.2.1. onCreate() and onDestroy()
      2. 16.2.2. onStart(), onRestart(), and onStop()
      3. 16.2.3. onPause() and onResume()
    3. 16.3. The Grace of State
  21. 17. Creating Intent Filters
    1. 17.1. What's Your Intent?
      1. 17.1.1. Pieces of Intents
      2. 17.1.2. Intent Routing
    2. 17.2. Stating Your Intent(ions)
    3. 17.3. Narrow Receivers
    4. 17.4. The Pause Caveat
  22. 18. Launching Activities and Subactivities
    1. 18.1. Peers and Subs
    2. 18.2. Start 'Em Up
      1. 18.2.1. Make an Intent
      2. 18.2.2. Make the Call
    3. 18.3. Tabbed Browsing, Sort Of
  23. 19. Handling Rotation
    1. 19.1. A Philosophy of Destruction
    2. 19.2. It's All the Same, Just Different
    3. 19.3. Now with More Savings!
    4. 19.4. DIY Rotation
    5. 19.5. Forcing the Issue
    6. 19.6. Making Sense of It All
  24. 20. Working with Resources
    1. 20.1. The Resource Lineup
    2. 20.2. String Theory
      1. 20.2.1. Plain Strings
      2. 20.2.2. String Formats
      3. 20.2.3. Styled Text
      4. 20.2.4. Styled String Formats
    3. 20.3. Got the Picture?
    4. 20.4. XML: The Resource Way
    5. 20.5. Miscellaneous Values
      1. 20.5.1. Dimensions
      2. 20.5.2. Colors
      3. 20.5.3. Arrays
    6. 20.6. Different Strokes for Different Folks
  25. 21. Using Preferences
    1. 21.1. Getting What You Want
    2. 21.2. Stating Your Preference
    3. 21.3. And Now, a Word from Our Framework
    4. 21.4. Letting Users Have Their Say
    5. 21.5. Adding a Wee Bit o' Structure
    6. 21.6. The Kind of Pop-Ups You Like
  26. 22. Managing and Accessing Local Databases
    1. 22.1. The Database Example
    2. 22.2. A Quick SQLite Primer
    3. 22.3. Start at the Beginning
    4. 22.4. Setting the Table
    5. 22.5. Makin' Data
    6. 22.6. What Goes Around Comes Around
      1. 22.6.1. Raw Queries
      2. 22.6.2. Regular Queries
      3. 22.6.3. Building with Builders
      4. 22.6.4. Using Cursors
    7. 22.7. Data, Data, Everywhere
  27. 23. Accessing Files
    1. 23.1. You and the Horse You Rode in On
    2. 23.2. Readin' 'n Writin'
  28. 24. Leveraging Java Libraries
    1. 24.1. The Outer Limits
    2. 24.2. Ants and JARs
    3. 24.3. Following the Script
    4. 24.4. ...And Not a Drop to Drink
    5. 24.5. Reviewing the Script
  29. 25. Communicating via the Internet
    1. 25.1. REST and Relaxation
    2. 25.2. HTTP Operations via Apache HttpClient
    3. 25.3. Parsing Responses
    4. 25.4. Stuff to Consider
  30. 26. Using a Content Provider
    1. 26.1. Pieces of Me
    2. 26.2. Getting a Handle
    3. 26.3. Makin' Queries
    4. 26.4. Adapting to the Circumstances
    5. 26.5. Give and Take
    6. 26.6. Beware of the BLOB!
  31. 27. Building a Content Provider
    1. 27.1. First, Some Dissection
    2. 27.2. Next, Some Typing
    3. 27.3. Creating Your Content Provider
      1. 27.3.1. Step 1: Create a Provider Class
        1. 27.3.1.1. onCreate()
        2. 27.3.1.2. query()
        3. 27.3.1.3. insert()
        4. 27.3.1.4. update()
        5. 27.3.1.5. delete()
        6. 27.3.1.6. getType()
      2. 27.3.2. Step 2: Supply a Uri
      3. 27.3.3. Step 3: Declare the Properties
      4. 27.3.4. Step 4: Update the Manifest
    4. 27.4. Notify-on-Change Support
  32. 28. Requesting and Requiring Permissions
    1. 28.1. Mother, May I?
    2. 28.2. Halt! Who Goes There?
      1. 28.2.1. Enforcing Permissions via the Manifest
      2. 28.2.2. Enforcing Permissions Elsewhere
    3. 28.3. May I See Your Documents?
  33. 29. Creating a Service
    1. 29.1. Service with Class
    2. 29.2. There Can Only Be One
    3. 29.3. Manifest Destiny
    4. 29.4. Lobbing One Over the Fence
      1. 29.4.1. Callbacks
      2. 29.4.2. Broadcast Intents
    5. 29.5. Where's the Remote? And the Rest of the Code?
  34. 30. Invoking a Service
    1. 30.1. The Ties That Bind
    2. 30.2. Catching the Lob
  35. 31. Alerting Users via Notifications
    1. 31.1. Types of Pestering
      1. 31.1.1. Hardware Notifications
      2. 31.1.2. Icons
    2. 31.2. Seeing Pestering in Action
  36. 32. Accessing Location-Based Services
    1. 32.1. Location Providers: They Know Where You're Hiding
    2. 32.2. Finding Yourself
    3. 32.3. On the Move
    4. 32.4. Are We There Yet? Are We There Yet? Are We There Yet?
    5. 32.5. Testing...Testing...
  37. 33. Mapping with MapView and MapActivity
    1. 33.1. Terms, Not of Endearment
    2. 33.2. Piling On
    3. 33.3. The Bare Bones
    4. 33.4. Exercising Your Control
      1. 33.4.1. Zoom
      2. 33.4.2. Center
    5. 33.5. Rugged Terrain
    6. 33.6. Layers upon Layers
      1. 33.6.1. Overlay Classes
      2. 33.6.2. Drawing the ItemizedOverlay
      3. 33.6.3. Handling Screen Taps
    7. 33.7. My, Myself, and MyLocationOverlay
    8. 33.8. The Key to It All
  38. 34. Handling Telephone Calls
    1. 34.1. Report to the Manager
    2. 34.2. You Make the Call!
  39. 35. Development Tools
    1. 35.1. Hierarchical Management
    2. 35.2. Delightful Dalvik Debugging Detailed, Demoed
      1. 35.2.1. Logging
      2. 35.2.2. File Push and Pull
      3. 35.2.3. Screenshots
      4. 35.2.4. Location Updates
      5. 35.2.5. Placing Calls and Messages
    3. 35.3. Put It on My Card
      1. 35.3.1. Creating a Card Image
      2. 35.3.2. Inserting the Card
  40. 36. Handling Multiple Screen Sizes
    1. 36.1. Taking the Default
    2. 36.2. Whole in One
      1. 36.2.1. Think About Rules, Rather Than Positions
      2. 36.2.2. Consider Physical Dimensions
      3. 36.2.3. Avoid Real Pixels
      4. 36.2.4. Choose Scalable Drawables
    3. 36.3. Tailor-Made, Just for You (and You, and You, and...)
      1. 36.3.1. Add <supports-screens>
      2. 36.3.2. Resources and Resource Sets
        1. 36.3.2.1. Default Scaling
        2. 36.3.2.2. Density-Based Sets
        3. 36.3.2.3. Size-Based Sets
        4. 36.3.2.4. Version-Based Sets
      3. 36.3.3. Finding Your Size
    4. 36.4. Ain't Nothing Like the Real Thing
      1. 36.4.1. Density Differs
      2. 36.4.2. Adjusting the Density
      3. 36.4.3. Accessing Actual Devices
    5. 36.5. Ruthlessly Exploiting the Situation
      1. 36.5.1. Replace Menus with Buttons
      2. 36.5.2. Replace Tabs with a Simple Activity
      3. 36.5.3. Consolidate Multiple Activities
    6. 36.6. Example: EU4You
      1. 36.6.1. The First Cut
      2. 36.6.2. Fixing the Fonts
      3. 36.6.3. Fixing the Icons
      4. 36.6.4. Using the Space
      5. 36.6.5. What If It's Not a Browser?
    7. 36.7. What Are a Few Bugs Among Friends?
  41. 37. Dealing with Devices
    1. 37.1. This App Contains Explicit Instructions
    2. 37.2. Button, Button, Who's Got the Button?
    3. 37.3. A Guaranteed Market
    4. 37.4. The Down and Dirty Details
      1. 37.4.1. Archos 5 Android Internet Tablet
      2. 37.4.2. Motorola CLIQ/DEXT
      3. 37.4.3. Motorola DROID/Milestone
      4. 37.4.4. Google/HTC Nexus One
      5. 37.4.5. Motorola BACKFLIP
  42. 38. Handling Platform Changes
    1. 38.1. Brand Management
    2. 38.2. More Things That Make You Go Boom
      1. 38.2.1. View Hierarchy
      2. 38.2.2. Changing Resources
    3. 38.3. Handling API Changes
      1. 38.3.1. Detecting the Version
      2. 38.3.2. Wrapping the API
  43. 39. Where Do We Go from Here?
    1. 39.1. Questions–Sometimes with Answers
    2. 39.2. Heading to the Source
    3. 39.3. Getting Your News Fix