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, 4th 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 Bootcamp, 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 Oreo and Android "P".

 

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. Title Page
  2. Dedication
  3. Acknowledgments
  4. Table of Contents
  5. Learning Android
    1. Prerequisites
    2. What’s New in the Fourth Edition?
      1. Kotlin vs Java
    3. How to Use This Book
    4. How This Book Is Organized
      1. Challenges
      2. Are you more curious?
    5. Typographical Conventions
    6. Using an eBook
    7. Android Versions
  6. The Necessary Tools
    1. Downloading and Installing Android Studio
    2. Downloading Earlier SDK Versions
    3. A Hardware Device
  7. 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
        1. android:layout_width and android:layout_height
        2. android:orientation
        3. android:text
      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
    8. Running on the Emulator
    9. For the More Curious: The Android Build Process
      1. Android build tools
    10. Challenges
    11. Challenge: Customizing the Toast
  8. 2. Android and Model-View-Controller
    1. Creating a New Class
    2. Model-View-Controller and Android
      1. Deciding to use MVC
    3. Updating the View Layer
    4. Updating the Controller Layer
    5. Adding an Icon
      1. Adding resources to a project
      2. Referencing resources in XML
    6. Screen Pixel Densities
    7. Running on a Device
    8. Challenge: Add a Listener to the TextView
    9. Challenge: Add a Previous Button
    10. Challenge: From Button to ImageButton
  9. 3. The Activity Lifecycle
    1. Rotating GeoQuiz
    2. Activity States and Lifecycle Callbacks
    3. Logging the Activity Lifecycle
      1. Making log messages
      2. Using Logcat
    4. Exploring How the Activity Lifecycle Responds to User Actions
      1. Temporarily leaving an activity
      2. Finishing an activity
      3. Rotating an activity
    5. Device Configuration Changes and the Activity Lifecycle
      1. Creating a landscape layout
    6. For the More Curious: UI Updates and Multi-Window Mode
    7. For the More Curious: Log Levels
    8. Challenge: Preventing Repeat Answers
    9. Challenge: Graded Quiz
  10. 4. Persisting UI State
    1. Including the ViewModel Dependency
    2. Adding a ViewModel
      1. ViewModel lifecycle and ViewModelProvider
      2. Add data to your ViewModel
    3. Saving Data Across Process Death
      1. Overriding onSaveInstanceState(Bundle)
      2. Saved instance state and activity records
    4. ViewModel vs Saved Instance State
    5. For the More Curious: Jetpack, AndroidX, and Architecture Components
    6. For the More Curious: Avoiding a Half-Baked Solution
  11. 5. Debugging Android Apps
    1. Exceptions and Stack Traces
      1. Diagnosing misbehaviors
      2. Logging stack traces
      3. Setting breakpoints
    2. Android-Specific Debugging
      1. Using Android Lint
      2. Issues with the R class
    3. Challenge: Exploring the Layout Inspector
    4. Challenge: Exploring the Profiler
  12. 6. 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 MainActivity
    2. Starting an Activity
      1. Communicating with intents
        1. Explicit and implicit intents
    3. Passing Data Between Activities
      1. Using intent extras
      2. Getting a result back from a child activity
        1. Setting a result
        2. Sending back an intent
        3. Handling a result
    4. How Android Sees Your Activities
    5. Challenge: Closing Loopholes for Cheaters
    6. Challenge: Tracking Cheat Status by Question
  13. 7. 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
      6. Jetpack libraries
    3. Using the Android Developer Documentation
    4. Challenge: Reporting the Device’s Android Version
    5. Challenge: Limited Cheats
  14. 8. UI Fragments and the Fragment Manager
    1. The Need for UI Flexibility
    2. Introducing Fragments
    3. Starting CriminalIntent
      1. Creating a new project
    4. Creating a Data Class
    5. Creating a UI Fragment
      1. Defining CrimeFragment’s layout
      2. Creating the CrimeFragment class
        1. Different types of fragments
        2. Implementing fragment lifecycle functions
        3. Wiring up widgets in a fragment
    6. Hosting a UI Fragment
      1. Defining a container view
      2. Adding a UI fragment to the FragmentManager
        1. Fragment transactions
        2. The FragmentManager and the fragment lifecycle
    7. Application Architecture with Fragments
      1. Deciding whether to use fragments
  15. 9. Displaying Lists with RecyclerView
    1. Adding a New Fragment and ViewModel
      1. ViewModel lifecycle with fragments
    2. Adding a RecyclerView
    3. Creating an Item View Layout
    4. Implementing a ViewHolder
    5. Implementing an Adapter to Populate the RecyclerView
      1. Setting the RecyclerView’s adapter
    6. Recycling Views
    7. Cleaning Up Binding List Items
    8. Responding to Presses
    9. For the More Curious: ListView and GridView
    10. Challenge: RecyclerView ViewTypes
  16. 10. Creating User Interfaces with Layouts and Widgets
    1. Introducing ConstraintLayout
    2. Introducing the Graphical Layout Editor
    3. Using ConstraintLayout
      1. Making room
      2. Adding widgets
      3. ConstraintLayout’s inner workings
      4. Editing properties
      5. Making list items dynamic
    4. More on Layout Attributes
      1. Styles, themes, and theme attributes
    5. For the More Curious: Margins vs Padding
    6. For the More Curious: New Developments in ConstraintLayout
    7. Challenge: Formatting the Date
  17. 11. Databases and the Room Library
    1. Room Architecture Component Library
    2. Creating a Database
      1. Defining entities
      2. Creating a database class
        1. Creating a type converter
    3. Defining a Data Access Object
    4. Accessing the Database Using the Repository Pattern
    5. Testing Queries
      1. Uploading test data
    6. Application Threads
      1. Background threads
    7. Using LiveData
      1. Observing LiveData
    8. Challenge: Addressing the Schema Warning
    9. For the More Curious: Singletons
  18. 12. Fragment Navigation
    1. Single Activity: Fragment Boss
      1. Fragment callback interfaces
      2. Replacing a fragment
    2. Fragment Arguments
      1. Attaching arguments to a fragment
      2. Retrieving arguments
    3. Using LiveData Transformations
    4. Updating the Database
      1. Using an executor
      2. Tying database writes to the fragment lifecycle
    5. For the More Curious: Why Use Fragment Arguments?
    6. For the More Curious: Navigation Architecture Component Library
    7. Challenge: Efficient RecyclerView Reloading
  19. 13. Dialogs
    1. Creating a DialogFragment
      1. Showing a DialogFragment
    2. Passing Data Between Two Fragments
      1. Passing data to DatePickerFragment
      2. Returning data to CrimeFragment
        1. Setting a target fragment
        2. Sending data to the target fragment
    3. Challenge: More Dialogs
  20. 14. The App Bar
    1. AppCompat Default App Bar
    2. Menus
      1. Defining a menu in XML
        1. The app namespace
      2. Creating the menu
      3. Responding to menu selections
    3. Using the Android Asset Studio
    4. For the More Curious: App Bar vs Action Bar vs Toolbar
    5. For the More Curious: Accessing the AppCompat App Bar
    6. Challenge: An Empty View for the RecyclerView
  21. 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
        1. Getting data from the contacts list
        2. Contacts permissions
      4. Checking for responding activities
    5. Challenge: Another Implicit Intent
  22. 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
  23. 17. Localization
    1. Localizing Resources
      1. Default resources
        1. Screen density works differently
      2. Checking string coverage using the Translations Editor
      3. Targeting a region
    2. Configuration Qualifiers
      1. Prioritizing alternative resources
      2. Multiple qualifiers
      3. Finding the best-matching resources
        1. Ruling out incompatible directories
        2. Stepping through the precedence table
    3. Testing Alternative Resources
    4. For the More Curious: More on Determining Device Size
    5. Challenge: Localizing Dates
  24. 18. 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
    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
  25. 19. Data Binding and MVVM
    1. Different Architectures: Why Bother?
    2. MVVM View Models vs Jetpack ViewModels
    3. Creating BeatBox
    4. Implementing Simple Data Binding
    5. Importing Assets
    6. Accessing Assets
    7. Wiring Up Assets for Use
    8. Binding to Data
      1. Creating a view model
      2. Binding to a view model
      3. Observable data
    9. For the More Curious: More About Data Binding
      1. Lambda expressions
      2. Syntactic sugar
      3. BindingAdapters
    10. For the More Curious: LiveData and Data Binding
  26. 20. Unit Testing and Audio Playback
    1. Creating a SoundPool
    2. Accessing Assets
    3. Loading Sounds
    4. Playing Sounds
    5. Test Dependencies
    6. Creating a Test Class
    7. Setting Up Your Test
      1. Setting up the test subject
    8. Writing Tests
      1. Testing object interactions
    9. Data Binding Callbacks
    10. Unloading Sounds
    11. For the More Curious: Integration Testing
    12. For the More Curious: Mocks and Testing
    13. Challenge: Playback Speed Control
    14. Challenge: Play Sound Across Rotation
  27. 21. 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
  28. 22. 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
  29. 23. 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. For the More Curious: Processes vs Tasks
    7. For the More Curious: Concurrent Documents
    8. Challenge: Icons
  30. 24. HTTP and Background Tasks
    1. Creating PhotoGallery
    2. Networking Basics with Retrofit
      1. Defining an API interface
      2. Building the Retrofit object and creating an API instance
        1. Adding a String converter
      3. Executing a web request
      4. Asking permission to network
      5. Moving toward the repository pattern
    3. Fetching JSON from Flickr
      1. Deserializing JSON text into model objects
    4. Networking Across Configuration Changes
    5. Displaying Results in RecyclerView
    6. For the More Curious: Alternate Parsers and Data Formats
    7. For the More Curious: Canceling Requests
    8. For the More Curious: Managing Dependencies
    9. Challenge: Adding a Custom Gson Deserializer
    10. Challenge: Paging
    11. Challenge: Dynamically Adjusting the Number of Columns
  31. 25. Loopers, Handlers, and HandlerThread
    1. Preparing RecyclerView to Display Images
    2. Preparing to Download Bytes from a URL
    3. Downloading Lots of Small Things
    4. Assembling a Background Thread
      1. Making your thread lifecycle aware
      2. Starting and stopping a HandlerThread
    5. Messages and Message Handlers
      1. Message anatomy
      2. Handler anatomy
      3. Using handlers
      4. Passing handlers
    6. Listening to the View Lifecycle
    7. Retained Fragments
      1. Rotation and retained fragments
      2. Whether to retain
    8. For the More Curious: Solving the Image Downloading Problem
    9. For the More Curious: StrictMode
    10. Challenge: Observing View LifecycleOwner LiveData
    11. Challenge: Improving ThumbnailDownloader’s Lifecycle Awareness
    12. Challenge: Preloading and Caching
  32. 26. SearchView and SharedPreferences
    1. Searching Flickr
    2. Using SearchView
      1. Responding to SearchView user interactions
    3. Simple Persistence with SharedPreferences
    4. Polishing Your App
    5. Editing SharedPreferences with Android KTX
    6. Challenge: Polishing Your App Some More
  33. 27. WorkManager
    1. Creating a Worker
    2. Scheduling Work
    3. Checking for New Photos
    4. Notifying the User
    5. Providing User Control over Polling
  34. 28. Broadcast Intents
    1. Regular Intents vs Broadcast Intents
    2. Filtering Foreground Notifications
      1. Sending broadcast intents
      2. Creating and registering a standalone receiver
      3. Limiting broadcasts to your app using private permissions
        1. More about protection levels
      4. Creating and registering a dynamic receiver
      5. Passing and receiving data with ordered broadcasts
    3. Receivers and Long-Running Tasks
    4. For the More Curious: Local Events
      1. Using EventBus
      2. Using RxJava
    5. For the More Curious: Limitations on Broadcast Receivers
    6. For the More Curious: Detecting the Visibility of Your Fragment
  35. 29. 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. WebView vs a Custom UI
    6. For the More Curious: Injecting JavaScript Objects
    7. For the More Curious: WebView Updates
    8. For the More Curious: Chrome Custom Tabs (Another Easy Way)
    9. Challenge: Using the Back Button for Browser History
  36. 30. 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. For the More Curious: GestureDetector
    6. Challenge: Saving State
    7. Challenge: Rotating Boxes
    8. Challenge: Accessibility Support
  37. 31. 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
  38. 32. Afterword
    1. The Final Challenge
    2. Shameless Plugs
    3. Thank You
  39. Index