0%

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 course, 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.1 (Jelly Bean) and up, including coverage of Lollipop and material design.

Write and run code every step of the way, creating apps that integrate with other Android 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.

Big Nerd Ranch specializes in developing and designing innovative applications for clients around the world. Our experts teach others through our books, bootcamps, and onsite training. Whether it's Android, iOS, Ruby and Ruby on Rails, Cocoa, Mac OS X, JavaScript, HTML5 or UX/UI, we've got you covered.

The Android team is constantly improving and updating Android Studio and other tools. As a result, some of the instructions we provide in the book are no longer correct. You can find an addendum addressing breaking changes at:

https://github.com/bignerdranch/AndroidCourseResources/raw/master/2ndEdition/Errata/2eAddendum.pdf.

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 Second Edition?
    3. How to Use This Book
    4. How This Book is Organized
    5. Challenges
    6. Are you more curious?
    7. Code Style
    8. Typographical Conventions
    9. Using an eBook
    10. Android Versions
  6. The Necessary Tools
    1. Downloading and Installing Android Studio
    2. Downloading Earlier SDK Versions
    3. An Alternative Emulator
    4. 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 User Interface
    5. The view hierarchy
    6. Widget attributes
    7. android:layout_width and android:layout_height
    8. android:orientation
    9. android:text
    10. Creating string resources
    11. Previewing the layout
    12. From Layout XML to View Objects
    13. Resources and resource IDs
    14. Wiring Up Widgets
    15. Getting references to widgets
    16. Setting listeners
    17. Using anonymous inner classes
    18. Making Toasts
    19. Using code completion
    20. Running on the Emulator
    21. For the More Curious: Android Build Process
    22. Android build tools
  8. 2. Android and Model-View-Controller
    1. Creating a New Class
    2. Generating getters and setters
    3. Model-View-Controller and Android
    4. Benefits of MVC
    5. Updating the View Layer
    6. Updating the Controller Layer
    7. Running on a Device
    8. Connecting your device
    9. Configuring your device for development
    10. Adding an Icon
    11. Adding resources to a project
    12. Referencing resources in XML
    13. Challenges
    14. Challenge: Add a Listener to the TextView
    15. Challenge: Add a Previous Button
    16. Challenge: From Button to ImageButton
  9. 3. The Activity Lifecycle
    1. Logging the Activity Lifecycle
    2. Making log messages
    3. Using LogCat
    4. Rotation and the Activity Lifecycle
    5. Device configurations and alternative resources
    6. Creating a landscape layout
    7. Saving Data Across Rotation
    8. Overriding onSaveInstanceState(Bundle)
    9. The Activity Lifecycle, Revisited
    10. For the More Curious: Testing onSaveInstanceState(Bundle)
    11. For the More Curious: Logging Levels and Methods
  10. 4. Debugging Android Apps
    1. Exceptions and Stack Traces
    2. Diagnosing misbehaviors
    3. Logging stack traces
    4. Setting breakpoints
    5. Using exception breakpoints
    6. Android-Specific Debugging
    7. Using Android Lint
    8. Issues with the R class
  11. 5. Your Second Activity
    1. Setting Up a Second Activity
    2. Creating a new activity
    3. A new activity subclass
    4. Declaring activities in the manifest
    5. Adding a Cheat! button to QuizActivity
    6. Starting an Activity
    7. Communicating with intents
    8. Explicit and implicit intents
    9. Passing Data Between Activities
    10. Using intent extras
    11. Getting a result back from a child activity
    12. Setting a result
    13. Sending back an intent
    14. Handling a result
    15. How Android Sees Your Activities
    16. Challenge
  12. 6. Android SDK Versions and Compatibility
    1. Android SDK Versions
    2. Compatibility and Android Programming
    3. A sane minimum
    4. Minimum SDK version
    5. Target SDK version
    6. Compile SDK version
    7. Adding code from later APIs safely
    8. Using the Android Developer Documentation
    9. Challenge: Reporting the Build Version
  13. 7. UI Fragments and the Fragment Manager
    1. The Need for UI Flexibility
    2. Introducing Fragments
    3. Starting CriminalIntent
    4. Creating a new project
    5. Fragments and the support library
    6. Adding dependencies in Android Studio
    7. Creating the Crime class
    8. Hosting a UI Fragment
    9. The fragment lifecycle
    10. Two approaches to hosting
    11. Defining a container view
    12. Creating a UI Fragment
    13. Defining CrimeFragment’s layout
    14. Creating the CrimeFragment class
    15. Implementing fragment lifecycle methods
    16. Wiring widgets in a fragment
    17. Adding a UI Fragment to the FragmentManager
    18. Fragment transactions
    19. The FragmentManager and the fragment lifecycle
    20. Application Architecture with Fragments
    21. The reason all our activities will use fragments
    22. For the More Curious: Why Support Fragments are Superior
    23. For the More Curious: Using Built-In Fragments
  14. 8. Creating User Interfaces with Layouts and Widgets
    1. Upgrading Crime
    2. Updating the Layout
    3. Wiring Widgets
    4. More on XML Layout Attributes
    5. Styles, themes, and theme attributes
    6. Screen pixel densities and dp and sp
    7. Android’s design guidelines
    8. Layout parameters
    9. Margins vs. padding
    10. Using the Graphical Layout Tool
    11. Creating a landscape layout
    12. Adding a new widget
    13. Editing attributes in properties view
    14. Reorganizing widgets in the component tree
    15. Updating child layout parameters
    16. How android:layout_weight works
    17. The graphical layout tool and you
    18. Widget IDs and multiple layouts
    19. Challenge: Formatting the Date
  15. 9. Displaying Lists with RecyclerView
    1. Updating CriminalIntent’s Model Layer
    2. Singletons and centralized data storage
    3. An Abstract Activity for Hosting a Fragment
    4. A generic fragment-hosting layout
    5. An abstract Activity class
    6. Using an abstract class
    7. Creating the new controllers
    8. Declaring CrimeListActivity
    9. RecyclerView, Adapter, and ViewHolder
    10. ViewHolders and Adapters
    11. Adapters
    12. Using a RecyclerView
    13. Implementing an Adapter and ViewHolder
    14. Customizing List Items
    15. Creating the list item layout
    16. Using a custom item view
    17. Responding to Presses
    18. For the More Curious: ListView and GridView
    19. For the More Curious: Singletons
  16. 10. Using Fragment Arguments
    1. Starting an Activity from a Fragment
    2. Putting an extra
    3. Retrieving an extra
    4. Updating CrimeFragment’s view with Crime data
    5. The downside to direct retrieval
    6. Fragment Arguments
    7. Attaching arguments to a fragment
    8. Retrieving arguments
    9. Reloading the List
    10. Getting Results with Fragments
    11. Challenge: Efficient RecyclerView Reloading
    12. For the More Curious: Why Use Fragment Arguments?
  17. 11. Using ViewPager
    1. Creating CrimePagerActivity
    2. ViewPager and PagerAdapter
    3. Integrating CrimePagerActivity
    4. FragmentStatePagerAdapter vs. FragmentPagerAdapter
    5. For the More Curious: How ViewPager Really Works
    6. For the More Curious: Laying Out Views in Code
  18. 12. Dialogs
    1. The AppCompat Library
    2. Creating a DialogFragment
    3. Showing a DialogFragment
    4. Setting a dialog’s contents
    5. Passing Data Between Two Fragments
    6. Passing data to DatePickerFragment
    7. Returning data to CrimeFragment
    8. Setting a target fragment
    9. Sending data to the target fragment
    10. More flexibility in presenting a DialogFragment
    11. Challenge: More Dialogs
    12. Challenge: A Responsive DialogFragment
  19. 13. The Toolbar
    1. AppCompat
    2. Using the AppCompat library
    3. Updating the theme
    4. Using AppCompatActivity
    5. Menus
    6. Defining a menu in XML
    7. The app namespace
    8. Using Android Asset Studio
    9. Creating the menu
    10. Responding to menu selections
    11. Enabling Hierarchical Navigation
    12. How hierarchical navigation works
    13. An Alternative Action Item
    14. Toggling the action item title
    15. “Just one more thing...”
    16. For the More Curious: Toolbar vs Action Bar
    17. Challenge: Deleting Crimes
    18. Challenge: Plural String Resources
    19. Challenge: An Empty View for the RecyclerView
  20. 14. SQLite Databases
    1. Defining a Schema
    2. Building Your Initial Database
    3. Debugging database issues
    4. Gutting CrimeLab
    5. Writing to the Database
    6. Using ContentValues
    7. Inserting and updating rows
    8. Reading from the Database
    9. Using a CursorWrapper
    10. Converting to model objects
    11. Refreshing model data
    12. For the More Curious: More Databases
    13. For the More Curious: The Application Context
    14. Challenge: Deleting Crimes
  21. 15. Implicit Intents
    1. Adding Buttons
    2. Adding a Suspect to the Model Layer
    3. Using a Format String
    4. Using Implicit Intents
    5. Parts of an implicit intent
    6. Sending a crime report
    7. Asking Android for a contact
    8. Getting the data from the contact list
    9. Contacts permissions
    10. Checking for responding activities
    11. Challenge: ShareCompat
    12. Challenge: Another Implicit Intent
  22. 16. Taking Pictures with Intents
    1. A Place for Your Photo
    2. Including layout files
    3. External Storage
    4. Designating a picture location
    5. Using a Camera Intent
    6. External storage permission
    7. Firing the intent
    8. Scaling and Displaying Bitmaps
    9. Declaring Features
    10. For the More Curious: Using Includes
    11. Challenge: Detail Display
    12. Challenge: Efficient Thumbnail Load
  23. 17. Two-Pane Master-Detail Interfaces
    1. Adding Layout Flexibility
    2. Modifying SingleFragmentActivity
    3. Creating a layout with two fragment containers
    4. Using an alias resource
    5. Creating tablet alternatives
    6. Activity: Fragment Boss
    7. Fragment callback interfaces
    8. Implementing CrimeListFragment.Callbacks
    9. Implementing CrimeFragment.Callbacks
    10. For the More Curious: More on Determining Device Size
  24. 18. Assets
    1. Why Assets, Not Resources
    2. Creating BeatBox
    3. Importing Assets
    4. Getting at Assets
    5. Wiring Up Assets for Use
    6. Accessing Assets
    7. For the More Curious: Non-Assets?
  25. 19. Audio Playback with SoundPool
    1. Creating a SoundPool
    2. Loading Sounds
    3. Playing Sounds
    4. Unloading Sounds
    5. Rotation and Object Continuity
    6. Retaining a fragment
    7. Rotation and retained fragments
    8. For the More Curious: Whether to Retain
    9. For the More Curious: More on Rotation Handling
  26. 20. Styles and Themes
    1. Color Resources
    2. Styles
    3. Style inheritance
    4. Themes
    5. Modifying the theme
    6. Adding Theme Colors
    7. Overriding Theme Attributes
    8. Theme spelunking
    9. Modifying Button Attributes
    10. For the More Curious: More on Style Inheritance
    11. For the More Curious: Accessing Theme Attributes
    12. Challenge: An Appropriate Base Theme
  27. 21. 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: 9-Patch Images
    7. For the More Curious: Mipmap Images
  28. 22. 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
    5. Switching between tasks
    6. Starting a new task
    7. Using NerdLauncher as a Home Screen
    8. Challenge: Icons
    9. For the More Curious: Processes vs. Tasks
    10. For the More Curious: Concurrent Documents
  29. 23. HTTP & Background Tasks
    1. Creating PhotoGallery
    2. Networking Basics
    3. Asking permission to network
    4. Using AsyncTask to Run on a Background Thread
    5. You and Your Main Thread
    6. Beyond the main thread
    7. Fetching JSON from Flickr
    8. Parsing JSON text
    9. From AsyncTask Back to the Main Thread
    10. Cleaning Up AsyncTasks
    11. For the More Curious: More on AsyncTask
    12. For the More Curious: Alternatives to AsyncTask
    13. Challenge: Gson
    14. Challenge: Paging
    15. Challenge: Dynamically Adjusting the Number of Columns
  30. 24. 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
    6. Message anatomy
    7. Handler anatomy
    8. Using handlers
    9. Passing handlers
    10. For the More Curious: AsyncTask vs. Threads
    11. Challenge: Preloading and Caching
    12. For the More Curious: Solving the Image Downloading Problem
  31. 25. Search
    1. Searching Flickr
    2. Using SearchView
    3. Responding to SearchView user interactions
    4. Simple Persistence with Shared Preferences
    5. Polishing Your App
    6. Challenge: Polishing Your App Some More
  32. 26. Background Services
    1. Creating an IntentService
    2. What Services are For
    3. Safe background networking
    4. Looking for New Results
    5. Delayed Execution with AlarmManager
    6. Being a good citizen: using alarms the right way
    7. Inexact vs. exact repeating
    8. Time basis options
    9. PendingIntent
    10. Managing alarms with PendingIntent
    11. Controlling Your Alarm
    12. Notifications
    13. Challenge: Notifications on Android Wear
    14. For the More Curious: Service Details
    15. What a service does (and does not) do
    16. A service’s lifecycle
    17. Non-sticky services
    18. Sticky services
    19. Bound services
    20. Local service binding
    21. Remote service binding
    22. For the More Curious: JobScheduler and JobServices
    23. For the More Curious: Sync Adapters
    24. Challenge: Using JobService on Lollipop
  33. 27. Broadcast Intents
    1. Regular Intents vs. Broadcast Intents
    2. Receiving a System Broadcast: Waking Up on Boot
    3. Creating and registering a standalone receiver
    4. Using receivers
    5. Filtering Foreground Notifications
    6. Sending broadcast intents
    7. Creating and registering a dynamic receiver
    8. Limiting broadcasts to your app using private permissions
    9. More about protection levels
    10. Passing and receiving data with ordered broadcasts
    11. Receivers and Long-Running Tasks
    12. For the More Curious: Local Events
    13. Using EventBus
    14. Using RxJava
    15. For the More Curious: Detecting the Visibility of Your Fragment
  34. 28. Browsing the Web and WebView
    1. One Last Bit of Flickr Data
    2. The Easy Way: Implicit Intents
    3. The Harder Way: WebView
    4. Using WebChromeClient to spruce things up
    5. Proper Rotation with WebView
    6. Dangers of handling configuration changes
    7. For the More Curious: Injecting JavaScript Objects
    8. For the More Curious: KitKat’s WebView Overhaul
    9. Challenge: Using the Back Button for Browser History
    10. Challenge: Supporting Non-HTTP Links
  35. 29. Custom Views and Touch Events
    1. Setting Up the DragAndDraw Project
    2. Setting up DragAndDrawActivity
    3. Setting up DragAndDrawFragment
    4. Creating a Custom View
    5. Creating BoxDrawingView
    6. Handling Touch Events
    7. Tracking across motion events
    8. Rendering Inside onDraw(…)
    9. Challenge: Saving State
    10. Challenge: Rotating Boxes
  36. 30. Property Animation
    1. Building the Scene
    2. Simple Property Animation
    3. View transformation properties
    4. Using different interpolators
    5. Color evaluation
    6. Playing Animators Together
    7. For the More Curious: Other Animation APIs
    8. Legacy animation tools
    9. Transitions
    10. Challenges
  37. 31. Locations and Play Services
    1. Locations and Libraries
    2. Google Play Services
    3. Creating Locatr
    4. Play Services and Location Testing on Emulators
    5. Mock location data
    6. Building out Locatr
    7. Setting Up Google Play Services
    8. Location permissions
    9. Using Google Play Services
    10. Flickr Geosearch
    11. Getting a Location Fix
    12. Find and Display an Image
    13. Challenge: Progress
  38. 32. Maps
    1. Importing Play Services Maps
    2. Mapping on Android
    3. Maps API Setup
    4. Getting a Maps API Key
    5. Your signing key
    6. Getting an API key
    7. Setting Up Your Map
    8. Getting More Location Data
    9. Working with Your Map
    10. Drawing on the map
    11. For the More Curious: Teams and API Keys
  39. 33. Material Design
    1. Material Surfaces
    2. Elevation and Z values
    3. State list animators
    4. Animation Tools
    5. Circular reveal
    6. Shared element transitions
    7. View Components
    8. Cards
    9. Floating action buttons
    10. Snackbars
    11. More on Material Design
  40. 34. Afterword
    1. The Final Challenge
    2. Shameless Plugs
    3. Thank You
  41. Index
3.133.144.197