Android Cookbook, 2nd Edition

Book description

Jump in and build working Android apps with the help of more than 230 tested recipes. The second edition of this acclaimed cookbook includes recipes for working with user interfaces, multitouch gestures, location awareness, web services, and specific device features such as the phone, camera, and accelerometer. You also get useful info on packaging your app for the Google Play Market.

Ideal for developers familiar with Java, Android basics, and the Java SE API, this book features recipes contributed by more than three dozen Android developers. Each recipe provides a clear solution and sample code you can use in your project right away. Among numerous topics, this cookbook helps you:

  • Get started with the tooling you need for developing and testing Android apps
  • Create layouts with Android’s UI controls, graphical services, and pop-up mechanisms
  • Build location-aware services on Google Maps and OpenStreetMap
  • Control aspects of Android’s music, video, and other multimedia capabilities
  • Work with accelerometers and other Android sensors
  • Use various gaming and animation frameworks
  • Store and retrieve persistent data in files and embedded databases
  • Access RESTful web services with JSON and other formats
  • Test and troubleshoot individual components and your entire application

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Conventions Used in This Book
    2. Getting and Using the Code Examples
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. Acknowledgments
  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. Setting Up Android Studio
    6. 1.6. Installing Platform Editions and Keeping the SDK Updated
    7. 1.7. Creating a “Hello, World” App Using Android Studio
    8. 1.8. Using the Kotlin Language in Building Android Apps
    9. 1.9. Converting an Eclipse ADT Project to Android Studio
    10. 1.10. Preserving History While Converting from Eclipse to Android Studio
    11. 1.11. Controlling Emulators/Devices Using Command-Line ADB
    12. 1.12. Referencing Libraries to Implement External Functionality
    13. 1.13. Using New Features on Old Devices via the Compatibility Libraries
    14. 1.14. Using SDK Samples to Help Avoid Head Scratching
    15. 1.15. Taking a Screenshot/Video from the Emulator/Android Device
    16. 1.16. Program: A Simple CountDownTimer Example
    17. 1.17. 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 Runtime
    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 Numbers
    11. 2.11. Formatting with Correct Plurals
    12. 2.12. Formatting the Time and Date for Display
    13. 2.13. Simplifying Date/Time Calculations with the Java 8 java.time API
    14. 2.14. Controlling Input with KeyListeners
    15. 2.15. Backing Up Android Application Data
    16. 2.16. 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 Wide Range of Devices with Cloud-Based Testing
    3. 3.3. Testing with Android Studio and JUnit
    4. 3.4. Testing with Robolectric and JUnit 4
    5. 3.5. Testing with ATSL, Espresso, and JUnit 4
    6. 3.6. Troubleshooting Application Crashes
    7. 3.7. Debugging Using Log.d() and LogCat
    8. 3.8. Getting Bug Reports Automatically with Crash Reporting
    9. 3.9. Using a Local Runtime Application Log for Analysis of Field Errors or Situations
    10. 3.10. Reproducing Activity Life-Cycle Scenarios for Testing
    11. 3.11. Keeping Your App Snappy with StrictMode
    12. 3.12. Static Code Testing with Android Lint
    13. 3.13. Dynamic Testing with the Monkey Program
    14. 3.14. 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 Subactivity 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 a 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 a 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. Creating a Loading Screen that Will Appear Between Two Activities
    28. 6.28. Adding a Border with Rounded Corners to a Layout
    29. 6.29. Detecting Gestures in Android
    30. 6.30. 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 WheelPicker 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 a Preference Activity
    6. 10.6. Checking the Consistency of Default Shared Preferences
    7. 10.7. Using a SQLite Database in an Android Application
    8. 10.8. Performing Advanced Text Searches on a SQLite Database
    9. 10.9. Working with Dates in SQLite
    10. 10.10. Exposing Non-SQL Data as a 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 Up 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 a URLConnection
    2. 12.2. Consuming a RESTful Web Service with Volley
    3. 12.3. Notifying Your App with Google Cloud Messaging “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 a WebView
    9. 12.9. Customizing a WebView
    10. 12.10. Writing an Inter-Process Communication Service
  14. Gaming and Animation
    1. 13.1. Building an Android Game Using flixel-gdx
    2. 13.2. Building an Android Game Using AndEngine
    3. 13.3. Processing Timed Keyboard Input
  15. Social Networking
    1. 14.1. Authenticating Users with OAUTH2
    2. 14.2. Integrating Social Networking Using HTTP
    3. 14.3. Loading a User’s Twitter Timeline Using HTML or JSON
  16. Location and Map Applications
    1. 15.1. Getting Location Information
    2. 15.2. Accessing GPS Information in Your Application
    3. 15.3. Mocking GPS Coordinates on a Device
    4. 15.4. Using Geocoding and Reverse Geocoding
    5. 15.5. Getting Ready for Google Maps API V2 Development
    6. 15.6. Using the Google Maps API V2
    7. 15.7. Displaying Map Data Using OpenStreetMap
    8. 15.8. Creating Overlays in OpenStreetMap Maps
    9. 15.9. Using a Scale on an OpenStreetMap Map
    10. 15.10. Handling Touch Events on an OpenStreetMap Overlay
    11. 15.11. Getting Location Updates with OpenStreetMap Maps
  17. Accelerometer
    1. 16.1. Checking for the Presence or Absence of a Sensor
    2. 16.2. Using the Accelerometer to Detect Shaking
    3. 16.3. Checking Whether a Device Is Facing Up or Down
    4. 16.4. Reading the Temperature Sensor
  18. Bluetooth
    1. 17.1. Enabling Bluetooth and Making the Device Discoverable
    2. 17.2. Connecting to a Bluetooth-Enabled Device
    3. 17.3. Accepting Connections from a Bluetooth Device
    4. 17.4. Implementing Bluetooth Device Discovery
  19. System and Device Control
    1. 18.1. Accessing Phone Network/Connectivity Information
    2. 18.2. Obtaining Information from the Manifest File
    3. 18.3. Changing Incoming Call Notification to Silent, Vibrate, or Normal
    4. 18.4. Copying Text and Getting Text from the Clipboard
    5. 18.5. Using LED-Based Notifications
    6. 18.6. Making the Device Vibrate
    7. 18.7. Determining Whether a Given Application Is Running
  20. All the World’s Not Java: Other Programming Languages and Frameworks
    1. 19.1. Learning About Cross-Platform Solutions
    2. 19.2. Running Shell Commands from Your Application
    3. 19.3. Running Native C/C++ Code with JNI on the NDK
    4. 19.4. Getting Started with SL4A, the Scripting Layer for Android
    5. 19.5. Creating Alerts in SL4A
    6. 19.6. Fetching Your Google Documents and Displaying Them in a ListView Using SL4A
    7. 19.7. Sharing SL4A Scripts in QR Codes
    8. 19.8. Using Native Handset Functionality from a WebView via JavaScript
    9. 19.9. Building a Cross-Platform App with Xamarin
    10. 19.10. Creating a Cross-Platform App Using PhoneGap/Cordova
    11. 19.11. Creating a Cross-Platform App Using Flutter
  21. All the World’s Not English: Strings and Internationalization
    1. 20.1. Internationalizing Application Text
    2. 20.2. Finding and Translating Strings
    3. 20.3. Handling the Nuances of strings.xml
  22. Packaging, Deploying, and Distributing/Selling Your App
    1. 21.1. Creating a Signing Certificate and Using It to Sign Your Application
    2. 21.2. Distributing Your Application via the Google Play Store
    3. 21.3. Distributing Your Application via Other App Stores
    4. 21.4. Monetizing Your App with AdMob
    5. 21.5. Obfuscating and Optimizing with ProGuard
    6. 21.6. Hosting Your App on Your Own Server
    7. 21.7. Creating a “Self-Updating” App
    8. 21.8. Providing a Link to Other Published Apps in the Google Play Store
  23. Index
  24. About the Author

Product information

  • Title: Android Cookbook, 2nd Edition
  • Author(s): Ian F. Darwin
  • Release date: May 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449374488