O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Android Programming: The Big Nerd Ranch Guide, Third Edition

Book Description

Android Programming: The Big Nerd Ranch Guide is an introductory Android book for programmers with Java experience.
 
Based on Big Nerd Ranch’s popular Android bootcamps, this guide will lead you through the wilderness using hands-on example apps combined with clear explanations of key concepts and APIs. This book focuses on practical techniques for developing apps compatible with Android 4.4 (KitKat) through Android 7.0 (Nougat) and beyond.
 
Write and run code every step of the way, using Android Studio to create apps that integrate with other apps, download and display pictures from the web, play sounds, and more. Each chapter and app has been designed and tested to provide the knowledge and experience you need to get started in Android development.

Table of Contents

  1. Cover
  2. Learning Android
    1. Prerequisites
    2. What’s New in the Third Edition?
    3. How to Use This Book
    4. How This Book Is Organized
      1. Challenges
      2. Are you more curious?
    5. Code Style
    6. Typographical Conventions
    7. Using an eBook
    8. Android Versions
  3. The Necessary Tools
    1. Downloading and Installing Android Studio
    2. Downloading Earlier SDK Versions
    3. A Hardware Device
  4. 1. Your First Android Application
    1. App Basics
    2. Creating an Android Project
    3. Navigating in Android Studio
    4. Laying Out the UI
      1. The view hierarchy
      2. Widget attributes
      3. Creating string resources
      4. Previewing the layout
    5. From Layout XML to View Objects
      1. Resources and resource IDs
    6. Wiring Up Widgets
      1. Getting references to widgets
      2. Setting listeners
    7. Making Toasts
      1. Using code completion
    8. Running on the Emulator
    9. For the More Curious: Android Build Process
      1. Android build tools
    10. Challenges
    11. Challenge: Customizing the Toast
  5. 2. Android and Model-View-Controller
    1. Creating a New Class
      1. Generating getters and setters
    2. Model-View-Controller and Android
      1. Benefits of MVC
    3. Updating the View Layer
    4. Updating the Controller Layer
    5. Running on a Device
      1. Connecting your device
      2. Configuring your device for development
    6. Adding an Icon
      1. Adding resources to a project
      2. Referencing resources in XML
    7. Challenge: Add a Listener to the TextView
    8. Challenge: Add a Previous Button
    9. Challenge: From Button to ImageButton
  6. 3. The Activity Lifecycle
    1. Logging the Activity Lifecycle
      1. Making log messages
      2. Using Logcat
      3. Exploring the activity lifecycle by example
    2. Rotation and the Activity Lifecycle
      1. Device configurations and alternative resources
    3. Saving Data Across Rotation
      1. Overriding onSaveInstanceState(Bundle)
    4. The Activity Lifecycle, Revisited
    5. For the More Curious: Current State of Activity Cleanup
    6. For the More Curious: Log Levels and Methods
    7. Challenge: Preventing Repeat Answers
    8. Challenge: Graded Quiz
  7. 4. Debugging Android Apps
    1. Exceptions and Stack Traces
      1. Diagnosing misbehaviors
      2. Logging stack traces
      3. Setting breakpoints
      4. Using exception breakpoints
    2. Android-Specific Debugging
      1. Using Android Lint
      2. Issues with the R class
    3. Challenge: Exploring the Layout Inspector
    4. Challenge: Exploring Allocation Tracking
  8. 5. Your Second Activity
    1. Setting Up a Second Activity
      1. Creating a new activity
      2. A new activity subclass
      3. Declaring activities in the manifest
      4. Adding a cheat button to QuizActivity
    2. Starting an Activity
      1. Communicating with intents
    3. Passing Data Between Activities
      1. Using intent extras
      2. Getting a result back from a child activity
    4. How Android Sees Your Activities
    5. Challenge: Closing Loopholes for Cheaters
  9. 6. Android SDK Versions and Compatibility
    1. Android SDK Versions
    2. Compatibility and Android Programming
      1. A sane minimum
      2. Minimum SDK version
      3. Target SDK version
      4. Compile SDK version
      5. Adding code from later APIs safely
    3. Using the Android Developer Documentation
    4. Challenge: Reporting the Build Version
    5. Challenge: Limited Cheats
  10. 7. UI Fragments and the Fragment Manager
    1. The Need for UI Flexibility
    2. Introducing Fragments
    3. Starting CriminalIntent
      1. Creating a new project
      2. Two types of fragments
      3. Adding dependencies in Android Studio
      4. Creating the Crime class
    4. Hosting a UI Fragment
      1. The fragment lifecycle
      2. Two approaches to hosting
      3. Defining a container view
    5. Creating a UI Fragment
      1. Defining CrimeFragment’s layout
      2. Creating the CrimeFragment class
    6. Adding a UI Fragment to the FragmentManager
      1. Fragment transactions
      2. The FragmentManager and the fragment lifecycle
    7. Application Architecture with Fragments
      1. The reason all our activities will use fragments
    8. For the More Curious: Fragments and the Support Library
    9. For the More Curious: Why Support Fragments Are Superior
  11. 8. Displaying Lists with RecyclerView
    1. Updating CriminalIntent’s Model Layer
      1. Singletons and centralized data storage
    2. An Abstract Activity for Hosting a Fragment
      1. A generic fragment-hosting layout
      2. An abstract Activity class
    3. RecyclerView, Adapter, and ViewHolder
      1. ViewHolders and Adapters
      2. Using a RecyclerView
      3. A view to display
      4. Implementing a ViewHolder and an Adapter
    4. Binding List Items
    5. Responding to Presses
    6. For the More Curious: ListView and GridView
    7. For the More Curious: Singletons
    8. Challenge: RecyclerView ViewTypes
  12. 9. Creating User Interfaces with Layouts and Widgets
    1. Using the Graphical Layout Tool
    2. Introducing ConstraintLayout
      1. Using ConstraintLayout
      2. The graphical editor
      3. Making room
      4. Adding widgets
      5. ConstraintLayout’s inner workings
      6. Editing properties
      7. Making list items dynamic
    3. More on Layout Attributes
      1. Screen pixel densities and dp and sp
      2. Margins vs padding
      3. Styles, themes, and theme attributes
      4. Android’s design guidelines
    4. The Graphical Layout Tools and You
    5. Challenge: Formatting the Date
  13. 10. Using Fragment Arguments
    1. Starting an Activity from a Fragment
      1. Putting an extra
      2. Retrieving an extra
      3. Updating CrimeFragment’s view with Crime data
      4. The downside to direct retrieval
    2. Fragment Arguments
      1. Attaching arguments to a fragment
      2. Retrieving arguments
    3. Reloading the List
    4. Getting Results with Fragments
    5. For the More Curious: Why Use Fragment Arguments?
    6. Challenge: Efficient RecyclerView Reloading
    7. Challenge: Improving CrimeLab Performance
  14. 11. Using ViewPager
    1. Creating CrimePagerActivity
      1. ViewPager and PagerAdapter
      2. Integrating CrimePagerActivity
    2. FragmentStatePagerAdapter vs FragmentPagerAdapter
    3. For the More Curious: How ViewPager Really Works
    4. For the More Curious: Laying Out Views in Code
    5. Challenge: Restoring CrimeFragment’s Margins
    6. Challenge: Adding First and Last Buttons
  15. 12. Dialogs
    1. Creating a DialogFragment
      1. Showing a DialogFragment
      2. Setting a dialog’s contents
    2. Passing Data Between Two Fragments
      1. Passing data to DatePickerFragment
      2. Returning data to CrimeFragment
    3. Challenge: More Dialogs
    4. Challenge: A Responsive DialogFragment
  16. 13. The Toolbar
    1. AppCompat
      1. Using the AppCompat library
    2. Menus
      1. Defining a menu in XML
      2. Creating the menu
      3. Responding to menu selections
    3. Enabling Hierarchical Navigation
      1. How hierarchical navigation works
    4. An Alternative Action Item
      1. Toggling the action item title
      2. “Just one more thing...”
    5. For the More Curious: Action Bar vs Toolbar
    6. Challenge: Deleting Crimes
    7. Challenge: Plural String Resources
    8. Challenge: An Empty View for the RecyclerView
  17. 14. SQLite Databases
    1. Defining a Schema
    2. Building Your Initial Database
      1. Exploring files using Android Device Monitor
      2. Debugging database issues
    3. Gutting CrimeLab
    4. Writing to the Database
      1. Using ContentValues
      2. Inserting and updating rows
    5. Reading from the Database
      1. Using a CursorWrapper
      2. Converting to model objects
    6. For the More Curious: More Databases
    7. For the More Curious: The Application Context
    8. Challenge: Deleting Crimes
  18. 15. Implicit Intents
    1. Adding Buttons
    2. Adding a Suspect to the Model Layer
    3. Using a Format String
    4. Using Implicit Intents
      1. Parts of an implicit intent
      2. Sending a crime report
      3. Asking Android for a contact
      4. Checking for responding activities
    5. Challenge: ShareCompat
    6. Challenge: Another Implicit Intent
  19. 16. Taking Pictures with Intents
    1. A Place for Your Photo
    2. File Storage
      1. Using FileProvider
      2. Designating a picture location
    3. Using a Camera Intent
      1. Firing the intent
    4. Scaling and Displaying Bitmaps
    5. Declaring Features
    6. Challenge: Detail Display
    7. Challenge: Efficient Thumbnail Load
  20. 17. Two-Pane Master-Detail Interfaces
    1. Adding Layout Flexibility
      1. Modifying SingleFragmentActivity
      2. Creating a layout with two fragment containers
      3. Using an alias resource
      4. Creating tablet alternatives
    2. Activity: Fragment Boss
      1. Fragment callback interfaces
    3. For the More Curious: More on Determining Device Size
    4. Challenge: Adding Swipe to Dismiss
  21. 18. Localization
    1. Localizing Resources
      1. Default resources
      2. Checking string coverage using Translations Editor
      3. Targeting a region
    2. Configuration Qualifiers
      1. Prioritizing alternative resources
      2. Multiple qualifiers
      3. Finding the best-matching resources
    3. Testing Alternative Resources
    4. Challenge: Localizing Dates
  22. 19. Accessibility
    1. TalkBack
      1. Explore by Touch
      2. Linear navigation by swiping
    2. Making Non-Text Elements Readable by TalkBack
      1. Adding content descriptions
      2. Making a widget focusable
    3. Creating a Comparable Experience
      1. Using labels to provide context
    4. For the More Curious: Using Accessibility Scanner
    5. Challenge: Improving the List
    6. Challenge: Providing Enough Context for Data Entry
    7. Challenge: Announcing Events
  23. 20. Data Binding and MVVM
    1. Different Architectures: Why Bother?
    2. Creating BeatBox
      1. Simple data binding
    3. Importing Assets
    4. Getting At Assets
    5. Wiring Up Assets for Use
    6. Binding to Data
      1. Creating a ViewModel
      2. Binding to a ViewModel
      3. Observable data
    7. Accessing Assets
    8. For the More Curious: More About Data Binding
      1. Lambda expressions
      2. More syntactic sugar
      3. BindingAdapters
    9. For the More Curious: Why Assets, Not Resources
    10. For the More Curious: Non-Assets?
  24. 21. Unit Testing and Audio Playback
    1. Creating a SoundPool
    2. Loading Sounds
    3. Playing Sounds
    4. Test Dependencies
    5. Creating a Test Class
    6. Setting Up Your Test
      1. Using mocked dependencies
    7. Writing Tests
      1. Testing object interactions
    8. Data Binding Callbacks
    9. Unloading Sounds
    10. Rotation and Object Continuity
      1. Retaining a fragment
      2. Rotation and retained fragments
    11. For the More Curious: Whether to Retain
    12. For the More Curious: Espresso and Integration Testing
    13. For the More Curious: Mocks and Testing
    14. Challenge: Playback Speed Control
  25. 22. Styles and Themes
    1. Color Resources
    2. Styles
      1. Style inheritance
    3. Themes
      1. Modifying the theme
    4. Adding Theme Colors
    5. Overriding Theme Attributes
      1. Theme spelunking
    6. Modifying Button Attributes
    7. For the More Curious: More on Style Inheritance
    8. For the More Curious: Accessing Theme Attributes
  26. 23. XML Drawables
    1. Making Uniform Buttons
    2. Shape Drawables
    3. State List Drawables
    4. Layer List Drawables
    5. For the More Curious: Why Bother with XML Drawables?
    6. For the More Curious: Mipmap Images
    7. For the More Curious: 9-Patch Images
    8. Challenge: Button Themes
  27. 24. More About Intents and Tasks
    1. Setting Up NerdLauncher
    2. Resolving an Implicit Intent
    3. Creating Explicit Intents at Runtime
    4. Tasks and the Back Stack
      1. Switching between tasks
      2. Starting a new task
    5. Using NerdLauncher as a Home Screen
    6. Challenge: Icons
    7. For the More Curious: Processes vs Tasks
    8. For the More Curious: Concurrent Documents
  28. 25. HTTP and Background Tasks
    1. Creating PhotoGallery
    2. Networking Basics
      1. Asking permission to network
    3. Using AsyncTask to Run on a Background Thread
    4. You and Your Main Thread
      1. Beyond the main thread
    5. Fetching JSON from Flickr
      1. Parsing JSON text
    6. From AsyncTask Back to the Main Thread
    7. Cleaning Up AsyncTasks
    8. For the More Curious: More on AsyncTask
    9. For the More Curious: Alternatives to AsyncTask
    10. Challenge: Gson
    11. Challenge: Paging
    12. Challenge: Dynamically Adjusting the Number of Columns
  29. 26. Loopers, Handlers, and HandlerThread
    1. Preparing RecyclerView to Display Images
    2. Downloading Lots of Small Things
    3. Communicating with the Main Thread
    4. Assembling a Background Thread
    5. Messages and Message Handlers
      1. Message anatomy
      2. Handler anatomy
      3. Using handlers
      4. Passing handlers
    6. For the More Curious: AsyncTasks vs Threads
    7. For the More Curious: Solving the Image Downloading Problem
    8. For the More Curious: StrictMode
    9. Challenge: Preloading and Caching
  30. 27. Search
    1. Searching Flickr
    2. Using SearchView
      1. Responding to SearchView user interactions
    3. Simple Persistence with Shared Preferences
    4. Polishing Your App
    5. Challenge: Polishing Your App Some More
  31. 28. Background Services
    1. Creating an IntentService
    2. What Services Are For
      1. Safe background networking
    3. Looking for New Results
    4. Delayed Execution with AlarmManager
      1. Being a good citizen: using alarms the right way
      2. PendingIntent
      3. Managing alarms with PendingIntent
    5. Controlling Your Alarm
    6. Notifications
    7. Challenge: Notifications on Android Wear
    8. For the More Curious: Service Details
      1. What a service does (and does not do)
      2. A service’s lifecycle
      3. Non-sticky services
      4. Sticky services
      5. Bound services
    9. For the More Curious: JobScheduler and JobServices
      1. JobScheduler and the future of background work
    10. Challenge: Using JobService on Lollipop
    11. For the More Curious: Sync Adapters
  32. 29. Broadcast Intents
    1. Regular Intents vs Broadcast Intents
    2. Receiving a System Broadcast: Waking Up on Boot
      1. Creating and registering a standalone receiver
      2. Using receivers
    3. Filtering Foreground Notifications
      1. Sending broadcast intents
      2. Creating and registering a dynamic receiver
      3. Limiting broadcasts to your app using private permissions
      4. Passing and receiving data with ordered broadcasts
    4. Receivers and Long-Running Tasks
    5. For the More Curious: Local Events
      1. Using EventBus
      2. Using RxJava
    6. For the More Curious: Detecting the Visibility of Your Fragment
  33. 30. Browsing the Web and WebView
    1. One Last Bit of Flickr Data
    2. The Easy Way: Implicit Intents
    3. The Harder Way: WebView
      1. Using WebChromeClient to spruce things up
    4. Proper Rotation with WebView
      1. Dangers of handling configuration changes
    5. For the More Curious: Injecting JavaScript Objects
    6. For the More Curious: WebView Updates
    7. Challenge: Using the Back Button for Browser History
    8. Challenge: Supporting Non-HTTP Links
  34. 31. Custom Views and Touch Events
    1. Setting Up the DragAndDraw Project
    2. Creating a Custom View
      1. Creating BoxDrawingView
    3. Handling Touch Events
      1. Tracking across motion events
    4. Rendering Inside onDraw(Canvas)
    5. Challenge: Saving State
    6. Challenge: Rotating Boxes
  35. 32. Property Animation
    1. Building the Scene
    2. Simple Property Animation
      1. View transformation properties
      2. Using different interpolators
      3. Color evaluation
    3. Playing Animators Together
    4. For the More Curious: Other Animation APIs
      1. Legacy animation tools
      2. Transitions
    5. Challenges
  36. 33. Locations and Play Services
    1. Locations and Libraries
      1. Google Play Services
    2. Creating Locatr
    3. Play Services and Location Testing on Emulators
      1. Mock location data
    4. Building Out Locatr
    5. Setting Up Google Play Services
      1. Location permissions
    6. Using Google Play Services
    7. Flickr Geosearch
    8. Getting a Location Fix
    9. Asking for Permission at Runtime
      1. Checking for permissions
    10. Find and Display an Image
    11. Challenge: Permissions Rationale
    12. Challenge: Progress
  37. 34. Maps
    1. Importing Play Services Maps
    2. Mapping on Android
    3. Getting a Maps API Key
    4. Setting Up Your Map
    5. Getting More Location Data
    6. Working with Your Map
      1. Drawing on the map
    7. For the More Curious: Teams and API Keys
  38. 35. Material Design
    1. Material Surfaces
      1. Elevation and Z values
      2. State list animators
    2. Animation Tools
      1. Circular reveal
      2. Shared element transitions
    3. View Components
      1. Cards
      2. Floating action buttons
      3. Snackbars
    4. More on Material Design
  39. 36. Afterword
    1. The Final Challenge
    2. Shameless Plugs
    3. Thank You
  40. Index