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