You are previewing Android Cookbook, 2nd Edition.
O'Reilly logo
Android Cookbook, 2nd Edition

Book Description

This cookbook doesn't just teach you how to build Android apps; it also gives you the recipes you need to build real-world applications. Written by the author of the best-selling Java Cookbook, and with contributions from many members of the Android community, this book shows you how to do everything: working with multitouch, dealing with the phone (and the camera, accelerometer, GPS, and other built-in devices), working with web services, packaging an app for sale in the Android Market, and more.

This revised second edition includes new recipes on JSON, material design, and Android Studio IDE. If this book doesn't show you how to do it, you probably don't need it. The Android is gradually overtaking the iPhone as the hottest platform in mobile computing. Make sure you're on the bandwagon.

Table of Contents

  1. Preface
    1. Conventions Used in This Book
    2. Getting and Using the Code Examples
    3. Safari® Books Online
    4. How to Contact Us
    5. Acknowledgments
    6. Content Updates
  2. Getting Started
    1. 1.1. Understanding the Android Application Architecture
    2. 1.2. Understanding the Android Activity Life Cycle
    3. 1.3. Learning About Android Releases
    4. 1.4. Learning the Java Language
    5. 1.5. Creating a “Hello, World” Application from the Command Line
    6. 1.6. Creating a “Hello, World” App with Apache Maven
    7. 1.7. Choosing an IDE for Android Development
    8. 1.8. Setting Up Android Studio
    9. 1.9. Installing Platform Editions and Keeping the SDK Updated
    10. 1.10. Creating a “Hello, World” App using Android Studio
    11. 1.11. Converting an Eclipse ADT Project to Android Studio
    12. 1.12. Preserving History while Converting from Eclipse to Android Studio
    13. 1.13. Building an Android Application both with Eclipse and with Android Studio
    14. 1.14. Setting Up Eclipse with AndMore (Replacing ADT)
    15. 1.15. Creating a “Hello, World” Application using Eclipse
    16. 1.16. Installing the Eclipse Marketplace Client in your Eclipse
    17. 1.17. Upgrading a Project from Eclipse ADT to Eclipse AndMore
    18. 1.18. Using Java 8 Features in Android Studio 2
    19. 1.19. Controlling Emulators/Devices Using Command-Line ADB
    20. 1.20. Sharing Java Classes from Another Eclipse Project
    21. 1.21. Referencing Libraries to Implement External Functionality
    22. 1.22. Using New Features on Old Devices via the Compatibility Libraries
    23. 1.23. Using SDK Samples to Help Avoid Head Scratching
    24. 1.24. Taking a Screenshot/Video from the Emulator/Android Device
    25. 1.25. Program: A Simple CountDownTimer Example
    26. 1.26. Program: Tipster, a Tip Calculator for the Android OS
  3. Designing a Successful Application
    1. 2.1. Exception Handling
    2. 2.2. Requesting Android Permissions at Run Time
    3. 2.3. Accessing Android’s Application Object as a “Singleton”
    4. 2.4. Keeping Data When the User Rotates the Device
    5. 2.5. Monitoring the Battery Level of an Android Device
    6. 2.6. Creating Splash Screens in Android
    7. 2.7. Designing a Conference/Camp/Hackathon/Institution App
    8. 2.8. Using Google Analytics in an Android Application
    9. 2.9. Setting First-Run Preferences
    10. 2.10. Formatting the Time and Date for Display
    11. 2.11. Simplifying Date/Time Calculations with the Java 8 <em xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">java.time</em> API API
    12. 2.12. Controlling Input with KeyListeners
    13. 2.13. Backing Up Android Application Data
    14. 2.14. Using Hints Instead of Tool Tips
  4. Application Testing
    1. 3.1. Setting Up an Android Virtual Device (AVD) for App Testing
    2. 3.2. Testing on a Huge Range of Devices with Cloud-based Testing
    3. 3.3. Testing with Eclipse and JUnit
    4. 3.4. Testing with Android Studio and JUnit
    5. 3.5. Testing with Robolectric and JUnit 4
    6. 3.6. Testing with ATSL, Espresso and JUnit 4
    7. 3.7. Troubleshooting Application Crashes
    8. 3.8. Debugging Using Log.d and LogCat
    9. 3.9. Getting Bug Reports Automatically with Crash Reporting
    10. 3.10. Using a Local Runtime Application Log for Analysis of Field Errors or Situations
    11. 3.11. Reproducing Activity Life-Cycle Scenarios for Testing
    12. 3.12. Keeping Your App Snappy with StrictMode
    13. 3.13. Static Code Testing with Android Lint
    14. 3.14. Dynamic Testing with the Monkey Program
    15. 3.15. Sending Text Messages and Placing Calls Between AVDs
  5. Inter-/Intra-Process Communication
    1. 4.1. Opening a Web Page, Phone Number, or Anything Else with an Intent
    2. 4.2. Emailing Text from a View
    3. 4.3. Sending an Email with Attachments
    4. 4.4. Pushing String Values Using Intent.putExtra()
    5. 4.5. Retrieving Data from a Sub-Activity Back to Your Main Activity
    6. 4.6. Keeping a Background Service Running While Other Apps Are on Display
    7. 4.7. Sending/Receiving a Broadcast Message
    8. 4.8. Starting a Service After Device Reboot
    9. 4.9. Creating a Responsive Application Using Threads
    10. 4.10. Using AsyncTask to Do Background Processing
    11. 4.11. Sending Messages Between Threads Using an Activity Thread Queue and Handler
    12. 4.12. Creating an Android Epoch HTML/JavaScript Calendar
  6. Graphics
    1. 5.1. Using a Custom Font
    2. 5.2. Drawing a Spinning Cube with OpenGL ES
    3. 5.3. Adding Controls to the OpenGL Spinning Cube
    4. 5.4. Freehand Drawing Smooth Curves
    5. 5.5. Taking a Picture Using an Intent
    6. 5.6. Taking a Picture Using android.media.Camera
    7. 5.7. Scanning a Barcode or QR Code with the Google ZXing Barcode Scanner
    8. 5.8. Using AndroidPlot to Display Charts and Graphs
    9. 5.9. Using Inkscape to Create an Android Launcher Icon from OpenClipArt.org
    10. 5.10. Using Paint.NET to Create Launcher Icons from OpenClipArt.org
    11. 5.11. Using Nine Patch Files
    12. 5.12. Creating HTML5 Charts with Android RGraph
    13. 5.13. Adding a Simple Raster Animation
    14. 5.14. Using Pinch to Zoom
  7. Graphical User Interface
    1. 6.1. Understanding and Following User Interface Guidelines
    2. 6.2. Looking Good with Material Design
    3. 6.3. Choosing a Layout Manager (a.k.a. ViewGroup) and Arranging Components
    4. 6.4. Handling Configuration Changes by Decoupling the View from the Model
    5. 6.5. Controlling the Action Bar
    6. 6.6. Adding a Share Action to your action bar
    7. 6.7. Building Modern UIs with the Fragment API
    8. 6.8. Creating a Button and Its Click Event Listener
    9. 6.9. Enhancing UI Design Using Image Buttons
    10. 6.10. Using FloatingActionButton
    11. 6.11. Wiring Up an Event Listener in Many Different Ways
    12. 6.12. Using CheckBoxes and RadioButtons
    13. 6.13. Using Card Widgets
    14. 6.14. Offering a Drop-Down Chooser via the Spinner Class
    15. 6.15. Handling Long-Press/Long-Click Events
    16. 6.16. Displaying Text Fields with TextView and EditText
    17. 6.17. Constraining EditText Values with Attributes and the TextWatcher Interface
    18. 6.18. Implementing AutoCompleteTextView
    19. 6.19. Feeding AutoCompleteTextView Using an SQLite Database Query
    20. 6.20. Turning Edit Fields into Password Fields
    21. 6.21. Changing the Enter Key to “Next” on the Soft Keyboard
    22. 6.22. Processing Key-Press Events in an Activity
    23. 6.23. Let Them See Stars: Using RatingBar
    24. 6.24. Making a View Shake
    25. 6.25. Providing Haptic Feedback
    26. 6.26. Navigating Different Activities Within a TabView
    27. 6.27. Formatting Numbers
    28. 6.28. Formatting with Correct Plurals
    29. 6.29. Creating a Loading Screen That Will Appear Between Two Activities
    30. 6.30. Adding a Border with Rounded Corners to a Layout
    31. 6.31. Detecting Gestures in Android
    32. 6.32. Creating a Simple App Widget
  8. GUI Alerts: Menus, Dialogs, Toasts, Snackbars and Notifications
    1. 7.1. Alerting the User with Toast and Snackbar
    2. 7.2. Customizing the Appearance of a Toast
    3. 7.3. Creating and Displaying a Menu
    4. 7.4. Handling Choice Selection in a Menu
    5. 7.5. Creating a Submenu
    6. 7.6. Creating a Pop-up/Alert Dialog
    7. 7.7. Using a Timepicker Widget
    8. 7.8. Creating an iPhone-like Wheel Picker for Selection
    9. 7.9. Creating a Tabbed Dialog
    10. 7.10. Creating a ProgressDialog
    11. 7.11. Creating a Custom Dialog with Buttons, Images, and Text
    12. 7.12. Creating a Reusable “About Box” Class
    13. 7.13. Creating a Notification in the Status Bar
  9. Other GUI Elements: Lists and Views
    1. 8.1. Building List-based Applications with RecyclerView
    2. 8.2. Building List-Based Applications with ListView
    3. 8.3. Creating a “No Data” View for ListViews
    4. 8.4. Creating an Advanced ListView with Images and Text
    5. 8.5. Using Section Headers in ListViews
    6. 8.6. Keeping the ListView with the User’s Focus
    7. 8.7. Writing a Custom List Adapter
    8. 8.8. Using a SearchView to Search Through Data in a ListView
    9. 8.9. Handling Orientation Changes: From ListView Data Values to Landscape Charting
  10. Multimedia
    1. 9.1. Playing a YouTube Video
    2. 9.2. Capturing Video Using MediaRecorder
    3. 9.3. Using Android’s Face Detection Capability
    4. 9.4. Playing Audio from a File
    5. 9.5. Playing Audio Without Interaction
    6. 9.6. Using Speech to Text
    7. 9.7. Making the Device Speak with Text-to-Speech
  11. Data Persistence
    1. 10.1. Reading and Writing Files in Internal and External Storage
    2. 10.2. Getting File and Directory Information
    3. 10.3. Reading a File Shipped with the App Rather Than in the Filesystem
    4. 10.4. Getting Space Information About the SD Card
    5. 10.5. Providing User Preference Activity
    6. 10.6. Checking the Consistency of Default Shared Preferences
    7. 10.7. Using an SQLite Database in an Android Application
    8. 10.8. Performing Advanced Text Searches on an SQLite Database
    9. 10.9. Working with Dates in SQLite
    10. 10.10. Exposing non-SQL data as an SQL Cursor
    11. 10.11. Displaying Data with a CursorLoader
    12. 10.12. Parsing JSON Using JSONObject
    13. 10.13. Parsing an XML Document Using the DOM API
    14. 10.14. Storing and Retrieving Data via a Content Provider
    15. 10.15. Writing a Content Provider
    16. 10.16. Adding a Contact through the Contacts Content provider
    17. 10.17. Reading Contact Data Using a Content Provider
    18. 10.18. Implementing Drag and Drop
    19. 10.19. Sharing Files via a FileProvider
    20. 10.20. Backing your SQLite Data to the Cloud with a SyncAdapter
    21. 10.21. Storing Data In the Cloud with Google Firebase
  12. Telephone Applications
    1. 11.1. Doing Something When the Phone Rings
    2. 11.2. Processing Outgoing Phone Calls
    3. 11.3. Dialing the Phone
    4. 11.4. Sending Single-Part or Multipart SMS Messages
    5. 11.5. Receiving an SMS Message
    6. 11.6. Using Emulator Controls to Send SMS Messages to the Emulator
    7. 11.7. Using Android’s TelephonyManager to Obtain Device Information
  13. Networked Applications
    1. 12.1. Consuming a RESTful Web Service using URLConnection
    2. 12.2. Consuming a RESTful Web Service with Volley
    3. 12.3. Notifying your App with Google Cloud Messaging (GCM) “Push Messaging”
    4. 12.4. Extracting Information from Unstructured Text Using Regular Expressions
    5. 12.5. Parsing RSS/Atom Feeds Using ROME
    6. 12.6. Using MD5 to Digest Clear Text
    7. 12.7. Converting Text into Hyperlinks
    8. 12.8. Accessing a Web Page Using WebView
    9. 12.9. Customizing a WebView
    10. 12.10. Writing an Inter-Process Communication Service