Android Recipes: A Problem-Solution Approach

Book description

Android continues to be one of the two leading mobile OS and development platforms driving today's mobile innovations and the apps ecosystem. Android appears complex, but offers a variety of organized development kits to those coming into Android with differing programming language skill sets.

Android Recipes: A Problem-Solution Approach guides you step by step through a wide range of useful topics using complete and real-world working code examples.

In this book, you quickly get a refresh of Android architecture and app fundamentals, and then you get down to business and build an app with Google's Android SDK at the command line and Eclipse in an IDE context. Then you'll learn how to accomplish practical tasks pertaining to the user interface, communications with the cloud, device hardware, data persistence, communications between applications, and interacting with Android itself. Finally, you'll learn how to leverage various libraries and Scripting Layer for Android to help you perform tasks more quickly; how to use the Android NDK to boost app performance; and how to design apps for performance, responsiveness, seamlessness, and more.

Instead of abstract descriptions of complex concepts, you find live code examples in this book. When you start a new project, you can consider copying and pasting the code and configuration files from this book, and then modifying them for your own customization needs. This can save you a great deal of work over creating a project from scratch!

What you'll learn

  • Discover Android architecture and various Android-specific APIs.

  • How to develop a Units Converter app in the context of command-line/Android SDK and Eclipse/Android SDK environments.

  • How to accomplish various tasks related to the user interface and more.

  • How to use external libraries to save time and effort.

  • How to quickly develop an app using the Scripting Layer 4 Android tool.

  • How to boost app performance by using the Android NDK.

  • Guidelines for designing filtered apps, performant apps, responsive apps, and seamless apps.

Who this book is for

Newcomers to Android as well as more accomplished Android developers.

Table of contents

  1. Copyright
  2. Foreword
  3. About the Authors
  4. About the Technical Reviewer
  5. Acknowledgments
  6. Preface
    1. What Will You Find in the Book?
    2. Keep a Level Eye on the Target
  7. 1. Getting Started with Android
    1. 1.1. What Is Android?
    2. 1.2. History of Android
    3. 1.3. Android Architecture
    4. 1.4. App Architecture
      1. 1.4.1. Components
        1. 1.4.1.1. Activities
        2. 1.4.1.2. Services
        3. 1.4.1.3. Broadcast Receivers
        4. 1.4.1.4. Content Providers
      2. 1.4.2. Intents
      3. 1.4.3. Manifest
      4. 1.4.4. App Package
    5. 1.5. Activities in Depth
    6. 1.6. Services in Depth
    7. 1.7. Broadcast Receivers in Depth
    8. 1.8. Content Providers in Depth
      1. 1.8.1. 1-1. Installing the Android SDK
        1. 1.8.1.1. Problem
        2. 1.8.1.2. Solution
        3. 1.8.1.3. How It Works
      2. 1.8.2. 1-2. Installing an Android Platform
        1. 1.8.2.1. Problem
        2. 1.8.2.2. Solution
        3. 1.8.2.3. How It Works
      3. 1.8.3. 1-3. Creating an Android Virtual Device
        1. 1.8.3.1. Problem
        2. 1.8.3.2. Solution
        3. 1.8.3.3. How It Works
      4. 1.8.4. 1-4. Starting the AVD
        1. 1.8.4.1. Problem
        2. 1.8.4.2. Solution
        3. 1.8.4.3. How It Works
      5. 1.8.5. 1-5. Introducing UC
        1. 1.8.5.1. Problem
        2. 1.8.5.2. Solution
        3. 1.8.5.3. How It Works
      6. 1.8.6. 1-6. Creating UC
        1. 1.8.6.1. Problem
        2. 1.8.6.2. Solution
        3. 1.8.6.3. How It Works
      7. 1.8.7. 1-7. Installing and Running UC
        1. 1.8.7.1. Problem
        2. 1.8.7.2. Solution
        3. 1.8.7.3. How It Works
      8. 1.8.8. 1-8. Preparing UC for Publishing
        1. 1.8.8.1. Problem
        2. 1.8.8.2. Solution
        3. 1.8.8.3. How It Works
          1. 1.8.8.3.1. Version UC
          2. 1.8.8.3.2. Build UC in Release Mode
          3. 1.8.8.3.3. Sign UC's App Package
          4. 1.8.8.3.4. Align UC's App Package
      9. 1.8.9. 1-9. Migrating to Eclipse
        1. 1.8.9.1. Problem
        2. 1.8.9.2. Solution
        3. 1.8.9.3. How It Works
      10. 1.8.10. 1-10. Developing UC with Eclipse
        1. 1.8.10.1. Problem
        2. 1.8.10.2. Solution
        3. 1.8.10.3. How It Works
    9. 1.9. Summary
  8. 2. User Interface Recipes
    1. 2.1.
      1. 2.1.1. 2-1. Customizing the Window
        1. 2.1.1.1. Problem
        2. 2.1.1.2. Solution
        3. 2.1.1.3. How It Works
          1. 2.1.1.3.1. Customize Window Attributes with a Theme
      2. 2.1.2. 2-2. Creating and Displaying Views
        1. 2.1.2.1. Problem
        2. 2.1.2.2. Solution
        3. 2.1.2.3. How It Works
      3. 2.1.3. 2-3. Monitoring Click Actions
        1. 2.1.3.1. Problem
        2. 2.1.3.2. Solution
        3. 2.1.3.3. How It Works
      4. 2.1.4. 2-4. Resolution-Independent Assets
        1. 2.1.4.1. Problem
        2. 2.1.4.2. Solution
        3. 2.1.4.3. How It Works
      5. 2.1.5. 2-5. Locking Activity Orientation
        1. 2.1.5.1. Problem
        2. 2.1.5.2. Solution
        3. 2.1.5.3. How It Works
      6. 2.1.6. 2-6. Dynamic Orientation Locking
        1. 2.1.6.1. Problem
        2. 2.1.6.2. Solution
        3. 2.1.6.3. How It Works
          1. 2.1.6.3.1. User Rotation Lock Button
      7. 2.1.7. 2-7. Manually Handling Rotation
        1. 2.1.7.1. Problem
        2. 2.1.7.2. Solution
        3. 2.1.7.3. How It Works
      8. 2.1.8. 2-8. Creating Pop-Up Menu Actions
        1. 2.1.8.1. Problem
        2. 2.1.8.2. Solution
        3. 2.1.8.3. How It Works
          1. 2.1.8.3.1. ContextMenu
          2. 2.1.8.3.2. AlertDialog
      9. 2.1.9. 2-9. Customizing Options Menu
        1. 2.1.9.1. Problem
        2. 2.1.9.2. Solution
        3. 2.1.9.3. How It Works
      10. 2.1.10. 2-10. Customizing Back Button
        1. 2.1.10.1. Problem
        2. 2.1.10.2. Solution
        3. 2.1.10.3. How It Works
      11. 2.1.11. 2-11. Emulating the Home Button
        1. 2.1.11.1. Problem
        2. 2.1.11.2. Solution
        3. 2.1.11.3. How It Works
      12. 2.1.12. 2-12. Monitoring Textview Changes
        1. 2.1.12.1. Problem
        2. 2.1.12.2. Solution
        3. 2.1.12.3. How It Works
          1. 2.1.12.3.1. Character Counter Example
          2. 2.1.12.3.2. Currency Formatter Example
      13. 2.1.13. 2-13. Scrolling TextView Ticker
        1. 2.1.13.1. Problem
        2. 2.1.13.2. Solution
        3. 2.1.13.3. How It Works
      14. 2.1.14. 2-14. Animating a View
        1. 2.1.14.1. Problem
        2. 2.1.14.2. Solution
        3. 2.1.14.3. How It Works
          1. 2.1.14.3.1. System Animations
          2. 2.1.14.3.2. Custom Animations
          3. 2.1.14.3.3. AnimationSet
      15. 2.1.15. 2-15. Creating Drawables as Backgrounds
        1. 2.1.15.1. Problem
        2. 2.1.15.2. Solution
        3. 2.1.15.3. How It Works
          1. 2.1.15.3.1. Gradient ListView Row
          2. 2.1.15.3.2. Rounded View Group
      16. 2.1.16. 2-16. Creating Custom State Drawables
        1. 2.1.16.1. Problem
        2. 2.1.16.2. Solution
        3. 2.1.16.3. How It Works
          1. 2.1.16.3.1. Button and Clickable Widgets
          2. 2.1.16.3.2. CheckBox and Checkable Widgets
      17. 2.1.17. 2-17. Applying Masks to Images
        1. 2.1.17.1. Problem
        2. 2.1.17.2. Solution
        3. 2.1.17.3. How It Works
          1. 2.1.17.3.1. Rounded Corner Bitmap
          2. 2.1.17.3.2. Arbitrary Mask Image
          3. 2.1.17.3.3. Please Try This At Home
      18. 2.1.18. 2-18. Creating Dialogs that Persist
        1. 2.1.18.1. Problem
        2. 2.1.18.2. Solution
        3. 2.1.18.3. How It Works
      19. 2.1.19. 2-19. Implementing Situation-Specific Layouts
        1. 2.1.19.1. Problem
        2. 2.1.19.2. Solution
        3. 2.1.19.3. How It Works
          1. 2.1.19.3.1. Orientation-Specific
          2. 2.1.19.3.2. Size-Specific
          3. 2.1.19.3.3. Example
          4. 2.1.19.3.4. Late Additions
      20. 2.1.20. 2-20. Customizing Keyboard Actions
        1. 2.1.20.1. Problem
        2. 2.1.20.2. Solution
        3. 2.1.20.3. How It Works
          1. 2.1.20.3.1. Custom Enter Key
          2. 2.1.20.3.2. Custom Action
      21. 2.1.21. 2-21. Dismissing Soft Keyboard
        1. 2.1.21.1. Problem
        2. 2.1.21.2. Solution
        3. 2.1.21.3. How It Works
      22. 2.1.22. 2-22. Customizing AdapterView Empty Views
        1. 2.1.22.1. Problem
        2. 2.1.22.2. Solution
        3. 2.1.22.3. How It Works
          1. 2.1.22.3.1. Make Empty Interesting
      23. 2.1.23. 2-23. Customizing ListView Rows
        1. 2.1.23.1. Problem
        2. 2.1.23.2. Solution
        3. 2.1.23.3. How It Works
          1. 2.1.23.3.1. Simply Custom
          2. 2.1.23.3.2. Adapting to a More Complex Choice
      24. 2.1.24. 2-24. Making ListView Section Headers
        1. 2.1.24.1. Problem
        2. 2.1.24.2. Solution
        3. 2.1.24.3. How It Works
          1. 2.1.24.3.1. That Darn Expansion
      25. 2.1.25. 2-25. Creating Compound Controls
        1. 2.1.25.1. Problem
        2. 2.1.25.2. Solution
        3. 2.1.25.3. How It Works
          1. 2.1.25.3.1. TextImageButton
    2. 2.2. Useful Tools to Know: DroidDraw
      1. 2.2.1. Obtaining and Launching DroidDraw
      2. 2.2.2. Exploring DroidDraw's User Interface
      3. 2.2.3. Creating a Simple Screen
    3. 2.3. Summary
  9. 3. Communications and Networking
    1. 3.1.
      1. 3.1.1. 3-1. Displaying Web Information
        1. 3.1.1.1. Problem
        2. 3.1.1.2. Solution
        3. 3.1.1.3. How It Works
          1. 3.1.1.3.1. Display a URL
          2. 3.1.1.3.2. Local Assets
      2. 3.1.2. 3-2. Intercepting WebView Events
        1. 3.1.2.1. Problem
        2. 3.1.2.2. Solution
        3. 3.1.2.3. How It Works
      3. 3.1.3. 3-3. Accessing WebView with JavaScript
        1. 3.1.3.1. Problem
        2. 3.1.3.2. Solution
        3. 3.1.3.3. How It Works
      4. 3.1.4. 3-4. Downloading an Image File
        1. 3.1.4.1. Problem
        2. 3.1.4.2. Solution
        3. 3.1.4.3. How It Works
      5. 3.1.5. 3-5. Downloading Completely in the Background
        1. 3.1.5.1. Problem
        2. 3.1.5.2. Solution
        3. 3.1.5.3. How It Works
          1. 3.1.5.3.1. Destinations
      6. 3.1.6. 3-6. Accessing a REST API
        1. 3.1.6.1. Problem
        2. 3.1.6.2. Solution
        3. 3.1.6.3. How It Works
          1. 3.1.6.3.1. GET Example
          2. 3.1.6.3.2. POST Example
          3. 3.1.6.3.3. Basic Authentication
      7. 3.1.7. 3-7. Parsing JSON
        1. 3.1.7.1. Problem
        2. 3.1.7.2. Solution
        3. 3.1.7.3. How It Works
          1. 3.1.7.3.1. Debugging Trick
      8. 3.1.8. 3-8. Parsing XML
        1. 3.1.8.1. Problem
        2. 3.1.8.2. Solution
        3. 3.1.8.3. How It Works
      9. 3.1.9. 3-8. Receiving SMS
        1. 3.1.9.1. Problem
        2. 3.1.9.2. Solution
        3. 3.1.9.3. How It Works
      10. 3.1.10. 3-9. Sending an SMS Message
        1. 3.1.10.1. Problem
        2. 3.1.10.2. Solution
        3. 3.1.10.3. How It Works
      11. 3.1.11. 3-10. Communicating over Bluetooth
        1. 3.1.11.1. Problem
        2. 3.1.11.2. Solution
        3. 3.1.11.3. How It Works
          1. 3.1.11.3.1. Bluetooth Peer-To-Peer
        4. 3.1.11.4. Search Mode
          1. 3.1.11.4.1. Bluetooth Beyond Android
      12. 3.1.12. 3-11. Querying Network Reachability
        1. 3.1.12.1. Problem
        2. 3.1.12.2. Solution
        3. 3.1.12.3. How It Works
          1. 3.1.12.3.1. Determining Connection Type
    2. 3.2. Summary
  10. 4. Interacting with Device Hardware and Media
    1. 4.1.
      1. 4.1.1. 4-1. Integrating Device Location
        1. 4.1.1.1. Problem
        2. 4.1.1.2. Solution
        3. 4.1.1.3. How It Works
      2. 4.1.2. 4-2. Mapping Locations
        1. 4.1.2.1. Problem
        2. 4.1.2.2. Solution
        3. 4.1.2.3. How It Works
      3. 4.1.3. 4-3. Annotating Maps
        1. 4.1.3.1. Problem
        2. 4.1.3.2. Solution
        3. 4.1.3.3. How It Works
          1. 4.1.3.3.1. Make Them Interactive
          2. 4.1.3.3.2. What About Me?
      4. 4.1.4. 4-4. Capturing Images and Video
        1. 4.1.4.1. Problem
        2. 4.1.4.2. Solution
        3. 4.1.4.3. How It Works
          1. 4.1.4.3.1. Image Capture
          2. 4.1.4.3.2. Video Capture
      5. 4.1.5. 4-5. Making a Custom Camera Overlay
        1. 4.1.5.1. Problem
        2. 4.1.5.2. Solution
        3. 4.1.5.3. How It Works
          1. 4.1.5.3.1. Later Additions
          2. 4.1.5.3.2. Photo Overlay
      6. 4.1.6. 4-6. Recording Audio
        1. 4.1.6.1. Problem
        2. 4.1.6.2. Solution
        3. 4.1.6.3. How It Works
      7. 4.1.7. 4-7. Adding Speech Recognition
        1. 4.1.7.1. Problem
        2. 4.1.7.2. Solution
        3. 4.1.7.3. How It Works
      8. 4.1.8. 4-8. Playing Back Audio/Video
        1. 4.1.8.1. Problem
        2. 4.1.8.2. Solution
        3. 4.1.8.3. How It Works
          1. 4.1.8.3.1. Audio Playback
          2. 4.1.8.3.2. Audio Player
          3. 4.1.8.3.3. Video Player
          4. 4.1.8.3.4. Handling Redirects
      9. 4.1.9. 4-9. Creating a Tilt Monitor
        1. 4.1.9.1. Problem
        2. 4.1.9.2. Solution
        3. 4.1.9.3. How It Works
      10. 4.1.10. 4-10. Monitoring Compass Orientation
        1. 4.1.10.1. Problem
        2. 4.1.10.2. Solution
        3. 4.1.10.3. How It Works
    2. 4.2. Useful Tools to Know: SensorSimulator
      1. 4.2.1. Obtaining Sensor Simulator
      2. 4.2.2. Launching Sensor Simulator Settings and Sensor Simulator
      3. 4.2.3. Accessing Sensor Simulator from Your Apps
    3. 4.3. Summary
  11. 5. Persisting Data
    1. 5.1.
      1. 5.1.1. 5-1. Making a Preference Screen
        1. 5.1.1.1. Problem
        2. 5.1.1.2. Solution
        3. 5.1.1.3. How It Works
          1. 5.1.1.3.1. Loading Defaults and Accessing Preferences
      2. 5.1.2. 5-2. Persisting Simple Data
        1. 5.1.2.1. Problem
        2. 5.1.2.2. Solution
        3. 5.1.2.3. How It Works
          1. 5.1.2.3.1. Sharing SharedPreferences
      3. 5.1.3. 5-3. Reading and Writing Files
        1. 5.1.3.1. Problem
        2. 5.1.3.2. Solution
        3. 5.1.3.3. How It Works
          1. 5.1.3.3.1. Internal Storage
          2. 5.1.3.3.2. External Storage
      4. 5.1.4. 5-4. Using Files as Resources
        1. 5.1.4.1. Problem
        2. 5.1.4.2. Solution
        3. 5.1.4.3. How It Works
          1. 5.1.4.3.1. Parsing the CSV
      5. 5.1.5. 5-5. Managing a Database
        1. 5.1.5.1. Problem
        2. 5.1.5.2. Solution
        3. 5.1.5.3. How It Works
          1. 5.1.5.3.1. A Note About Upgrading
          2. 5.1.5.3.2. Using the Database
      6. 5.1.6. 5-6. Querying a Database
        1. 5.1.6.1. Problem
        2. 5.1.6.2. Solution
        3. 5.1.6.3. How It Works
      7. 5.1.7. 5-7. Backing Up Data
        1. 5.1.7.1. Problem
        2. 5.1.7.2. Solution
        3. 5.1.7.3. How It Works
          1. 5.1.7.3.1. Extra Credit
      8. 5.1.8. 5-8. Sharing Your Database
        1. 5.1.8.1. Problem
        2. 5.1.8.2. Solution
        3. 5.1.8.3. How It Works
      9. 5.1.9. 5-9. Sharing Your Other Data
        1. 5.1.9.1. Problem
        2. 5.1.9.2. Solution
        3. 5.1.9.3. How It Works
          1. 5.1.9.3.1. Usage Example
    2. 5.2. Useful Tools to Know: SQLite3
      1. 5.2.1. SQLite3 and UC
    3. 5.3. Summary
  12. 6. Interacting with the System
    1. 6.1.
      1. 6.1.1. 6-1. Notifying from the Background
        1. 6.1.1.1. Problem
        2. 6.1.1.2. Solution
        3. 6.1.1.3. How It Works
      2. 6.1.2. 6-2. Creating Timed and Periodic Tasks
        1. 6.1.2.1. Problem
        2. 6.1.2.2. Solution
        3. 6.1.2.3. How It Works
      3. 6.1.3. 6-3. Scheduling a Periodic Task
        1. 6.1.3.1. Problem
        2. 6.1.3.2. Solution
        3. 6.1.3.3. How It Works
          1. 6.1.3.3.1. A More Precise Example
      4. 6.1.4. 6-4. Creating Sticky Operations
        1. 6.1.4.1. Problem
        2. 6.1.4.2. Solution
        3. 6.1.4.3. How It Works
          1. 6.1.4.3.1. Possible Drawback
      5. 6.1.5. 6-5. Running Persistent Background Operations
        1. 6.1.5.1. Problem
        2. 6.1.5.2. Solution
        3. 6.1.5.3. How It Works
      6. 6.1.6. 6-6. Launching Other Applications
        1. 6.1.6.1. Problem
        2. 6.1.6.2. Solution
        3. 6.1.6.3. How It Works
          1. 6.1.6.3.1. Read a PDF File
          2. 6.1.6.3.2. Share with Friends
      7. 6.1.7. 6-7. Launching System Applications
        1. 6.1.7.1. Problem
        2. 6.1.7.2. Solution
        3. 6.1.7.3. How It Works
          1. 6.1.7.3.1. Browser
          2. 6.1.7.3.2. Phone Dialer
          3. 6.1.7.3.3. Maps
          4. 6.1.7.3.4. E-mail
          5. 6.1.7.3.5. SMS (Messages)
          6. 6.1.7.3.6. Contact Picker
          7. 6.1.7.3.7. Android Market
      8. 6.1.8. 6-8. Letting Other Applications Launch Yours
        1. 6.1.8.1. Problem
        2. 6.1.8.2. Solution
        3. 6.1.8.3. How It Works
          1. 6.1.8.3.1. Processing a Successful Launch
      9. 6.1.9. 6-9. Interacting with Contacts
        1. 6.1.9.1. Problem
        2. 6.1.9.2. Solution
        3. 6.1.9.3. How It Works
          1. 6.1.9.3.1. Listing/Viewing Contacts
          2. 6.1.9.3.2. Changing/Adding Contacts
          3. 6.1.9.3.3. Maintaining a Reference
      10. 6.1.10. 6-10. Picking Device Media
        1. 6.1.10.1. Problem
        2. 6.1.10.2. Solution
        3. 6.1.10.3. How It Works
      11. 6.1.11. 6-11. Saving to the MediaStore
        1. 6.1.11.1. Problem
        2. 6.1.11.2. Solution
        3. 6.1.11.3. How It Works
    2. 6.2. Summary
  13. 7. Working with Libraries
    1. 7.1.
      1. 7.1.1. 7-1. Creating Java Library JARs
        1. 7.1.1.1. Problem
        2. 7.1.1.2. Solution
        3. 7.1.1.3. How It Works
          1. 7.1.1.3.1. Creating MathUtils with the JDK
          2. 7.1.1.3.2. Creating MathUtils with Eclipse
      2. 7.1.2. 7-2. Using Java Library JARs
        1. 7.1.2.1. Problem
        2. 7.1.2.2. Solution
        3. 7.1.2.3. How It Works
      3. 7.1.3. 7-3. Creating Android Library Projects
        1. 7.1.3.1. Problem
        2. 7.1.3.2. Solution
        3. 7.1.3.3. How It Works
      4. 7.1.4. 7-4. Using Android Library Projects
        1. 7.1.4.1. Problem
        2. 7.1.4.2. Solution
        3. 7.1.4.3. How It Works
      5. 7.1.5. 7-5. Charting
        1. 7.1.5.1. Problem
        2. 7.1.5.2. Solution
        3. 7.1.5.3. How It Works
      6. 7.1.6. 7-6. Practical Push Messaging
        1. 7.1.6.1. Problem
        2. 7.1.6.2. Solution
        3. 7.1.6.3. How It Works
          1. 7.1.6.3.1. Client Sample
          2. 7.1.6.3.2. Testing the Client
    2. 7.2. Summary
  14. A. Scripting Layer for Android
    1. A.1. Installing SL4A
    2. A.2. Exploring SL4A
      1. A.2.1. Adding a Shell Script
      2. A.2.2. Accessing the Linux Shell
    3. A.3. Installing the Python Interpreter
    4. A.4. Scripting with Python
  15. B. Android NDK
    1. B.1. Installing the NDK
    2. B.2. Exploring the NDK
    3. B.3. Greetings from the NDK
      1. B.3.1. Building and Running NDKGreetings with the Android SDK
      2. B.3.2. Building and Running NDKGreetings with Eclipse
    4. B.4. Sampling the NDK
  16. C. App Design Guidelines
    1. C.1.
      1. C.1.1. C-1. Designing Filtered Apps
        1. C.1.1.1. Problem
        2. C.1.1.2. Solution
      2. C.1.2. C-2. Designing High-Performance Apps
        1. C.1.2.1. Problem
        2. C.1.2.2. Solution
      3. C.1.3. C-3. Designing Responsive Apps
        1. C.1.3.1. Problem
        2. C.1.3.2. Solution
      4. C.1.4. C-4. Designing Seamless Apps
        1. C.1.4.1. Problem
        2. C.1.4.2. Solution

Product information

  • Title: Android Recipes: A Problem-Solution Approach
  • Author(s):
  • Release date: May 2011
  • Publisher(s): Apress
  • ISBN: 9781430234135