You are previewing Android Application Development Cookbook - Second Edition.
O'Reilly logo
Android Application Development Cookbook - Second Edition

Book Description

Over 100 recipes to help you solve the most common problems faced by Android Developers today

About This Book

  • Find the answers to your common Android programming problems, from set up to security, to help you deliver better applications, faster

  • Uncover the latest features of Android Marshmallow to make your applications stand out

  • Get up to speed with Android Studio 1.4 - the first Android Studio based on the IntelliJ IDE from JetBrains

  • Who This Book Is For

    If you are new to Android development and want to take a hands-on approach to learning the framework, or if you are an experienced developer in need of clear working code to solve the many challenges in Android development, you can benefit from this book. Either way, this is a resource you’ll want to keep at your desk for a quick reference to solve new problems as you tackle more challenging projects.

    What You Will Learn

  • Along with Marshmallow, get hands-on working with Google’s new Android Studio IDE

  • Develop applications using the latest Android framework while maintaining backward-compatibility with the support library

  • Master Android programming best practices from the recipes

  • Create exciting and engaging applications using knowledge gained from recipes on graphics, animations, and multimedia

  • Work through succinct steps on specifics that will help you complete your project faster

  • Keep your app responsive (and prevent ANRs) with examples on the AsynchTask class

  • Utilize Google Speech Recognition APIs for your app.

  • Make use of Google Cloud Messaging (GCM) to create Push Notifications for your users

  • Get a better understanding of the Android framework through detailed explanations

  • In Detail

    The Android OS has the largest installation base of any operating system in the world; there has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community!

    This “cookbook” will make it easy for you to jump to a topic of interest and get what you need to implement the feature in your own application. If you are new to Android and learn best by “doing,” then this book will provide many topics of interest.

    Starting with the basics of Android development, we move on to more advanced concepts, and we’ll guide you through common tasks developers struggle to solve. The first few chapters cover the basics including Activities, Layouts, Widgets, and the Menu. From there, we cover fragments and data storage (including SQLite), device sensors, the camera, and GPS. Then we move on more advanced topics such as graphics and animation (including OpenGL), multi-threading with AsyncTask, and Internet functionality with Volley. We’ll also demonstrate Google Maps and Google Cloud Messaging (also known as Push Notifications) using the Google API Library.

    Finally, we’ll take a look at several online services designed especially for Android development. Take your application big-time with full Internet web services without having to become a server admin by leveraging the power of Backend as a Service (BaaS) providers.

    Style and approach

    This book progresses from the fundamentals of Android Development to more advanced concepts, with recipes to solve the most common problems faced by developers. This cookbook makes it easy to jump to specific topics of interest, where you’ll find simple steps to implement the solution and get a clear explanation of how it works.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Android Application Development Cookbook Second Edition
      1. Table of Contents
      2. Android Application Development Cookbook Second Edition
      3. Credits
      4. Disclaimer
      5. About the Authors
      6. About the Reviewer
      7. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why Subscribe?
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Activities
        1. Introduction
        2. Declaring an activity
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Starting a new activity with an intent object
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Switching between activities
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Passing data to another activity
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Returning a result from an activity
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Saving an activity's state
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Storing persistent activity data
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using more than one preference file
          5. See also
        9. Understanding the activity lifecycle
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Shutting down an activity
      10. 2. Layouts
        1. Introduction
        2. Defining and inflating a layout
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Using RelativeLayout
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Using LinearLayout
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating tables – TableLayout and GridLayout
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Using ListView, GridView, and Adapters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Changing layout properties during runtime
          1. Getting ready
          2. How to do it....
          3. How it works...
        8. Optimizing layouts with the Hierarchy Viewer
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      11. 3. Views, Widgets, and Styles
        1. Introduction
        2. Inserting a widget into a layout
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Using graphics to show button state
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using designated folders for screen-specific resources
          5. See also
        4. Creating a widget at runtime
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Creating a custom component
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Applying a style to a View
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Turning a style into a theme
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Selecting theme based on the Android version
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
      12. 4. Menus
        1. Introduction
        2. Creating an Options menu
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using a menu item to launch an activity
            2. Creating sub menus
            3. Grouping menu items
          5. See also
        3. Modifying menus and menu items during runtime
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Enabling Contextual Action Mode for a view
          1. Creating a Floating Context Menu
          2. Getting ready
          3. How to do it...
          4. How it works...
          5. There's more...
          6. See also
        5. Using Contextual Batch Mode with a ListView
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Creating a pop-up menu
          1. Getting ready
          2. How to do it...
          3. How it works...
      13. 5. Exploring Fragments, AppWidgets, and the System UI
        1. Introduction
        2. Creating and using a Fragment
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Adding and removing Fragments during runtime
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Passing data between Fragments
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating a shortcut on the Home screen
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Creating a Home screen widget
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Adding Search to the Action Bar
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Showing your app full screen
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Sticky Immersion
            2. Dimming the System UI
            3. Setting the Action Bar as an Overlay
            4. Translucent system bars
          5. See also
      14. 6. Working with Data
        1. Introduction
        2. Storing simple data
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Read and write a text file to internal storage
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Cache Files
          5. See also
        4. Read and write a text file to external storage
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Getting public folders
            2. Checking available space
            3. Deleting a file
            4. Working with directories
            5. Preventing files from being included in galleries
          5. See also
        5. Including resource files in your project
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Creating and using an SQLite database
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Upgrading a database
          5. See also
        7. Access data in the background using a Loader
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      15. 7. Alerts and Notifications
        1. Introduction
        2. Lights, Action, and Sound – getting the user's attention!
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Creating a Toast using a custom layout
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Displaying a message box with AlertDialog
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Add an icon
            2. Using a list
            3. Custom Layout
        5. Displaying a progress dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Lights, Action, and Sound Redux using Notifications
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Adding a button to the notification using addAction()
            2. Expanded notifications
            3. Lock screen notifications
          5. See also
        7. Creating a Media Player Notification
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Making a Flashlight with a Heads-Up Notification
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      16. 8. Using the Touchscreen and Sensors
        1. Introduction
        2. Listening for click and long-press events
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Recognizing tap and other common gestures
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Pinch-to-zoom with multi-touch gestures
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Swipe-to-Refresh
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Listing available sensors – an introduction to the Android Sensor Framework
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Reading sensor data – using the Android Sensor Framework events
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Environment sensors
            2. Position sensors
            3. Motion sensors
          5. See also
        8. Reading device orientation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Getting current device rotation
          5. See also
      17. 9. Graphics and Animation
        1. Introduction
        2. Scaling down large images to avoid Out of Memory exceptions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. A transition animation – defining scenes and applying a transition
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Creating the start scene
            2. Creating the transition:
            3. Defining the ending scene:
            4. Starting the transition:
          4. There's more...
          5. See also
        4. Creating a Compass using sensor data and RotateAnimation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating a slideshow with ViewPager
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Creating a Setup Wizard
          5. See also
        6. Creating a Card Flip Animation with Fragments
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Creating a Zoom Animation with a Custom Transition
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Getting the default animation duration
          5. See also
      18. 10. A First Look at OpenGL ES
        1. Introduction
        2. Set up the OpenGL ES environment
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Declaring OpenGL in the Android Manifest
            2. Extending the GLSurfaceView class
            3. Creating an OpenGL rendered class
          4. There's more...
        3. Drawing shapes on GLSurfaceView
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Applying Projection and Camera View while drawing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Moving the triangle with rotation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. The render mode
        6. Rotating the triangle with user input
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      19. 11. Multimedia
        1. Introduction
        2. Playing sound effects with SoundPool
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Playing audio with MediaPlayer
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Playing music in the background
            2. Using hardware volume keys to control your app's audio volume
          5. See also
        4. Responding to hardware media controls in your app
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Checking the hardware being used
          5. See also
        5. Taking a photo with the default camera app
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Calling the default video app
          5. See also
        6. Taking a picture using the (old) Camera API
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Setting the camera parameters
          5. See also
        7. Taking a picture using the Camera2 (the new) API
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Setting up the preview
            2. Capturing the image
          4. There's more...
          5. See also
      20. 12. Telephony, Networks, and the Web
        1. Introduction
        2. How to make a phone call
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Monitoring phone call events
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. How to send SMS (text) messages
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Multipart messages
            2. Delivery status notification
          5. See also
        5. Receiving SMS messages
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Reading existing SMS messages
          5. See also
        6. Displaying a web page in your application
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Controlling page navigation
            2. How to enable JavaScript
            3. Enable built-in zoom
          5. See also
        7. Checking online status and connection type
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Monitoring network state changes
          5. See also
        8. Getting started with Volley for Internet requests
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Canceling a Volley request
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Using Volley to request a JSON response
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        11. Using Volley to request an image
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Creating a Volley singleton
          5. See also
        12. Using Volley's NetworkImageView and ImageLoader
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      21. 13. Getting Location and Using Geofencing
        1. Introduction
        2. How to get the last location
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Mock locations
          5. See also
        3. Resolving problems reported with the GoogleApiClient OnConnectionFailedListener
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. How to receive location updates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Stop receiving location updates
          5. See also
        5. Create and monitor a Geofence
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      22. 14. Getting your app ready for the Play Store
        1. Introduction
        2. The new Android 6.0 Run-Time permission model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. How to schedule an alarm
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Cancel the alarm
            2. Repeating alarm
          5. See also
        4. Receive notification of device boot
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Using the AsyncTask for background work
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Parameter types
            2. Cancel the task
          5. See also
        6. Adding speech recognition to your app
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Push Notification using GCM
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Simple testing option
          5. See also
        8. How to add Google sign-in to your app
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Localization resources
          5. See also
      23. 15. The Backend as a Service Options
        1. Introduction
        2. App42
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Backendless
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Buddy
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Firebase
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Kinvey
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      24. Index