Table of Contents

Learning Android

Prerequisites

What’s New in the Fourth Edition?

Kotlin vs Java

How to Use This Book

How This Book Is Organized

Challenges

Are you more curious?

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

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

Making Toasts

Running on the Emulator

For the More Curious: The Android Build Process

Android build tools

Challenges

Challenge: Customizing the Toast

2. Android and Model-View-Controller

Creating a New Class

Model-View-Controller and Android

Deciding to use MVC

Updating the View Layer

Updating the Controller Layer

Adding an Icon

Adding resources to a project

Referencing resources in XML

Screen Pixel Densities

Running on a Device

Challenge: Add a Listener to the TextView

Challenge: Add a Previous Button

Challenge: From Button to ImageButton

3. The Activity Lifecycle

Rotating GeoQuiz

Activity States and Lifecycle Callbacks

Logging the Activity Lifecycle

Making log messages

Using Logcat

Exploring How the Activity Lifecycle Responds to User Actions

Temporarily leaving an activity

Finishing an activity

Rotating an activity

Device Configuration Changes and the Activity Lifecycle

Creating a landscape layout

For the More Curious: UI Updates and Multi-Window Mode

For the More Curious: Log Levels

Challenge: Preventing Repeat Answers

Challenge: Graded Quiz

4. Persisting UI State

Including the ViewModel Dependency

Adding a ViewModel

ViewModel lifecycle and ViewModelProvider

Add data to your ViewModel

Saving Data Across Process Death

Overriding onSaveInstanceState(Bundle)

Saved instance state and activity records

ViewModel vs Saved Instance State

For the More Curious: Jetpack, AndroidX, and Architecture Components

For the More Curious: Avoiding a Half-Baked Solution

5. Debugging Android Apps

Exceptions and Stack Traces

Diagnosing misbehaviors

Logging stack traces

Setting breakpoints

Android-Specific Debugging

Using Android Lint

Issues with the R class

Challenge: Exploring the Layout Inspector

Challenge: Exploring the Profiler

6. 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 MainActivity

Starting an Activity

Communicating with intents

Passing Data Between Activities

Using intent extras

Getting a result back from a child activity

How Android Sees Your Activities

Challenge: Closing Loopholes for Cheaters

Challenge: Tracking Cheat Status by Question

7. 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

Jetpack libraries

Using the Android Developer Documentation

Challenge: Reporting the Device’s Android Version

Challenge: Limited Cheats

8. UI Fragments and the Fragment Manager

The Need for UI Flexibility

Introducing Fragments

Starting CriminalIntent

Creating a new project

Creating a Data Class

Creating a UI Fragment

Defining CrimeFragment’s layout

Creating the CrimeFragment class

Hosting a UI Fragment

Defining a container view

Adding a UI fragment to the FragmentManager

Application Architecture with Fragments

Deciding whether to use fragments

9. Displaying Lists with RecyclerView

Adding a New Fragment and ViewModel

ViewModel lifecycle with fragments

Adding a RecyclerView

Creating an Item View Layout

Implementing a ViewHolder

Implementing an Adapter to Populate the RecyclerView

Setting the RecyclerView’s adapter

Recycling Views

Cleaning Up Binding List Items

Responding to Presses

For the More Curious: ListView and GridView

Challenge: RecyclerView ViewTypes

10. Creating User Interfaces with Layouts and Widgets

Introducing ConstraintLayout

Introducing the Graphical Layout Editor

Using ConstraintLayout

Making room

Adding widgets

ConstraintLayout’s inner workings

Editing properties

Making list items dynamic

More on Layout Attributes

Styles, themes, and theme attributes

For the More Curious: Margins vs Padding

For the More Curious: New Developments in ConstraintLayout

Challenge: Formatting the Date

11. Databases and the Room Library

Room Architecture Component Library

Creating a Database

Defining entities

Creating a database class

Defining a Data Access Object

Accessing the Database Using the Repository Pattern

Testing Queries

Uploading test data

Application Threads

Background threads

Using LiveData

Observing LiveData

Challenge: Addressing the Schema Warning

For the More Curious: Singletons

12. Fragment Navigation

Single Activity: Fragment Boss

Fragment callback interfaces

Replacing a fragment

Fragment Arguments

Attaching arguments to a fragment

Retrieving arguments

Using LiveData Transformations

Updating the Database

Using an executor

Tying database writes to the fragment lifecycle

For the More Curious: Why Use Fragment Arguments?

For the More Curious: Navigation Architecture Component Library

Challenge: Efficient RecyclerView Reloading

13. Dialogs

Creating a DialogFragment

Showing a DialogFragment

Passing Data Between Two Fragments

Passing data to DatePickerFragment

Returning data to CrimeFragment

Challenge: More Dialogs

14. The App Bar

AppCompat Default App Bar

Menus

Defining a menu in XML

Creating the menu

Responding to menu selections

Using the Android Asset Studio

For the More Curious: App Bar vs Action Bar vs Toolbar

For the More Curious: Accessing the AppCompat App Bar

Challenge: An Empty View for the RecyclerView

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

Checking for responding activities

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. Localization

Localizing Resources

Default resources

Checking string coverage using the Translations Editor

Targeting a region

Configuration Qualifiers

Prioritizing alternative resources

Multiple qualifiers

Finding the best-matching resources

Testing Alternative Resources

For the More Curious: More on Determining Device Size

Challenge: Localizing Dates

18. 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

For the More Curious: Using Accessibility Scanner

Challenge: Improving the List

Challenge: Providing Enough Context for Data Entry

Challenge: Announcing Events

19. Data Binding and MVVM

Different Architectures: Why Bother?

MVVM View Models vs Jetpack ViewModels

Creating BeatBox

Implementing Simple Data Binding

Importing Assets

Accessing Assets

Wiring Up Assets for Use

Binding to Data

Creating a view model

Binding to a view model

Observable data

For the More Curious: More About Data Binding

Lambda expressions

Syntactic sugar

BindingAdapters

For the More Curious: LiveData and Data Binding

20. Unit Testing and Audio Playback

Creating a SoundPool

Accessing Assets

Loading Sounds

Playing Sounds

Test Dependencies

Creating a Test Class

Setting Up Your Test

Setting up the test subject

Writing Tests

Testing object interactions

Data Binding Callbacks

Unloading Sounds

For the More Curious: Integration Testing

For the More Curious: Mocks and Testing

Challenge: Playback Speed Control

Challenge: Play Sound Across Rotation

21. 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

22. 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

23. 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

For the More Curious: Processes vs Tasks

For the More Curious: Concurrent Documents

Challenge: Icons

24. HTTP and Background Tasks

Creating PhotoGallery

Networking Basics with Retrofit

Defining an API interface

Building the Retrofit object and creating an API instance

Executing a web request

Asking permission to network

Moving toward the repository pattern

Fetching JSON from Flickr

Deserializing JSON text into model objects

Networking Across Configuration Changes

Displaying Results in RecyclerView

For the More Curious: Alternate Parsers and Data Formats

For the More Curious: Canceling Requests

For the More Curious: Managing Dependencies

Challenge: Adding a Custom Gson Deserializer

Challenge: Paging

Challenge: Dynamically Adjusting the Number of Columns

25. Loopers, Handlers, and HandlerThread

Preparing RecyclerView to Display Images

Preparing to Download Bytes from a URL

Downloading Lots of Small Things

Assembling a Background Thread

Making your thread lifecycle aware

Starting and stopping a HandlerThread

Messages and Message Handlers

Message anatomy

Handler anatomy

Using handlers

Passing handlers

Listening to the View Lifecycle

Retained Fragments

Rotation and retained fragments

Whether to retain

For the More Curious: Solving the Image Downloading Problem

For the More Curious: StrictMode

Challenge: Observing View LifecycleOwner LiveData

Challenge: Improving ThumbnailDownloader’s Lifecycle Awareness

Challenge: Preloading and Caching

26. SearchView and SharedPreferences

Searching Flickr

Using SearchView

Responding to SearchView user interactions

Simple Persistence with SharedPreferences

Polishing Your App

Editing SharedPreferences with Android KTX

Challenge: Polishing Your App Some More

27. WorkManager

Creating a Worker

Scheduling Work

Checking for New Photos

Notifying the User

Providing User Control over Polling

28. Broadcast Intents

Regular Intents vs Broadcast Intents

Filtering Foreground Notifications

Sending broadcast intents

Creating and registering a standalone receiver

Limiting broadcasts to your app using private permissions

Creating and registering a dynamic receiver

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: Limitations on Broadcast Receivers

For the More Curious: Detecting the Visibility of Your Fragment

29. 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

WebView vs a Custom UI

For the More Curious: Injecting JavaScript Objects

For the More Curious: WebView Updates

For the More Curious: Chrome Custom Tabs (Another Easy Way)

Challenge: Using the Back Button for Browser History

30. 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)

For the More Curious: GestureDetector

Challenge: Saving State

Challenge: Rotating Boxes

Challenge: Accessibility Support

31. 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

32. Afterword

The Final Challenge

Shameless Plugs

Thank You

Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.23.123