Table of Contents
; Authors of Head First Android Development; How to Use This Book: Intro; Who is this book for?; We know what you’re thinking; We know what your brain is thinking; Metacognition: thinking about thinking; Here’s what WE did:; Here’s what YOU can do to bend your brain into submission; Read me; The technical review team; Acknowledgments; Safari® Books Online; Chapter 1: Getting Started: Diving In; 1.1 Welcome to Androidville; 1.2 The Android platform dissected; 1.3 Here’s what we’re going to do; 1.4 Your development environment; 1.5 Install Java; 1.6 Build a basic app; 1.7 Let’s build the basic app; 1.8 Activities and layouts from 50,000 feet; 1.9 Building a basic app (continued); 1.10 Building a basic app (continued); 1.11 You’ve just created your first Android app; 1.12 Android Studio creates a complete folder structure for you; 1.13 Useful files in your project; 1.14 Edit code with the Android Studio editors; 1.15 Run the app in the Android emulator; 1.16 Creating an Android Virtual Device; 1.17 Run the app in the emulator; 1.18 You can watch progress in the console; 1.19 Test drive; 1.20 What just happened?; 1.21 Refining the app; 1.22 What’s in the layout?; 1.23 activity_main.xml has two elements; 1.24 The layout file contains a reference to a string, not the string itself; 1.25 Let’s look in the strings.xml file; 1.26 Take the app for a test drive; 1.27 Your Android Toolbox; Chapter 2: Building Interactive Apps: Apps That Do Something; 2.1 You’re going to build a Beer Adviser app; 2.2 Here’s what you need to do; 2.3 Create the project; 2.4 We’ve created a default activity and layout; 2.5 Adding components with the design editor; 2.6 activity_find_beer.xml has a new button; 2.7 A closer look at the layout code; 2.8 Changes to the XML...; 2.9 ...are reflected in the design editor; 2.10 Use string resources rather than hardcoding the text; 2.11 Change the layout to use the string resources; 2.12 Let’s take the app for a test drive; 2.13 Add values to the spinner; 2.14 Get the spinner to reference a string-array; 2.15 Test drive the spinner; 2.16 We need to make the button do something; 2.17 Make the button call a method; 2.18 What activity code looks like; 2.19 Add an onClickFindBeer() method to the activity; 2.20 onClickFindBeer() needs to do something; 2.21 Once you have a View, you can access its methods; 2.22 Update the activity code; 2.23 The first version of the activity; 2.24 What the code does; 2.25 Test drive the changes; 2.26 Building the custom Java class; 2.27 Enhance the activity to call the custom Java class so that we can get REAL advice; 2.28 Activity code version 2; 2.29 What happens when you run the code; 2.30 Test drive your app; 2.31 Your Android Toolbox; Chapter 3: Multiple Activities and Intents: State Your Intent; 3.1 Apps can contain more than one activity; 3.2 Here’s the app structure; 3.3 Create the project; 3.4 Update the layout; 3.5 Update strings.xml...; 3.6 Create the second activity and layout; 3.7 What just happened?; 3.8 Welcome to the Android manifest file; 3.9 Every activity needs to be declared; 3.10 An intent is a type of message; 3.11 Use an intent to start the second activity; 3.12 What happens when you run the app; 3.13 The story continues...; 3.14 Test drive the app; 3.15 Pass text to a second activity; 3.16 Update the text view properties; 3.17 putExtra() puts extra information in an intent; 3.18 Update the CreateMessageActivity code; 3.19 Get ReceiveMessageActivity to use the information in the intent; 3.20 What happens when the user clicks the Send Message button; 3.21 Test drive the app; 3.22 We can change the app to send messages to other people; 3.23 How Android apps work; 3.24 But we don’t know what apps are on the device; 3.25 Create an intent that specifies an action; 3.26 Change the intent to use an action; 3.27 What happens when the code runs; 3.28 The story continues...; 3.29 The intent filter tells Android which activities can handle which actions; 3.30 How Android uses the intent filter; 3.31 You need to run your app on a REAL device; 3.32 Test drive the app; 3.33 What if you ALWAYS want your users to choose an activity?; 3.34 What happens when you call createChooser(); 3.35 The story continues...; 3.36 Change the code to create a chooser; 3.37 Test drive the app; 3.38 If you have NO matching activities; 3.39 Your Android Toolbox; Chapter 4: The Activity Lifecycle: Being an Activity; 4.1 How do activities really work?; 4.2 The Stopwatch app; 4.3 The stopwatch layout code; 4.4 How the activity code will work; 4.5 Add code for the buttons; 4.6 The runTimer() method; 4.7 Handlers allow you to schedule code; 4.8 The full runTimer() code; 4.9 The full StopwatchActivity code; 4.10 What happens when you run the app; 4.11 The story continues; 4.12 Test drive the app; 4.13 What just happened?; 4.14 Rotating the screen changes the device configuration; 4.15 From birth to death: the states of an activity; 4.16 The activity lifecycle: from create to destroy; 4.17 Your activity inherits the lifecycle methods; 4.18 How do we deal with configuration changes?; 4.19 Or save the current state...; 4.20 ...then restore the state in onCreate(); 4.21 What happens when you run the app; 4.22 The story continues; 4.23 Test drive the app; 4.24 There’s more to an activity’s life than create and destroy; 4.25 The activity lifecycle: the visible lifetime; 4.26 We need to implement two more lifecycle methods; 4.27 The updated StopwatchActivity code; 4.28 What happens when you run the app; 4.29 Test drive the app; 4.30 But what if an app is only partially visible?; 4.31 The activity lifecycle: the foreground lifetime; 4.32 Stop the stopwatch if the activity’s paused; 4.33 What happens when you run the app; 4.34 Test drive the app; 4.35 The complete activity code; 4.36 Your handy guide to the lifecycle methods; 4.37 Your Android Toolbox; Chapter 5: The User Interface: Enjoy the View; 5.1 Your user interface is made up of layouts and GUI components; 5.2 Three key layouts: relative, linear, and grid; 5.3 RelativeLayout displays views in relative positions; 5.4 Adding padding; 5.5 Positioning views relative to the parent layout; 5.6 Attributes for positioning views relative to the parent layout; 5.7 Positioning views relative to other views; 5.8 Attributes for positioning views relative to other views; 5.9 Use margins to add distance between views; 5.10 RelativeLayout: a summary; 5.11 LinearLayout displays views in a single row or column; 5.12 A linear layout displays views in the order they appear in the layout XML; 5.13 Let’s change up a basic linear layout; 5.14 Here’s the starting point for the linear layout; 5.15 Make a view streeeeetch by adding weight; 5.16 Adding weight to one view; 5.17 Adding weight to multiple views; 5.18 Use gravity to specify where text appears in a view; 5.19 Test drive; 5.20 Using the android:gravity attribute: a list of values; 5.21 Move the button to the right with layout-gravity; 5.22 More values you can use with the android:layout-gravity attribute; 5.23 The full linear layout code; 5.24 LinearLayout: a summary; 5.25 GridLayout displays views in a grid; 5.26 Adding views to the grid layout; 5.27 Let’s create a new grid layout; 5.28 We’ll start with a sketch; 5.29 Row 0: add views to specific rows and columns; 5.30 Row 1: make a view span multiple columns; 5.31 Row 2: make a view span multiple columns; 5.32 The full code for the grid layout; 5.33 GridLayout: a summary; 5.34 Layouts and GUI components have a lot in common; 5.35 GUI components are a type of View; 5.36 What being a view buys you; 5.37 A layout is really a hierarchy of Views; 5.38 Playing with views; 5.39 Text view; 5.40 Edit Text; 5.41 Button; 5.42 Toggle button; 5.43 Switch; 5.44 Check boxes; 5.45 Radio buttons; 5.46 Spinner; 5.47 Image views; 5.48 Images: the layout XML; 5.49 Adding images to buttons; 5.50 Image Button; 5.51 Scroll views; 5.52 Toasts; 5.53 Your Android Toolbox; Chapter 6: List Views and Adapters: Getting Organized; 6.1 Every app starts with ideas; 6.2 Categorize your ideas: top-level, category, and detail/edit activities; 6.3 Navigating through the activities; 6.4 Use ListViews to navigate to data; 6.5 We’re going to build part of the Starbuzz app; 6.6 The drink detail activity; 6.7 The Starbuzz app structure; 6.8 Here are the steps; 6.9 The Drink class; 6.10 The image files; 6.11 The top-level layout contains an image and a list; 6.12 Use a list view to display the list of options; 6.13 The full top-level layout code; 6.14 Test drive; 6.15 Get ListViews to respond to clicks with a Listener; 6.16 Set the listener to the list view; 6.17 The full TopLevelActivity code; 6.18 Where we’ve got to; 6.19 A category activity displays the data for a single category; 6.20 A ListActivity is an activity that contains only a list; 6.21 How to create a list activity; 6.22 android:entries works for static array data held in strings.xml; 6.23 Connect list views to arrays with an array adapter; 6.24 Add the array adapter to DrinkCategoryActivity; 6.25 What happens when you run the code; 6.26 Test drive the app; 6.27 App review: where we’ve got to; 6.28 How we handled clicks in TopLevelActivity; 6.29 Pass data to an activity using the ListActivity onListItemClick() method; 6.30 The full DrinkCategoryActivity code; 6.31 A detail activity displays data for a single record; 6.32 Retrieve data from the intent; 6.33 Update the views with the data; 6.34 The DrinkActivity code; 6.35 What happens when you run the app; 6.36 The story continues; 6.37 Test drive the app; 6.38 Your Android Toolbox; Chapter 7: Fragments: Make it Modular; 7.1 Your app needs to look great on all devices; 7.2 Your app may need to behave differently too; 7.3 Fragments allow you to reuse code; 7.4 The Workout app structure; 7.5 Here are the steps; 7.6 The Workout class; 7.7 How to add a fragment to your project; 7.8 Fragment layout code looks just like activity layout code; 7.9 What fragment code looks like; 7.10 Adding a fragment to an activity’s layout; 7.11 Passing the workout ID to the fragment; 7.12 Get the activity to set the workout ID; 7.13 Activity states revisited; 7.14 The fragment lifecycle; 7.15 Your fragment inherits the lifecycle methods; 7.16 Set the view’s values in the fragment’s onStart() method; 7.17 Test drive the app; 7.18 Where we’ve got to; 7.19 We need to create a fragment with a list; 7.20 A ListFragment is a fragment that contains only a list; 7.21 How to create a list fragment; 7.22 We’ll use an ArrayAdapter to set the values in the ListView; 7.23 The updated WorkoutListFragment code; 7.24 Display WorkoutListFragment in the MainActivity layout; 7.25 Test drive the app; 7.26 Wiring up the list to the detail; 7.27 We need to decouple the fragment with an interface; 7.28 First, add the interface to the list fragment; 7.29 Then make the activity implement the interface; 7.30 You want fragments to work with the back button; 7.31 Don’t update—instead, replace; 7.32 Using fragment transactions; 7.33 The updated MainActivity code; 7.34 Test drive the app; 7.35 Rotating the device breaks the app; 7.36 The WorkoutDetailFragment code; 7.37 Phone versus tablet; 7.38 The phone and tablet app structures; 7.39 Put screen-specific resources in screen-specific folders; 7.40 The different folder options; 7.41 Tablets use layouts in the layout-large folder; 7.42 The MainActivity phone layout; 7.43 Phones will use DetailActivity to display details of the workout; 7.44 The full DetailActivity code; 7.45 Use layout differences to tell which layout the device is using; 7.46 The revised MainActivity code; 7.47 Test drive the app; 7.48 Your Android Toolbox; Chapter 8: Nested Fragments: Dealing with Children; 8.1 Creating nested fragments; 8.2 Fragments and activities have similar lifecycles...; 8.3 The StopwatchFragment code; 8.4 The StopwatchFragment layout; 8.5 Adding the stopwatch fragment to WorkoutDetailFragment; 8.6 Add a FrameLayout where the fragment should appear; 8.7 Then display the fragment in Java code; 8.8 getFragmentManager() creates transactions at the activity lavel; 8.9 Nested fragments need nested transactions; 8.10 Display the fragment in its parent’s onCreate() method; 8.11 The full WorkoutDetailFragment code; 8.12 Test drive the app; 8.13 Why does the app crash if you press a button?; 8.14 Let’s look at the StopwatchFragment layout code; 8.15 The onClick attribute calls methods in the activity, not the fragment; 8.16 First, remove the onClick attributes from the fragment’s layout; 8.17 Make the fragment implement OnClickListener; 8.18 Attach the OnClickListener to the buttons; 8.19 The StopwatchFragment code; 8.20 Test drive the app; 8.21 Rotating the device re-creates the activity; 8.22 onCreate() REPLACES the fragment; 8.23 The WorkoutDetailFragment code; 8.24 Test drive the app; 8.25 Your Android Toolbox; Chapter 9: Action Bars: Taking Shortcuts; 9.1 Great apps have a clear structure; 9.2 Different types of navigation; 9.3 Let’s start with the action bar; 9.4 The Android support libraries; 9.5 Your project may include support libraries; 9.6 We’ll get the app to use up to date themes; 9.7 Apply a theme in AndroidManifest.xml; 9.8 Define styles in style resource files; 9.9 Set the default theme in styles.xml; 9.10 What happens when you run the app; 9.11 Test drive the app; 9.12 Adding action items to the action bar; 9.13 The menu resource file; 9.14 The menu showAsAction attribute; 9.15 Add a new action item; 9.16 Inflate the menu in the activity with the onCreateOptionsMenu() method; 9.17 React to action item clicks with the onOptionsItemSelected() method; 9.18 Create OrderActivity; 9.19 Start OrderActivity with the Create Order action item; 9.20 The full MainActivity.java code; 9.21 Test drive the app; 9.22 Sharing content on the action bar; 9.23 Add a share action provider to menu_main.xml; 9.24 Specify the content with an intent; 9.25 The full MainActivity.java code; 9.26 Test drive the app; 9.27 Enabling Up navigation; 9.28 Setting an activity’s parent; 9.29 Adding the Up button; 9.30 Test drive the app; 9.31 Your Android Toolbox; Chapter 10: Navigation Drawers: Going Places; 10.1 The Pizza app revisited; 10.2 Navigation drawers deconstructed; 10.3 The Pizza app structure; 10.4 Create TopFragment; 10.5 Create PizzaFragment; 10.6 Create PastaFragment; 10.7 Create StoresFragment; 10.8 Add the DrawerLayout; 10.9 The full code for activity_main.xml; 10.10 Initialize the drawer’s list; 10.11 Use an OnItemClickListener to respond to clicks in the list view; 10.12 The selectItem() method so far; 10.13 Changing the action bar title; 10.14 Closing the navigation drawer; 10.15 The updated MainActivity.java code; 10.16 Get the drawer to open and close; 10.17 Using an ActionBarDrawerToggle; 10.18 Modifying action bar items at runtime; 10.19 The updated MainActivity.java code; 10.20 Enable the drawer to open and close; 10.21 Syncing the ActionBarDrawerToggle state; 10.22 The updated MainActivity.java code; 10.23 Test drive the app; 10.24 The title and fragment are getting out of sync; 10.25 Dealing with configuration changes; 10.26 Reacting to changes on the back stack; 10.27 Adding tags to fragments; 10.28 Find the fragment using its tag; 10.29 The full MainActivity.java code; 10.30 Test drive the app; 10.31 Your Android Toolbox; Chapter 11: SQLite Databases: Fire Up the Database; 11.1 Back to Starbuzz; 11.2 Android uses SQLite databases to persist data; 11.3 Android comes with SQLite classes; 11.4 The current Starbuzz app structure; 11.5 We’ll change the app to use a database; 11.6 The SQLite helper manages your database; 11.7 Create the SQLite helper; 11.8 1. Specify the database; 11.9 Inside a SQLite database; 11.10 You create tables using Structured Query Language (SQL); 11.11 Insert data using the insert() method; 11.12 Update records with the update() method; 11.13 Multiple conditions; 11.14 The StarbuzzDatabaseHelper code; 11.15 What the SQLite helper code does; 11.16 What if you need to change the database?; 11.17 SQLite databases have a version number; 11.18 Upgrading the database: an overview; 11.19 The story continues....; 11.20 How the SQLite helper makes decisions; 11.21 Upgrade your database with onUpgrade(); 11.22 Downgrade your database with onDowngrade(); 11.23 Let’s upgrade the database; 11.24 Upgrading an existing database; 11.25 Renaming tables; 11.26 The full SQLite helper code; 11.27 The SQLite helper code (continued); 11.28 What happens when the code runs; 11.29 Your Android Toolbox; Chapter 12: Cursors and Asynctasks: Connecting to Databases; 12.1 The story so far...; 12.2 We’ll change the app to use the database; 12.3 The current DrinkActivity code; 12.4 Get data from the database with a cursor; 12.5 A query lets you say what records you want from the database; 12.6 The SQLiteDatabase query() method lets you build SQL using a query builder; 12.7 Specifying table and columns; 12.8 Applying multiple conditions to your query; 12.9 Order data in your query; 12.10 Using SQL functions in queries; 12.11 SQL GROUP BY and HAVING clauses; 12.12 Get a reference to the database; 12.13 getReadableDatabase() versus getWritableDatabase(); 12.14 getReadableDatabase(); 12.15 getWritableDatabase(); 12.16 The code for getting a cursor; 12.17 To read a record from a cursor, you first need to navigate to it; 12.18 Navigating cursors; 12.19 Getting cursor values; 12.20 The DrinkActivity code; 12.21 What we’ve done so far; 12.22 The current DrinkCategoryActivity code; 12.23 How do we replace the array data in the ListView?; 12.24 A CursorAdapter reads just enough data; 12.25 The story continues; 12.26 A SimpleCursorAdapter maps data to views; 12.27 Creating the SimpleCursorAdapter; 12.28 Closing the cursor and database; 12.29 The revised code for DrinkCategoryActivity; 12.30 Test drive the app; 12.31 Put important information in the top-level activity; 12.32 Add favorites to DrinkActivity; 12.33 Add a new column to the cursor; 12.34 Respond to clicks to update the database; 12.35 The DrinkActivity code; 12.36 Display favorites in TopLevelActivity; 12.37 Display the favorite drinks in activity_top_level.xml; 12.38 What changes are needed for TopLevelActivity.java; 12.39 The new top-level activity code; 12.40 Test drive the app; 12.41 Cursors don’t automatically refresh; 12.42 Change the cursor with changeCursor(); 12.43 The revised TopLevelActivity.java code; 12.44 Test drive the app; 12.45 Databases can make your app go in sloooo-moooo....; 12.46 What code goes on which thread?; 12.47 AsyncTask performs asynchronous tasks; 12.48 The onPreExecute() method; 12.49 The doInBackground() method; 12.50 The onProgressUpdate() method; 12.51 The onPostExecute() method; 12.52 The AsyncTask class; 12.53 Execute the AsyncTask; 12.54 The DrinkActivity.java code; 12.55 A summary of the AsyncTask steps; 12.56 Your Android Toolbox; Chapter 13: Services: At Your Service; 13.1 Services work behind the scenes; 13.2 The started service app; 13.3 We’re going to create an IntentService; 13.4 The IntentService from 50,000 feet; 13.5 How to log messages; 13.6 The full DelayedMessageService code; 13.7 You declare services in AndroidManifest.xml; 13.8 Add a button to activity_main.xml; 13.9 You start a service using startService(); 13.10 Test drive the app; 13.11 We want to send a message to the screen; 13.12 onStartCommand() runs on the main thread; 13.13 The full DelayedMessageService.java code; 13.14 The application context; 13.15 Test drive the app; 13.16 Can we improve on using Toasts?; 13.17 How you use the notification service; 13.18 You create notifications using a notification builder; 13.19 Getting your notification to start an activity; 13.20 Send the notification using the notification service; 13.21 The full code for DelayedMessageService.java; 13.22 What happens when you run the code; 13.23 The story continues; 13.24 Test drive the app; 13.25 Bound services are more interactive; 13.26 The steps needed to create the OdometerService; 13.27 Create a new Odometer project; 13.28 How binding works; 13.29 Define the Binder; 13.30 Get the service to do something; 13.31 The Service class has four key methods; 13.32 Location, location, location...; 13.33 Add the LocationListener to the service; 13.34 Registering the LocationListener; 13.35 Stop location updates when the service is destroyed; 13.36 Tell the activity the distance traveled; 13.37 The full OdometerService.java code; 13.38 Update AndroidManifest.xml; 13.39 Where we’ve got to; 13.40 Update MainActivity’s layout; 13.41 Create a ServiceConnection; 13.42 Bind to the service when the activity starts; 13.43 Display the distance traveled; 13.44 The full MainActivity.java code; 13.45 What happens when you run the code; 13.46 The story continues; 13.47 Test drive the app; 13.48 Your Android Toolbox; Chapter 14: Material Design: Living in a Material World; 14.1 Welcome to Material Design; 14.2 CardViews and RecyclerViews; 14.3 The Pizza app structure; 14.4 Add the pizza data; 14.5 Add the support libraries; 14.6 Create the CardView; 14.7 The full card_captioned_image.xml code; 14.8 RecyclerViews use RecyclerView.Adapters; 14.9 Create the basic adapter; 14.10 Define the adapter’s ViewHolder; 14.11 Create the ViewHolders; 14.12 Each card view displays an image and a caption; 14.13 Add the data to the card views; 14.14 The full code for CaptionedImagesAdapter.java; 14.15 Create the recycler view; 14.16 Add the RecyclerView to the layout; 14.17 The PizzaMaterialFragment.java code; 14.18 A RecyclerView uses a layout manager to arrange its views; 14.19 Specifying the layout manager; 14.20 The full PizzaMaterialFragment.java code; 14.21 Get MainActivity to use the new PizzaMaterialFragment; 14.22 What happens when the code runs; 14.23 The story continues; 14.24 Test drive the app; 14.25 Where we’ve got to; 14.26 Create PizzaDetailActivity; 14.27 What PizzaDetailActivity.java needs to do; 14.28 The code for PizzaDetailActivity.java; 14.29 Getting a RecyclerView to respond to clicks; 14.30 You can listen to views from the adapter; 14.31 Keep your adapters reusable; 14.32 Add the interface to the adapter; 14.33 Implement the listener in PizzaMaterialFragment.java; 14.34 Test drive the app; 14.35 Bring the content forward; 14.36 The full code for fragment_top.xml; 14.37 The full code for TopFragment.java; 14.38 Test drive the app; 14.39 Your Android Toolbox; Leaving town...; ART: The Android Runtime; What is the Android runtime (ART)?; Performance and size; ADB: The Android Debug Bridge; adb: your command-line pal; Running a shell; Get the output from logcat; And much, much more...; The Emulator: The Android Emulator; Why the emulator is so slow; How to speed up your Android development; Leftovers: The Top Ten Things (we didn’t cover); 1. Distributing your app; 2. Content providers; 3. The WebView class; 4. Animation; 5. Maps; 6. Cursor loaders; 7. Broadcast receivers; 8. App widgets; 9. NinePatch graphics; 10. Testing; O’reilly®: Android Development; What will you learn from this book?; Why does this book look so different?;