Head First Android Development, 2nd Edition

Book description

What will you learn from this book?

If you have an idea for a killer Android app, this fully revised and updated edition will help you build your first working application in a jiffy. You’ll learn hands-on how to structure your app, design flexible and interactive interfaces, run services in the background, make your app work on various smartphones and tablets, and much more. It’s like having an experienced Android developer sitting right next to you! All you need to get started is some Java know-how.

Why does this book look so different?

Based on the latest research in cognitive science and learning theory, Head First Android Development uses a visually rich format to engage your mind, rather than a text-heavy approach that puts you to sleep. Why waste your time struggling with new concepts? This multi-sensory learning experience is designed for the way your brain really works.

Publisher resources

View/Submit Errata

Table of contents

  1. Authors of Head First Android Development
  2. How to use this Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. We know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did
    6. Here’s what YOU can do to bend your brain into submission
    7. Read me
  3. The technical review team
  4. Acknowledgments
  5. O’Reilly Safari ®
  6. 1. Getting Started: Diving In
    1. Welcome to Androidville
      1. Layouts define what each screen looks like
      2. Activities define what the app does
      3. Sometimes extra resources are needed too
    2. The Android platform dissected
    3. Here’s what we’re going to do
    4. Your development environment
      1. The Android SDK
      2. Android Studio is a special version of IntelliJ IDEA
    5. Install Android Studio
    6. Build a basic app
    7. How to build the app
      1. 1. Create a new project
    8. How to build the app (continued)
      1. 2. Configure the project
    9. How to build the app (continued)
      1. 3. Specify the minimum SDK
    10. Activities and layouts from 50,000 feet
    11. How to build the app (continued)
      1. 4. Add an activity
    12. How to build the app (continued)
      1. 5. Customize the activity
    13. You’ve just created your first Android app
    14. Android Studio creates a complete folder structure for you
      1. The folder structure includes different types of files
    15. Useful files in your project
    16. Edit code with the Android Studio editors
      1. The code editor
      2. The design editor
    17. Run the app in the Android emulator
      1. So what does the emulator look like?
    18. Create an Android Virtual Device
      1. Open the Android Virtual Device Manager
      2. Select the hardware
    19. Creating an AVD (continued)
      1. Select a system image
    20. Creating an AVD (continued)
      1. Verify the AVD configuration
    21. Run the app in the emulator
      1. Compile, package, deploy, and run
    22. You can watch progress in the console
    23. Test drive
    24. What just happened?
    25. Refine the app
      1. The app has one activity and one layout
      2. The activity controls what the app does
      3. The layout controls the app’s appearance
    26. What’s in the layout?
      1. The design editor
      2. The code editor
    27. activity_main.xml has two elements
    28. Update the text displayed in the layout
    29. Take the app for a test drive
    30. Chapter 1 Your Android Toolbox
  7. 2. Building Interactive Apps: Apps That Do Something
    1. Let’s build a Beer Adviser app
    2. Here’s what we’re going to do
    3. Create the project
    4. We’ve created a default activity and layout
    5. A closer look at the design editor
    6. Add a button using the design editor
      1. Changes in the design editor are reflected in the XML
    7. activity_find_beer.xml has a new button
      1. Buttons and text views are subclasses of the same Android View class
      2. android:id
      3. android:layout_width, android:layout_height
      4. android:text
    8. A closer look at the layout code
      1. The LinearLayout element
    9. A closer look at the layout code (continued)
      1. The Button element
      2. The TextView element
    10. Changes to the XML...
    11. ... are reflected in the design editor
    12. Let’s take the app for a test drive
      1. Here’s what we’ve done so far
    13. Hardcoding text makes localization hard
      1. Put the text in a String resource file
      2. How do you use String resources?
    14. Create the String resource
    15. Use the String resource in your layout
    16. The code for activity_find_beer.xml
    17. Time for a test drive
      1. Here’s what we’ve done so far
    18. Add values to the spinner
      1. Adding an array resource is similar to adding a String
    19. Add the string-array to strings.xml
      1. Get the spinner to reference a string-array
    20. Test drive the spinner
      1. Where we’ve got to
    21. We need to make the button do something
    22. Make the button call a method
      1. Use onClick to say which method the button calls
    23. What activity code looks like
    24. Add an onClickFindBeer() method to the activity
    25. onClickFindBeer() needs to do something
      1. Use findViewById() to get a reference to a view
    26. Once you have a view, you can access its methods
      1. Setting the text in a text view
      2. Retrieving the selected value in a spinner
    27. Update the activity code
    28. The first version of the activity
    29. What the code does
    30. Test drive the changes
    31. Build the custom Java class
      1. Custom Java class spec
      2. Build and test the Java class
    32. Enhance the activity to call the custom Java class so that we can get REAL advice
    33. Activity code version 2
    34. What happens when you run the code
    35. Test drive your app
    36. Chapter 2 Your Android Toolbox
  8. 3. Multiple Activities and Intents: State Your Intent
    1. Apps can contain more than one activity
      1. Here’s what we’re going to do in this chapter
    2. Here’s the app structure
    3. Get started: create the project
    4. Update the layout
    5. Update strings.xml...
      1. ... and add the method to the activity
    6. Create the second activity and layout
    7. What just happened?
    8. Welcome to the Android manifest file
    9. Every activity needs to be declared
    10. An intent is a type of message
    11. Use an intent to start the second activity
    12. What happens when you run the app
    13. The story continues...
    14. Test drive the app
    15. Pass text to a second activity
      1. Let’s start with the layout
    16. Update the text view properties
    17. putExtra() puts extra info in an intent
      1. How to retrieve extra information from an intent
    18. Update the CreateMessageActivity code
    19. Get ReceiveMessageActivity to use the information in the intent
    20. What happens when the user clicks the Send Message button
    21. Test drive the app
    22. We can change the app to send messages to other people
    23. How Android apps work
      1. Intents can start activities in other apps
    24. But we don’t know what apps are on the user’s device
      1. Here’s what you’re going to do
    25. Create an intent that specifies an action
      1. How to create the intent
      2. Adding extra information
    26. Change the intent to use an action
    27. What happens when the code runs
    28. The story continues...
    29. The intent filter tells Android which activities can handle which actions
    30. How Android uses the intent filter
    31. You need to run your app on a REAL device
      1. 1. Enable USB debugging on your device
      2. 2. Set up your system to detect your device
      3. 3. Plug your device into your computer with a USB cable
    32. Running your app on a real device (continued)
      1. 4. Stop your app running on the emulator
      2. 5. Run your app on the physical device
      3. And here’s the app running on the physical device
    33. Test drive the app
      1. If you have one activity
      2. If you have more than one activity
    34. What if you ALWAYS want your users to choose an activity?
      1. Intent.createChooser() displays a chooser dialog
    35. What happens when you call createChooser()
    36. The story continues...
    37. Change the code to create a chooser
      1. Update strings.xml...
      2. ... and update the onSendMessage() method
    38. Test drive the app
      1. If you have one activity
      2. If you have more than one activity
    39. If you have NO matching activities
    40. Chapter 3 Your Android Toolbox
  9. 4. The Activity Lifecycle: Being an Activity
    1. How do activities really work?
    2. The Stopwatch app
      1. Create a new project for the Stopwatch app
    3. Add String resources
      1. Update the stopwatch layout code
    4. The layout code (continued)
    5. How the activity code will work
    6. Add code for the buttons
    7. The runTimer() method
    8. Handlers allow you to schedule code
      1. The post() method
      2. The postDelayed() method
    9. The full runTimer() code
    10. The full StopwatchActivity code
    11. The activity code (continued)
    12. What happens when you run the app
    13. The story continues
    14. Test drive the app
      1. But there’s just one problem...
    15. What just happened?
    16. Rotating the screen changes the device configuration
    17. The states of an activity
    18. The activity lifecycle: from create to destroy
    19. Your activity inherits the lifecycle methods
    20. Save the current state...
    21. ... then restore the state in onCreate()
    22. The updated StopwatchActivity code
    23. What happens when you run the app
    24. The story continues
    25. Test drive the app
    26. There’s more to an activity’s life than create and destroy
      1. Start, stop, and restart
    27. The activity lifecycle: the visible lifetime
    28. We need to implement two more lifecycle methods
      1. Implement onStop() to stop the timer
    29. The updated StopwatchActivity code
    30. What happens when you run the app
    31. Test drive the app
    32. What if an app is only partially visible?
    33. The activity lifecycle: the foreground lifetime
    34. Stop the stopwatch if the activity’s paused
    35. Implement the onPause() and onResume() methods
    36. The complete StopwatchActivity code
    37. The activity code (continued)
    38. The activity code (continued)
    39. What happens when you run the app
    40. Test drive the app
    41. Your handy guide to the lifecycle methods
    42. Chapter 4 Your Android Toolbox
  10. 5. Views and View Groups: Enjoy the View
    1. Your user interface is made up of layouts and GUI components
    2. LinearLayout displays views in a single row or column
      1. You MUST set the layout’s width and height
    3. Orientation is vertical or horizontal
    4. Padding adds space
    5. Add a dimension resource file for consistent padding across layouts
    6. A linear layout displays views in the order they appear in the layout XML
    7. Use margins to add distance between views
    8. Let’s change up a basic linear layout
    9. Here’s the starting point for the linear layout
    10. Make a view streeeeetch by adding weight
    11. Adding weight to one view
    12. Adding weight to multiple views
    13. Gravity controls the position of a view’s contents
    14. Test drive
    15. Values you can use with the android:gravity attribute
    16. layout_gravity controls the position of a view within a layout
    17. More values you can use with the android:layout_gravity attribute
    18. The full linear layout code
    19. LinearLayout: a summary
      1. How you specify a linear layout
      2. Views get displayed in the order they’re listed in the code
      3. Stretch views using weight
      4. Use gravity to specify where a view’s contents appear within a view
      5. Use layout_gravity to specify where a view appears in its available space
    20. Frame layouts stack their views
      1. How you define a frame layout
      2. Create a new project
    21. Add an image to your project
    22. A frame layout stacks views in the order they appear in the layout XML
      1. Position views in the layout using layout_gravity
    23. You can nest layouts
    24. The full code to nest a layout
    25. FrameLayout: a summary
      1. How you specify a frame layout
      2. Views are stacked in the order they appear
      3. Use layout_gravity to specify where a view appears
    26. Layouts and GUI components have a lot in common
    27. GUI components are a type of View
      1. Layouts are a type of View called a ViewGroup
    28. What being a view buys you
      1. Getting and setting properties
      2. Size and position
      3. Focus handling
      4. Event handling and listeners
    29. A layout is really a hierarchy of Views
    30. Playing with views
    31. Text view
      1. Defining it in XML
      2. Using it in your activity code
    32. Editable text view
      1. Defining it in XML
      2. Using it in your activity code
    33. Button
      1. Defining it in XML
      2. Using it in your activity code
    34. Toggle button
      1. Defining it in XML
      2. Using it in your activity code
    35. Switch
      1. Defining it in XML
      2. Using it in your activity code
    36. Checkboxes
      1. Defining them in XML
      2. Using them in your activity code
    37. Checkboxes (continued)
    38. Radio buttons
      1. Defining them in XML
      2. Using them in your activity code
    39. Radio buttons (continued)
    40. Spinner
      1. Defining it in XML
      2. Using it in your activity code
    41. Image view
      1. Adding an image to your project
    42. Image view: the layout XML
      1. Using image views in your activity code
    43. Adding images to buttons
      1. Displaying text and an image on a button
    44. Image button
      1. Defining it in XML
      2. Using it in your activity code
    45. Scroll view
    46. Toasts
      1. Using it in your activity code
    47. Chapter 5 Your Android Toolbox
  11. 6. Constraint Layouts: Put Things in Their Place
    1. Nested layouts can be inefficient
    2. Introducing the constraint layout
      1. You design constraint layouts VISUALLY
      2. Create a new project
    3. Make sure your project includes the Constraint Layout Library
    4. Add the String resources to strings.xml
      1. Change activity_main.xml to use a constraint layout
    5. Use the blueprint tool
    6. Position views using constraints
      1. We’ll add a horizontal constraint to the button
    7. Add a vertical constraint
      1. Changing the view’s margins
    8. Changes to the blueprint are reflected in the XML
    9. How to center views
    10. Adjust a view’s position by updating its bias
    11. How to change a view’s size
      1. 1. Make the view a fixed size
      2. 2. Make it just big enough
      3. 3. Match the view’s constraints
      4. 4. Specify the width:height ratio
    12. How to align views
    13. Let’s build a real layout
    14. First, add the top line of views
      1. Get the design editor to infer the constraints
    15. The Infer Constraints feature guesses which constraints to add
    16. Add the next line to the blueprint...
      1. ... and then add the button
    17. Finally, add a view for the message
    18. Test drive the app
    19. Chapter 6 Your Android Toolbox
  12. 7. List Views and Adapters: Getting Organized
    1. Every app starts with ideas
    2. Organize your ideas: top-level, category, and detail/edit activities
      1. Top-level activities
      2. Category activities
      3. Detail/edit activities
    3. Navigating through the activities
      1. Top-level activities go at the top
      2. Category activities go between top-level and detail/edit activities
      3. Detail/edit activities
    4. Use list views to navigate to data
    5. We’re going to build part of the Starbuzz app
      1. The top-level activity
      2. The drink category activity
    6. The drink detail activity
      1. How the user navigates through the app
    7. The Starbuzz app structure
    8. Here’s what we’re going to do
      1. Create the project
    9. The Drink class
    10. The image files
    11. The top-level layout contains an image and a list
    12. Use a list view to display the list of options
      1. How to define a list view in XML
    13. The full top-level layout code
    14. Test drive
    15. Get list views to respond to clicks with a listener
      1. OnItemClickListener listens for item clicks
    16. Set the listener to the list view
      1. The full TopLevelActivity.java code
    17. TopLevelActivity.java (continued)
      1. What the TopLevelActivity.java code does
    18. Where we’ve got to
    19. A category activity displays the data for a single category
      1. Create DrinkCategoryActivity
    20. Update activity_drink_category.xml
    21. For nonstatic data, use an adapter
    22. Connect list views to arrays with an array adapter
    23. Add the array adapter to DrinkCategoryActivity
    24. What happens when you run the code
    25. Test drive the app
    26. App review: where we are
    27. How we handled clicks in TopLevelActivity
    28. Pass the ID of the item that was clicked by adding it to an intent
    29. The full DrinkCategoryActivity code
    30. A detail activity displays data for a single record
    31. Retrieve data from the intent
    32. Update the views with the data
    33. The DrinkActivity code
    34. What happens when you run the app
    35. The story continues
    36. Test drive the app
    37. Chapter 7 Your Android Toolbox
  13. 8. Support Libraries and App Bars: Taking Shortcuts
    1. Great apps have a clear structure
      1. Top-level screens
      2. Category screens
      3. Detail/edit screens
      4. They also have great shortcuts
    2. Different types of navigation
    3. Here’s what we’re going to do
    4. Add an app bar by applying a theme
    5. Support libraries allow you to use new features in older versions of Android
    6. Create the Pizza app
    7. Add the v7 AppCompat Support Library
    8. AppCompatActivity lets you use AppCompat themes
    9. MainActivity needs to be an AppCompatActivity
    10. AndroidManifest.xml can change your app bar’s appearance
    11. How to apply a theme
      1. 1. Hardcoding the theme
      2. 2. Using a style to apply the theme
    12. Define styles in a style resource file
    13. Theme gallery
    14. Customize the look of your app
    15. Define colors in a color resource file
    16. The code for activity_main.xml
    17. Test drive the app
    18. ActionBar vs. Toolbar
      1. How to add a toolbar
    19. 1. Add the AppCompat Support Library
    20. 2. Extend the AppCompatActivity class
    21. 3. Remove the app bar
    22. 4. Add a toolbar to the layout
    23. Add the toolbar to the layout...
    24. ... or define the toolbar as a separate layout
    25. Include the toolbar in the activity’s layout
    26. 5. Set the toolbar as the activity’s app bar
    27. Test drive the app
    28. Add actions to the app bar
      1. Create OrderActivity
    29. Update activity_order.xml
    30. Update OrderActivity.java
      1. Add a String resource for the activity’s title
    31. Change the app bar text by adding a label
    32. The code for AndroidManifest.xml
      1. How to add an action to an app bar
    33. 1. Add the action’s resources
      1. Add the icon
      2. Add the action’s title as a String resource
    34. 2. Create the menu resource file
    35. Control the action’s appearance
    36. 3. Add the menu to the app bar with the onCreateOptionsMenu() method
    37. 4. React to action item clicks with the onOptionsItemSelected() method
    38. The full MainActivity.java code
    39. Test drive the app
      1. But how do we get back to MainActivity?
    40. Enable Up navigation
    41. Set an activity’s parent
    42. Adding the Up button
    43. Test drive the app
    44. Sharing content on the app bar
      1. You share the content with an intent
    45. Add a share action provider to menu_main.xml
    46. Specify the content with an intent
    47. The full MainActivity.java code
    48. The MainActivity.java code (continued)
    49. Test drive the app
    50. Chapter 8 Your Android Toolbox
  14. 9. Fragments: Make It Modular
    1. Your app needs to look great on ALL devices
      1. On a phone:
      2. On a tablet:
    2. Your app may need to behave differently too
      1. On a phone:
      2. On a tablet:
      3. But that means you might duplicate code
    3. Fragments allow you to reuse code
      1. A fragment has a layout
    4. The phone version of the app
    5. Here’s what we’re going to do
    6. Create the project and activities
      1. Add the AppCompat Support Library
    7. Add a button to MainActivity’s layout
    8. Make the button respond to clicks
    9. How to add a fragment to your project
    10. What fragment code looks like
    11. The fragment’s onCreateView() method
    12. Fragment layout code looks just like activity layout code
    13. Add a fragment to an activity’s layout
    14. Simplify the layout
    15. Support Library fragments need activities that extend FragmentActivity
    16. What the code does
    17. The story continues
    18. Test drive the app
    19. Get the fragment and activity to interact
    20. The Workout class
    21. Pass the workout ID to the fragment
    22. Use the fragment manager to manage fragments
    23. Get the activity to set the workout ID
    24. Activity states revisited
    25. The fragment lifecycle
    26. Fragments inherit lifecycle methods
    27. Set the view’s values in the fragment’s onStart() method
    28. What happens when the code runs
    29. The story continues
    30. Test drive the app
    31. Where we’ve got to
    32. We need to create a fragment with a list
    33. A list fragment is a fragment that contains only a list
    34. How to create a list fragment
    35. Adapters revisited
    36. Our previous array adapter
      1. A fragment isn’t a subclass of Context
    37. The updated WorkoutListFragment code
    38. Display WorkoutListFragment in the MainActivity layout
    39. The code for activity_main.xml
    40. What happens when the code runs
    41. The story continues
    42. Test drive the app
    43. Connect the list to the detail
    44. We need to decouple the fragment by using an interface
      1. What WorkoutListFragment needs to do
      2. 1. Define the listener interface
      3. 2. Register the listener
      4. 3. Respond to clicks
    45. The code for WorkoutListFragment.java
    46. MainActivity needs to implement the interface
    47. DetailActivity needs to pass the ID to WorkoutDetailFragment
    48. What happens when the code runs
    49. The story continues...
    50. Test drive the app
    51. Chapter 9 Your Android Toolbox
      1. Fragment lifecycle methods
  15. 10. Fragments for Larger Interfaces: Different Size, Different Interface
    1. The Workout app looks the same on a phone and a tablet
    2. Designing for larger interfaces
    3. The phone version of the app
    4. The tablet version of the app
    5. Here’s what we’re going to do
    6. Create a tablet AVD
      1. Open the Android Virtual Device Manager
      2. Select the hardware
    7. Creating a tablet AVD (continued)
      1. Select a system image
    8. Creating a tablet AVD (continued)
      1. Verify the AVD configuration
    9. Put screen-specific resources in screen-specific folders
    10. The different folder options
    11. Tablets use layouts in the layout-large folder
    12. The layout-large version of the layout needs to display two fragments
    13. What the updated code does
    14. Test drive the app
    15. We need to change the itemClicked() code
      1. But how do we update the workout details?
    16. You want fragments to work with the Back button
    17. Welcome to the back stack
      1. A back stack scenario
    18. Back stack transactions don’t have to be activities
      1. Don’t update—instead, replace
    19. Use a frame layout to replace fragments programmatically
    20. Use layout differences to tell which layout the device is using
    21. The revised MainActivity code
    22. Using fragment transactions
      1. 1. Begin the transaction
      2. 2. Specify the changes
      3. 3. Commit the transaction
    23. The updated MainActivity code
    24. What happens when the code runs
    25. The story continues...
    26. Test drive the app
    27. Rotating the tablet breaks the app
    28. Saving an activity’s state (revisited)
      1. Fragments can lose state too
    29. Save the fragment’s state...
      1. ... then use onCreate() to restore the state
    30. The updated code for WorkoutDetailFragment.java
    31. WorkoutDetailFragment.java (continued)
    32. Test drive the app
    33. Chapter 10 Your Android Toolbox
  16. 11. Dynamic Fragments: Nesting Fragments
    1. Adding dynamic fragments
    2. Here’s what we’re going to do
    3. The new version of the app
    4. Create TempActivity
    5. TempActivity needs to extend AppCompatActivity
      1. We’ll add a new stopwatch fragment
    6. Fragments and activities have similar lifecycles...
      1. ... but the methods are slightly different
    7. The StopwatchFragment.java code
    8. StopwatchFragment.java (continued)
    9. StopwatchFragment.java (continued)
    10. The StopwatchFragment layout
    11. The StopwatchFragment layout (continued)
      1. The StopwatchFragment layout uses String values
    12. Add StopwatchFragment to TempActivity’s layout
    13. Test drive the app
    14. The app crashes if you click on a button
    15. Let’s look at the StopwatchFragment layout code
    16. The onClick attribute calls methods in the activity, not the fragment
      1. How to make button clicks call methods in the fragment
    17. 1. Remove the onClick attributes from the fragment’s layout
    18. 2. Change the onClick... method signatures
    19. 3. Make the fragment implement OnClickListener
      1. The StopwatchFragment onClick() method
    20. Attach the OnClickListener to the buttons
    21. The StopwatchFragment code
    22. The StopwatchFragment code (continued)
    23. The StopwatchFragment code (continued)
    24. Test drive the app
    25. Rotating the device resets the stopwatch
    26. Use <fragment> for static fragments...
      1. ... but dynamic fragments need a fragment transaction
    27. Change activity_temp.xml to use a FrameLayout
      1. Add a fragment transaction to TempActivity.java
    28. The full code for TempActivity.java
    29. Test drive the app
    30. Add the stopwatch to WorkoutDetailFragment
    31. What we’re going to do
      1. Start MainActivity when the app launches
    32. Add a FrameLayout where the fragment should appear
    33. So far, we’ve only used fragment transactions in activities
      1. Using fragment transactions in fragments uses most of the same code
    34. Using getFragmentManager() creates extra transactions on the back stack
    35. Using getChildFragmentManager() creates nested transactions instead
    36. What getChildFragmentManager() fragment transaction code looks like
    37. The full WorkoutDetailFragment.java code
    38. The full code (continued)
    39. Test drive the app
    40. Test drive (continued)
    41. Chapter 11 Your Android Toolbox
  17. 12. Design Support Library: Swipe Right
    1. The Bits and Pizzas app revisited
    2. The app structure
    3. Here’s what we’re going to do
    4. Create TopFragment
    5. Create PizzaFragment
    6. Create PastaFragment
    7. Create StoresFragment
    8. Use a view pager to swipe through fragments
      1. What view pager layout code looks like
    9. Add a view pager to MainActivity’s layout
    10. Tell a view pager about its pages using a fragment pager adapter
    11. The code for our fragment pager adapter
    12. Attach the fragment pager adapter to the view pager
    13. The full code for MainActivity.java
    14. The MainActivity.java code (continued)
    15. The MainActivity.java code (continued)
    16. Test drive the app
    17. Add tab navigation to MainActivity
    18. How to add tabs to your layout
    19. Add tabs to MainActivity’s layout
    20. Link the tab layout to the view pager
    21. The full code for MainActivity.java
    22. The MainActivity.java code (continued)
    23. The MainActivity.java code (continued)
    24. Test drive the app
    25. The Design Support Library helps you implement material design
    26. Here’s what we’ll do
    27. Make the toolbar respond to scrolls
      1. Use a CoordinatorLayout to coordinate animations between views
    28. Add a coordinator layout to MainActivity’s layout
    29. How to coordinate scroll behavior
      1. Mark the view the user will scroll
      2. Tell the toolbar to respond to scroll events
    30. The code to enable to toolbar to scroll
    31. Add scrollable content to TopFragment
      1. Add String and image resources
    32. Use a nested scroll view to make layout content scrollable
    33. How we’ll structure TopFragment’s layout
    34. The full code for fragment_top.xml
    35. Test drive the app
    36. Add a collapsing toolbar to OrderActivity
      1. First add some String resources
    37. How to create a plain collapsing toolbar
      1. Nested scroll view attributes
      2. Collapsing toolbar layout attributes
      3. App bar layout attributes
      4. Toolbar attributes
    38. The full code to add a collapsing toolbar to activity_order.xml
    39. The activity_order.xml code (continued)
    40. Test drive the app
      1. You can add images to collapsing toolbars too
    41. How to add an image to a collapsing toolbar
    42. The updated code for activity_order.xml
    43. The activity_order.xml code (continued)
    44. Test drive the app
    45. FABs and snackbars
      1. We’ll add a FAB and snackbar to OrderActivity
    46. Add the icon for the FAB
      1. How to add a FAB to your layout
    47. The updated code for activity_order.xml
    48. The activity_order.xml code (continued)
      1. Add the onClickDone() method to OrderActivity
    49. How to create a snackbar
    50. The full code for OrderActivity.java
    51. Test drive the app
    52. Chapter 12 Your Android Toolbox
  18. 13. Recycler Views and Card Views: Get Recycling
    1. There’s still work to do on the Bits and Pizzas app
    2. Recycler views from 10,000 feet
    3. Here’s what we’re going to do
    4. Add the pizza data
      1. Add the Pizza class
    5. Display the pizza data in a card
      1. Add the CardView and RecyclerView Support Libraries
    6. How to create a card view
    7. The full card_captioned_image.xml code
    8. How our recycler view adapter will work
    9. Add a recycler view adapter
    10. Tell the adapter what data it should work with...
      1. ... and implement the getItemCount() method
    11. Define the adapter’s view holder
    12. Override the onCreateViewHolder() method
    13. Add the data to the card views
    14. The full code for CaptionedImagesAdapter.java
    15. The full CaptionedImagesAdapter.java code (continued)
    16. Create the recycler view
      1. Add a layout for PizzaFragment
    17. Add the RecyclerView to PizzaFragment’s layout
      1. Get the recycler view to use the adapter
    18. The full PizzaFragment.java code
    19. A recycler view uses a layout manager to arrange its views
    20. Specify the layout manager
      1. Linear layout manager
      2. Grid layout manager
      3. Staggered grid layout manager
    21. The full PizzaFragment.java code
    22. What happens when the code runs
    23. The story continues
    24. Test drive the app
    25. Make the recycler view respond to clicks
    26. Create PizzaDetailActivity
    27. What PizzaDetailActivity needs to do
      1. Update AndroidManifest.xml to give PizzaDetailActivity a parent
    28. The code for PizzaDetailActivity.java
    29. Get a recycler view to respond to clicks
    30. You can listen for view events from the adapter
    31. Keep your adapters reusable
      1. Decouple your adapter with an interface
    32. Add the interface to the adapter
    33. The CaptionedImagesAdapter.java code (continued)
    34. Implement the listener in PizzaFragment.java
    35. The PizzaFragment.java code (continued)
    36. Test drive the app
    37. Chapter 13 Your Android Toolbox
  19. 14. Navigation Drawers: Going Places
    1. Tab layouts allow easy navigation...
    2. ... but navigation drawers let you show more options
    3. We’re going to create a navigation drawer for a new email app
    4. Navigation drawers deconstructed
    5. Here’s what we’re going to do
    6. Create the CatChat project
      1. Add the v7 AppCompat and Design Support Libraries
    7. Create InboxFragment
    8. Create DraftsFragment
    9. Create SentItemsFragment
    10. Create TrashFragment
    11. Create a toolbar layout
    12. Update the app’s theme
    13. Create HelpActivity
    14. Create FeedbackActivity
    15. We need to build a navigation drawer
    16. Create the navigation drawer’s header
      1. Add the image file
      2. Add the String resources
    17. The full nav_header.xml code
    18. The drawer gets its options from a menu
      1. We need to create a menu with two sections
    19. Add items in the order you want them to appear in the drawer
    20. How to group items together
    21. We’ll use a group for the first section
    22. Add the support section as a submenu
    23. The full menu_nav.xml code
    24. How to create a navigation drawer
    25. The full code for activity_main.xml
    26. Add InboxFragment to MainActivity’s frame layout
    27. Test drive the app
    28. What the activity code needs to do
    29. Add a drawer toggle
    30. Respond to the user clicking items in the drawer
    31. Implement the onNavigationItemSelected() method
    32. Close the drawer when the user presses the Back button
    33. The full MainActivity.java code
    34. MainActivity.java (continued)
    35. MainActivity.java (continued)
    36. Test drive the app
    37. Chapter 14 Your Android Toolbox
  20. 15. SQLite Databases: Fire Up the Database
    1. Back to Starbuzz
    2. Android uses SQLite databases to persist data
      1. Where’s the database stored?
    3. Android comes with SQLite classes
      1. The SQLite Helper
      2. The SQLite Database
      3. Cursors
    4. The current Starbuzz app structure
    5. Let’s change the app to use a database
    6. The SQLite helper manages your database
    7. Create the SQLite helper
    8. Specify the database
    9. Inside a SQLite database
      1. Storage classes and data types
    10. You create tables using Structured Query Language (SQL)
      1. The onCreate() method is called when the database is created
    11. Insert data using the insert() method
    12. Insert multiple records
    13. The StarbuzzDatabaseHelper code
    14. What the SQLite helper code does
    15. What if you need to make changes to the database?
    16. SQLite databases have a version number
    17. What happens when you change the version number
      1. Scenario 1: A first-time user installs the app
      2. Scenario 2: an existing user installs the new version
    18. Upgrade your database with onUpgrade()
    19. Downgrade your database with onDowngrade()
    20. Let’s upgrade the database
    21. The SQLite helper code (continued)
    22. Upgrade an existing database
      1. How to update records
    23. Update records with the update() method
    24. Apply conditions to multiple columns
      1. Delete records with the delete() method
    25. Change the database structure
      1. Add new columns to tables using SQL
      2. Renaming tables
    26. Delete tables by dropping them
      1. Execute the SQL using execSQL()
    27. The full SQLite helper code
    28. The SQLite helper code (continued)
    29. What happens when the code runs
    30. Chapter 15 Your Android Toolbox
  21. 16. Basic Cursors: Getting Data Out
    1. The story so far...
    2. The new Starbuzz app structure
    3. What we’ll do to change DrinkActivity to use the Starbuzz database
    4. The current DrinkActivity code
    5. Get a reference to the database
    6. Get data from the database with a cursor
    7. Return all the records from a table
    8. Return records in a particular order
    9. Return selected records
    10. The DrinkActivity code so far
    11. To read a record from a cursor, you first need to navigate to it
    12. Navigate cursors
    13. Get cursor values
    14. Finally, close the cursor and the database
    15. The DrinkActivity code
    16. The DrinkActivity code (continued)
    17. What we’ve done so far
    18. What we’ll do to change DrinkCategoryActivity to use the Starbuzz database
    19. The current DrinkCategoryActivity code
    20. Get a reference to the Starbuzz database...
    21. ... then create a cursor that returns the drinks
    22. How do we replace the array data in the list view?
    23. A simple cursor adapter maps cursor data to views
    24. How to use a simple cursor adapter
    25. Close the cursor and database
    26. The story continues
    27. The revised code for DrinkCategoryActivity
    28. The DrinkCategoryActivity code (continued)
    29. Test drive the app
    30. Chapter 16 Your Android Toolbox
  22. 17. Cursors and Asynctasks: Staying in the Background
    1. We want our Starbuzz app to update database data
    2. We’ll update DrinkActivity first
    3. Add a checkbox to DrinkActivity’s layout
    4. Display the value of the FAVORITE column
    5. Respond to clicks to update the database
    6. The full DrinkActivity.java code
    7. DrinkActivity.java (continued)
    8. DrinkActivity.java (continued)
    9. Test drive the app
    10. Display favorites in TopLevelActivity
    11. Display the favorite drinks in activity_top_level.xml
    12. Refactor TopLevelActivity.java
    13. What changes are needed for TopLevelActivity.java
    14. Ready Bake Code
    15. The new TopLevelActivity.java code
    16. The TopLevelActivity.java code (continued)
    17. The TopLevelActivity.java code (continued)
    18. Test drive the app
    19. Cursors don’t automatically refresh
    20. Change the cursor with changeCursor()
      1. 1. Define the cursor
      2. 2. Get a reference to the cursor adapter
      3. 3. Change the cursor using changeCursor()
    21. The revised TopLevelActivity.java code
    22. The TopLevelActivity.java code (continued)
    23. The TopLevelActivity.java code (continued)
    24. Test drive the app
    25. Databases can make your app go in sloooow-moooo....
      1. Life is better when threads work together
    26. What code goes on which thread?
    27. AsyncTask performs asynchronous tasks
    28. The onPreExecute() method
    29. The doInBackground() method
    30. The onProgressUpdate() method
    31. The onPostExecute() method
    32. The AsyncTask class parameters
    33. The full UpdateDrinkTask class
    34. Execute the AsyncTask...
    35. ... in DrinkActivity’s onFavoritesClicked() method
    36. The full DrinkActivity.java code
    37. The full DrinkActivity.java code (continued)
    38. The full DrinkActivity.java code (continued)
    39. The full DrinkActivity.java code (continued)
    40. Test drive the app
    41. Chapter 17 Your Android Toolbox
    42. A summary of the AsyncTask steps
  23. 18. Started Services: At Your Service
    1. Services work in the background
      1. There are three types of service
    2. We’ll create a STARTED service
      1. Create the project
    3. Use the IntentService class to create a basic started service
    4. How to log messages
    5. The full DelayedMessageService code
    6. You declare services in AndroidManifest.xml
    7. Add a button to activity_main.xml
    8. You start a service using startService()
    9. What happens when you run the app
    10. Test drive the app
    11. The states of a started service
    12. The started service lifecycle: from create to destroy
    13. Your service inherits the lifecycle methods
    14. Android has a built-in notification service
    15. We’ll use notifications from the AppCompat Support Library
    16. First create a notification builder
    17. Add an action to tell the notification which activity to start when clicked
    18. Issue the notification using the built-in notification service
    19. The full code for DelayedMessageService.java
    20. The DelayedMessageService.java code (continued)
    21. What happens when you run the code
    22. The story continues
    23. Test drive the app
    24. Chapter 18 Your Android Toolbox
  24. 19. Bound Services and Permissions: Bound Together
    1. Bound services are bound to other components
    2. Here’s what we’re going to do
      1. Create a new Odometer project
    3. Create a new service
    4. Implement a binder
    5. Add a getDistance() method to the service
    6. Update MainActivity’s layout
    7. What MainActivity needs to do
    8. Create a ServiceConnection
    9. The onServiceConnected() method
    10. The onServiceDisconnected() method
    11. Use bindService() to bind the service
    12. Use unbindService() to unbind from the service
    13. Call OdometerService’s getDistance() method
    14. The full MainActivity.java code
    15. The MainActivity.java code (continued)
    16. What happens when you run the code
    17. The story continues
    18. The story continues
    19. Test drive the app
    20. The states of a bound service
    21. The bound service lifecycle: from create to destroy
    22. We’ll use Android’s Location Services to return the distance traveled
    23. Add the AppCompat Support Library
    24. Declare the permissions you need
    25. Add a location listener to OdometerService
    26. We need a location manager and location provider
      1. Create a location manager
      2. Specify the location provider
    27. Request location updates...
    28. ... but first check that your app has permission
    29. Here’s the updated OdometerService code
    30. Calculate the distance traveled
    31. Return the miles traveled
    32. Stop the listener getting location updates
    33. The full OdometerService.java code
    34. The OdometerService.java code (continued)
    35. The OdometerService.java code (continued)
    36. Test drive the app
    37. Get the app to request permission
    38. Check permissions at runtime
      1. Ask for permissions you don’t have
    39. Check for Location Services permissions in MainActivity’s onStart() method
    40. Check the user’s response to the permission request
    41. Issue a notification if we’re denied permission
    42. Add notification code to onRequestPermissionsResults()
    43. The notification code (continued)
    44. The full code for MainActivity.java
    45. The MainActivity.java code (continued)
    46. The MainActivity.java code (continued)
    47. Test drive the app
    48. Chapter 19 Your Android Toolbox
    49. Leaving town...
    50. It’s been great having you here in Androidville
  25. A. Relative and Grid Layouts: Meet the Relatives
    1. A relative layout displays views in relative positions
      1. Positioning views relative to the parent layout
    2. Positioning views to the left or right
      1. Use start and end to take language direction into account
    3. Attributes for positioning views relative to the parent layout
    4. Positioning views relative to other views
    5. Attributes for positioning views relative to other views
    6. A grid layout displays views in a grid
      1. How you define a grid layout
    7. Adding views to the grid layout
    8. Let’s create a new grid layout
      1. Here’s what we’re going to do
    9. We’ll start with a sketch
      1. The grid layout needs two columns
    10. Row 0: add views to specific rows and columns
    11. Row 1: make a view span multiple columns
    12. Row 2: make a view span multiple columns
    13. The full code for the grid layout
    14. The grid layout code (continued)
  26. B. Gradle: The Gradle Build Tool
    1. What has Gradle have the Romans ever done for us?
      1. Your project’s Gradle files
    2. Your app’s main Gradle file
    3. Gradle comes built in to your project
    4. The check task
      1. The clean installDebug task
    5. The androidDependencies task
    6. gradlew <your-task-goes-here>
    7. Gradle plug-ins
  27. C. Art: The Android Runtime
    1. What is the Android runtime?
      1. Previously, Java code ran on the Oracle JVM
    2. ART compiles code into DEX files
    3. How DEX files get created
    4. DEX files are zipped into APK files
      1. You may need to sign the .apk file
    5. Say hello to the Android Debug Bridge (adb)
      1. How apps come alive: running your APK file
    6. Android converts the .dex code to OAT format
  28. D. ADB: The Android Debug Bridge
    1. adb: your command-line pal
    2. Running a shell
    3. Useful shell commands
    4. Kill the adb server
      1. Get the output from logcat
    5. Copying files to/from your device
      1. And much, much more...
  29. E. The Android Emulator: Speeding Things Up
    1. Why the emulator is so slow
    2. How to speed up your Android development
      1. 1. Use a real device
      2. 2. Use an emulator snapshot
      3. 3. Use hardware acceleration
      4. 4. Use Instant Run
  30. F. Leftovers: The Top Ten Things (we didn’t cover)
    1. 1. Distributing your app
      1. Preparing your app for release
      2. Releasing your app
    2. 2. Content providers
    3. 3. Loaders
    4. 4. Sync adapters
    5. 5. Broadcasts
    6. 6. The WebView class
    7. 7. Settings
    8. 8. Animation
      1. Property animation
      2. View animations
      3. Activity transitions
    9. 9. App widgets
    10. 10. Automated testing
      1. Unit tests
      2. On-device tests
  31. Index

Product information

  • Title: Head First Android Development, 2nd Edition
  • Author(s): Dawn Griffiths, David Griffiths
  • Release date: August 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491974056