Head First Android Development, 3rd 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 get you up and running in a jiffy. You'll go beyond syntax and how-to manuals and learn how to think like a great Android developer. This hands-on book teaches you everything from designing user interfaces to building multi-screen apps that persist data in a database. It covers the latest features of Android Jetpack, including Jetpack Compose. It's like having an experienced Android developer sitting right next to you! If you have some Kotlin know-how, you're ready to get started.

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 multisensory 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. Table of Contents (the real thing)
  3. 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
      1. Here’s what YOU can do to bend your brain into submission
    6. Read me
    7. The truly awesome technical review team
    8. Acknowledgments
    9. O’Reilly Online Learning
  4. 1. Getting Started: Diving In
    1. Welcome to Androidville
      1. Activities define what the app does
      2. Layouts define what each screen looks like
      3. There may be extra files too
    2. Activities and layouts form the backbone of your app
    3. Here’s what we’re going to do
    4. Android Studio: your development environment
      1. You need to install Android Studio
    5. Install Android Studio
    6. Let’s build a basic app
    7. How to build the app
      1. 1. Create a new project
      2. 2. Select a project template
      3. 3. Configure your project
    8. You’ve created your first Android project
    9. Dissecting your new project
      1. The folder structure includes different types of files
    10. Introducing the key files in your project
    11. Edit code with the Android Studio editors
      1. The code editor
      2. The design editor
    12. The story so far
    13. How to run the app on a physical device
      1. 1. Enable USB debugging on your device
      2. 2. Set up your computer to detect the device
      3. 3. Use a USB cable to plug your device into your computer
      4. 4. Run the app
    14. How to run the app on a virtual device
    15. Create an Android Virtual Device (AVD)
      1. Open the Android Virtual Device Manager
      2. Select the hardware
      3. Select a system image
      4. Verify the AVD configuration
      5. The virtual device gets created
      6. Run the app on the AVD
    16. Compile, package, deploy, run
    17. Test Drive
    18. What just happened?
    19. Let’s 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
    20. What’s in the layout?
      1. The design editor
      2. The code editor
    21. activity_main.xml has two elements
    22. Update the text displayed in the layout
    23. What the code does
    24. Test Drive
    25. Your Android Toolbox
  5. 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_main.xml has a new button
      1. Buttons and text views are subclasses of the same Android View class
    8. A closer look at the layout code
      1. The <LinearLayout> element
      2. The <Button> element
      3. The <TextView> element
    9. Let’s update the layout XML
    10. The XML changes are reflected in the design editor
    11. Test Drive
    12. There are warnings in the layout…
      1. …because there’s hardcoded text
    13. Put text in a String resource file
      1. Android Studio helps you extract String resources
    14. Extract the String resource
      1. A String resource has been added to strings.xml
    15. activity_main.xml uses the String resource
      1. You also can extract String resources manually
    16. Add and use a new String resource
    17. Test Drive
    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 display the array’s values
    20. The full code for activity_main.xml
    21. Test Drive
    22. We need to make the app interactive
    23. What the MainActivity code looks like
    24. A button can listen for on-click events…
      1. …using an OnClickListener
    25. Get a reference to the button…
      1. …and call its setOnClickListener method
    26. Pass a lambda to the setOnClickListener method
    27. How to edit a text view’s text
      1. How to get the spinner’s value
    28. Pool Puzzle
    29. Pool Puzzle Solution
    30. The updated code for MainActivity.kt
    31. What happens when you run the code
    32. Test Drive
    33. Add the getBeers() method
    34. Activity Magnets
    35. Activity Magnets Solution
    36. The full code for MainActivity.kt
    37. What happens when you run the code
    38. Test Drive
    39. Your Android Toolbox
  6. 3. Layouts: Being a layout
    1. It all starts with a layout
    2. Android has different types of layout
      1. Use the best layout for your screen design
    3. Let’s build a linear layout
      1. Create a new project
    4. How to define a linear layout
    5. Orientation can be vertical or horizontal
    6. Use padding to add space to the layout’s edges
    7. The layout code so far
    8. An edit text lets you enter text
    9. Add views to the layout XML
    10. Make a view streeeeetch by adding weight
    11. How to add weight to one view
    12. How to add weight to multiple views
    13. The gravity attribute controls the position of a view’s contents
    14. Values you can use with the android:gravity attribute
    15. Layout Magnets
    16. Layout Magnets Solution
    17. The story so far
    18. layout-gravity controls the position of a view within a layout
    19. More values you can use with the android:layout-gravity attribute
    20. Use margins to add space between views
    21. The full linear layout code
    22. Test Drive
    23. BE the Layout
    24. BE the Layout Solution
    25. Your activity code tells Android which layout it uses
      1. The layout’s views are inflated into objects
    26. Layout inflation: an example
    27. A frame layout stacks its views
      1. Create a new project
      2. How to define a frame layout
    28. Add an image to your project
    29. A frame layout stacks views in the order they appear in the layout XML
    30. Test Drive
    31. All layouts are a type of ViewGroup…
      1. …and a ViewGroup is a type of View
    32. A scroll view inserts a vertical scrollbar
    33. How to add a scroll view
    34. Test Drive
    35. Your Android Toolbox
  7. 4. Constraint Layouts: Draw Up a Blueprint
    1. Nested layouts revisited
    2. Nesting layouts comes at a price
    3. Introducing the constraint layout
      1. You design constraint layouts VISUALLY
    4. Constraint layouts are part of Android Jetpack
    5. Here’s what we’re going to do
      1. Create a new project
    6. Use Gradle to include Jetpack libraries
      1. The project build.gradle needs a Google repository line
      2. The app build.gradle includes the constraint layout’s library
    7. Let’s add a constraint layout to activity_main.xml
      1. Show the layout in the blueprint
    8. Add a button to the blueprint
    9. Position views using constraints
      1. We’ll add a horizontal constraint to the button
    10. Add a vertical constraint too
    11. Use opposing constraints to center views
    12. You can delete constraints you no longer need
    13. Remove constraints with the constraint widget
      1. You can use it to add margins too
    14. Changes to the blueprint appear in the XML
    15. Views can have bias
    16. Test Drive
    17. You can change a view’s size
      1. Make the view just big enough
      2. Match the view’s constraints
    18. BE the Constraint
    19. BE the Constraint Solution
    20. Most layouts need multiple views
      1. Add a second button to the blueprint
    21. You can connect views to other views
    22. You can align views too
    23. Align views using guidelines
    24. Guidelines have a fixed position
    25. Create a movable barrier
      1. Let’s build a layout that uses a barrier
    26. Add a horizontal barrier
      1. Place the barrier beneath the views
    27. Constrain a button under the barrier
    28. The full code for activity_main.xml
    29. Test Drive
    30. Use a chain to control a linear group of views
      1. We’re going to create a horizontal chain
    31. The chain will use three buttons
      1. Align the views we’re going to chain
    32. Create the horizontal chain
    33. There are different styles of chain
      1. Spread spaces out views between the blueprint’s edges
      2. Spread inside moves the first and last view to the edges
      3. Packed moves the views together
    34. Test Drive
    35. A flow is like a multi-line chain
    36. How to add a flow
    37. You can control the flow’s appearance
      1. Use “chain” to create a multi-line chain
      2. Use “aligned” to line up the views
    38. The full code for activity_main.xml
    39. Test Drive
    40. Your Android Toolbox
  8. 5. The Activity Lifecycle: Being an Activity
    1. How do activities really work?
      1. Introducing the Stopwatch app
    2. Create a new project
      1. Add String resources for the text labels
      2. The layout includes a Chronometer view
    3. The full code for activity_main.xml
    4. The activity code controls the stopwatch
      1. The Chronometer’s key properties and methods
      2. The base property
      3. The start() method
      4. The stop() method
    5. The full code for MainActivity.kt
    6. What happens when you run the app
    7. Test Drive
      1. But there’s just one problem…
    8. What happens when the app runs
    9. Rotating the screen changes the device configuration
    10. An activity’s states
    11. The activity lifecycle: from create to destroy
    12. Your activity inherits the lifecycle methods
    13. Save the current state in a Bundle
      1. Add values using put methods
      2. Retrieve values using get methods
    14. Save the state using onSaveInstanceState()
      1. Restore the state using onCreate()
    15. The updated MainActivity.kt code
    16. What happens when you run the app
    17. Test Drive
    18. Activity Magnets
    19. Activity Magnets Solution
    20. There’s more to an activity’s life than create and destroy
      1. Start, stop, and restart
    21. The visible lifecycle
    22. We need to implement two more lifecycle methods
      1. Implement onStop() to pause the stopwatch
    23. Restart the stopwatch when the app becomes visible
    24. The updated MainActivity.kt code
    25. What happens when you run the app
    26. Test Drive
    27. What if an activity is only partially visible?
    28. The foreground lifecycle
    29. Pause the stopwatch if the activity’s paused
    30. The complete MainActivity.kt code
    31. What happens when you run the app
    32. Test Drive
    33. Your handy guide to the activity lifecycle methods
    34. BE the Activity
    35. BE the Activity Solution
    36. Your Android Toolbox
  9. 6. Fragments and Navigation: Finding Your Way
    1. Most apps need more than one screen
    2. Each screen is a fragment
      1. WelcomeFragment
      2. MessageFragment
      3. EncryptFragment
    3. Navigate between screens using the Navigation component
    4. Here’s what we’re going to do
    5. Create a new project
      1. Add some String resources
    6. Add WelcomeFragment to the project
    7. What fragment code looks like
      1. Fragment code looks similar to activity code
    8. The fragment’s onCreateView() method
      1. Inflate the fragment’s layout, and return it
    9. Fragment layout code looks like activity layout code
    10. You display a fragment in a FragmentContainerView
    11. Update the activity_main.xml code
      1. The full code for MainActivity.kt
    12. What the code does
    13. Test Drive
    14. Pool Puzzle
    15. Pool Puzzle Solution
    16. Create MessageFragment
    17. Update MessageFragment’s layout
    18. Update MessageFragment.kt
    19. Use the Navigation component to navigate between fragments
    20. Use Gradle to add the Navigation component to your project
      1. Add a version number to the project build.gradle file
      2. Add a dependency to the app build.gradle file
    21. Create a navigation graph
    22. Add fragments to the navigation graph
    23. Connect fragments using an action
      1. Every action needs a unique ID
    24. Navigation graphs are XML resources
    25. Add a navigation host to the layout using a FragmentContainerView
    26. Add a NavHostFragment to activity_main.xml
      1. We need to navigate between the fragments
    27. Add an OnClickListener to the button
      1. Fragment OnClickListener code is a little different
    28. Get a navigation controller
      1. Tell it where to navigate with an action
    29. The full code for WelcomeFragment.kt
    30. What happens when the app runs
    31. Test Drive
    32. Your Android Toolbox
  10. 7. Safe Args: Passing Information
    1. The Secret Message app navigates between fragments
    2. MessageFragment needs to pass the message to a new fragment
    3. Here’s what we’re going to do
    4. Create EncryptFragment…
      1. …and update its layout
    5. Update EncryptFragment.kt
    6. Add EncryptFragment to the navigation graph
    7. The updated nav_graph.xml code
    8. MessageFragment needs to navigate to EncryptFragment
    9. Test Drive
    10. Add Safe Args to the build.gradle files
      1. Add a classpath to the project build.gradle file
      2. Add the plug-in to the app build.gradle file
    11. EncryptFragment needs to accept a String argument
    12. The updated nav_graph.xml code
    13. MessageFragment needs to pass a message to EncryptFragment
      1. You can pass arguments to navigation actions
    14. Safe Args generates Directions classes
      1. Use a Directions class to add arguments to actions
    15. Update the MessageFragment.kt code
    16. EncryptFragment needs to get the argument’s value
      1. Use an Args class to retrieve arguments
      2. We need to encrypt the message
    17. The full code for EncryptFragment.kt
    18. What happens when the app runs
    19. Test Drive
    20. Navigation Magnets
    21. Navigation Magnets Solution
    22. What if the user wants to go back?
    23. We could change the back behavior
    24. Welcome to the back stack
      1. A back stack scenario
    25. Use the navigation graph to pop fragments off the back stack
    26. The updated nav_graph.xml code
    27. Test Drive
    28. BE the Safe Args Plug-in
    29. BE the Safe Args Plug-in Solution
    30. Your Android Toolbox
  11. 8. Navigation ui: Going Places
    1. Different apps, different structures
    2. Android includes navigation UI components
      1. An app bar
      2. The bottom navigation bar
      3. A navigation drawer
    3. How the CatChat app will work
    4. Here’s what we’re going to do
    5. Create a new project
      1. A default app bar gets added…
      2. …by applying a theme
    6. The CatChat app will use a Material theme
      1. The app build.gradle file needs a Material library dependency
    7. Apply a theme in AndroidManifest.xml
    8. Define styles in style resource files
    9. Styles can override theme colors
      1. Color resource files define a set of colors
    10. Replace the default app bar with a toolbar
      1. Remove the default app bar using a theme
    11. A toolbar is a type of View
    12. Add the toolbar to activity_main.xml
      1. Where did the app name go?
    13. Set the toolbar as MainActivity’s app bar
    14. Test Drive
    15. Let’s use the toolbar to navigate
      1. How toolbar navigation will work
    16. Create InboxFragment
    17. Create HelpFragment
    18. We’ll use the Navigation component to navigate to HelpFragment
      1. Add a version number to the project build.gradle file
      2. Add dependencies to the app build.gradle file
    19. Add the fragments to a navigation graph
    20. Add a navigation host to activity_main.xml
    21. Specify items in the toolbar with a menu resource file
    22. Let’s add a Help item to the menu
    23. onCreateOptionsMenu() adds menu items to the toolbar
    24. Respond to menu item clicks with onOptionsItemSelected()
    25. We need to configure the toolbar
    26. Configure the toolbar using an AppBarConfiguration
    27. The full code for MainActivity.kt
    28. What happens when the app runs
    29. Test Drive
    30. BE the Menu
    31. BE the Menu Solution
    32. Most types of UI navigation work with the Navigation component
      1. How the bottom navigation bar will work
    33. Create SentItemsFragment
    34. Add SentItemsFragment to the Navigation graph
    35. The bottom navigation bar needs a new menu resource file
    36. A bottom navigation bar is a type of View
    37. The full code for activity_main.xml
    38. Link the bottom navigation bar to the navigation controller
    39. The updated code for MainActivity.kt
    40. Test Drive
    41. A navigation drawer lets you display many navigation items
    42. Let’s replace the bottom navigation bar with a navigation drawer
    43. Navigation drawers deconstructed
    44. The drawer gets its items from a menu
    45. Add the support section…
      1. …as a separate submenu
    46. Highlight the selected item with groups
    47. The full code for menu_main.xml
    48. Create the navigation drawer’s header
      1. Add the image file…
      2. …and update the nav_header.xml code
    49. How to create a navigation drawer
    50. The full code for activity_main.xml
      1. We’ve added a navigation drawer to the layout
    51. Configure the toolbar’s drawer icon…
      1. …and link the drawer to the navigation controller
    52. The full code for MainActivity.kt
    53. Test Drive
    54. Layout Magnets
    55. Layout Magnets Solution
    56. Your Android Toolbox
  12. 9. Material Views: A Material World
    1. Material is used throughout Androidville
    2. The Bits and Pizzas app
    3. Here’s what we’ll do
    4. Create the Bits and Pizzas project
      1. Add a Material library dependency to the app build.gradle file
    5. Create OrderFragment
    6. Display OrderFragment in MainActivity’s layout
    7. Replace the default app bar with a toolbar
    8. Fragments don’t have a setSupportActionBar() method
    9. We’ve added the toolbar…now what?
      1. We want the toolbar to respond to scrolls
    10. The coordinator layout coordinates animations between views
    11. The app bar layout enables toolbar animation
    12. Tell the toolbar how to respond to scroll events
    13. A nested scroll view makes layout content scrollable
    14. The full code for fragment_order.xml
    15. Test Drive
    16. BE the Layout
    17. BE the Layout Solution
    18. Let’s create a collapsing toolbar
    19. How to create a plain collapsing toolbar
    20. How to add an image to a collapsing toolbar
    21. Add a restaurant image drawable
      1. The full code for fragment_order.xml
    22. Test Drive
    23. Layout Magnets
    24. We need to build OrderFragment’s main content
    25. Choose a pizza type using a radio button
    26. Radio buttons are a type of compound button
    27. A chip is a type of flexible compound button
    28. Add multiple chips to a chip group
      1. Use isChecked to find out if a chip is checked
    29. A FAB is a floating action button
    30. You can anchor a FAB to a collapsing toolbar
    31. We need to build OrderFragment’s layout
    32. The full code for fragment_order.xml
    33. Test Drive
    34. Let’s make the FAB respond to clicks
    35. Add an OnClickListener to the FAB
      1. Find out if a pizza type has been selected
    36. A toast is a simple pop-up message
      1. Add a toast to the FAB OnClickListener
    37. Display the pizza order in a snackbar
      1. Snackbars can have actions
    38. The snackbar code for the pizza order
    39. The full code for OrderFragment.kt
    40. Test Drive
    41. Pool Puzzle
    42. Pool Puzzle Solution
    43. Layout Magnets Solution
    44. Your Android Toolbox
  13. 10. View Binding: Bound Together
    1. Behind the scenes of findViewById()
      1. findViewById() looks for a view in the view hierarchy
    2. There’s a downside to findViewById()
    3. View binding to the rescue
      1. View binding is safer and more efficient than findViewById()
    4. Here’s how we’ll use view binding
    5. The Stopwatch app revisited
    6. Enable view binding in the app build.gradle file
      1. Enabling view binding generates code for each layout
    7. How to add view binding to an activity
    8. Use the binding property to interact with views
      1. The layout code
      2. The activity code
    9. The full code for MainActivity.kt
    10. What the code does
    11. Test Drive
    12. Activity Magnets
    13. Fragments can use view binding too (but the code’s a little different)
      1. Let’s implement view binding in the Bits and Pizzas app
    14. Enable view binding for Bits and Pizzas
      1. Binding classes are generated for every layout
    15. Fragment view binding code is a little different
      1. Activities can access views from onCreate()
    16. Fragments can access views from onCreateView() to onDestroyView()
    17. What fragment view binding code looks like
    18. _binding refers to the binding object…
      1. ...and the binding property provides non-null access to it
    19. The full code for OrderFragment.kt
    20. Test Drive
    21. Activity Magnets Solution
    22. Your Android Toolbox
  14. 11. View Models: Model Behavior
    1. Configuration changes revisited
      1. There are other problems, too
    2. Introducing the view model
      1. Why use a view model?
    3. What the guessing game will do
    4. How the app will be structured
    5. Here’s what we’re going to do
      1. Create the Guessing Game project
    6. Update the project build.gradle file…
      1. …and update the app build.gradle file too
    7. The Guessing Game app has two fragments
      1. Create GameFragment…
      2. …and then create ResultFragment
    8. How navigation should work
      1. Create the navigation graph
    9. Update the navigation graph
    10. Display the current fragment in MainActivity’s layout
    11. Update GameFragment’s layout
      1. What GameFragment needs to do
    12. The GameFragment.kt code
    13. Update ResultFragment’s layout
      1. We also need to update ResultFragment.kt
    14. The ResultFragment.kt code
    15. What happens when the app runs
    16. Test Drive
    17. The game loses state when the screen rotates
    18. A view model holds business logic
    19. Add a view model dependency to the app build.gradle file…
      1. …and create a view model
    20. The full code for GameViewModel.kt
    21. Create a GameViewModel object
      1. Use a ViewModelProvider to create view models
    22. The updated code for GameFragment.kt
    23. What happens when the app runs
    24. Test Drive
    25. BE the View Model
    26. We’ve added a view model for GameFragment
      1. ResultFragment needs a view model too
    27. ResultViewModel needs to hold the result
      1. We need to link ResultViewModel to ResultFragment
    28. A view model factory creates view models
    29. Create the ResultViewModelFactory class
    30. Use the factory to create the view model
    31. The updated code for ResultFragment.kt
    32. What happens when the app runs
    33. Test Drive
    34. View Model Magnets
    35. View Model Magnets Solution
    36. BE the View Model Solution
    37. Your Android Toolbox
  15. 12. Live Data: Leaping into Action
    1. The Guessing Game app revisited
    2. The fragments decide when to update views
      1. Let the view model say when a value’s changed
    3. Here’s what we’re going to do
      1. Add a live data dependency to the app build.gradle file
    4. GameViewModel and GameFragment need to use live data
      1. Which view model properties should use live data?
    5. Live data objects use a value property
      1. The value property can be null
    6. The full code for GameViewModel.kt
    7. The fragment observes the view model properties and reacts to changes.
    8. The full code for GameFragment.kt
    9. What happens when the app runs
    10. Test Drive
    11. Fragments can update GameViewModel’s properties
      1. Keep your privates private
    12. The full code for GameViewModel.kt
    13. What happens when the app runs
    14. Test Drive
    15. GameFragment still includes game logic
      1. Make GameViewModel decide when the game is over
    16. Pool Puzzle
    17. Pool Puzzle Solution
    18. The full code for GameViewModel.kt
    19. Make GameFragment observe the new property
    20. What happens when the app runs
    21. Fragment Magnets
    22. Fragment Magnets Solution
    23. Test Drive
    24. Your Android Toolbox
  16. 13. Data Binding: Building Smart Layouts
    1. Back to the Guessing Game app
    2. The fragments update the views in their layouts
      1. We can make the views update themselves instead
    3. Here’s what we’re going to do
    4. Enable data binding in the app build.gradle file
    5. ResultFragment updates the text in its layout
      1. How we’ll implement data binding
    6. 1. Add <layout> and <data> elements
    7. 2. Set the layout’s data binding variable
    8. 3. Use the layout’s data binding variable to access the view model
    9. The full code for fragment_result.xml
    10. The full code for ResultFragment.kt
    11. What happens when the app runs
    12. Test Drive
    13. Layout Magnets
    14. Layout Magnets Solution
    15. GameFragment can use data binding too
    16. Add <layout> and <data> elements to fragment_game.xml
    17. Use the data binding variable to set the layout’s text
      1. Two of the views need to show extra text
    18. String resources revisited
      1. String resources can have arguments
      2. Add two new String resources to strings.xml
    19. The layout can pass parameters to String resources
    20. The full code for fragment_game.xml
    21. We need to set the gameViewModel variable
      1. We also need to make the data binding use live data
    22. The full code for GameFragment.kt
    23. What happens when the app runs
    24. Test Drive
    25. You can use data binding to call methods
    26. Add finishGame() to GameViewModel.kt
    27. Use data binding to make a button call a method when clicked
    28. The full code for fragment_game.xml
    29. What happens when the app runs
    30. Test Drive
    31. Pool Puzzle
    32. Fireside Chats
    33. We can switch off view binding
      1. Switch off view binding in the app’s build.gradle file
    34. Test Drive
    35. Binding-Cross
    36. Binding-Cross Solution
    37. Pool Puzzle Solution
    38. Your Android Toolbox
  17. 14. Room Databases: Room with a View
    1. Most apps need to store data
      1. Apps can persist data using a database
    2. How the app will be structured
    3. Room is a database library that sits on top of SQLite
      1. Room apps are usually structured using MVVM
    4. Here’s what we’re going to do
      1. Create the Tasks project
    5. Add a variable to the project build.gradle file…
      1. …and update the app build.gradle file, too
    6. Create TasksFragment
      1. Update TasksFragment.kt
    7. Update fragment_tasks.xml
    8. Display TasksFragment in MainActivity’s layout…
      1. …and check the MainActivity.kt code
    9. How Room databases are created
      1. 1. A database class
      2. 2. Data classes for the tables
      3. 3. Interfaces for data access
    10. We’ll store tasks data in a table
      1. You define tables with an annotated data class
      2. Create the Task data class
    11. Specify a table name with @Entity
      1. Specify the primary key with @PrimaryKey
      2. Specify column names with @ColumnInfo
    12. The full code for Task.kt
    13. Use an interface to specify data operations
      1. Create the TaskDao interface
      2. Use @Dao to mark the interface for data access
    14. Use @Insert to insert a record
      1. Use @Update to update a record
    15. Use @Delete to delete a record
      1. Use @Query for everything else
    16. The full code for TaskDao.kt
    17. Create a TaskDatabase abstract class
      1. Annotate the class with @Database
    18. Add properties for any DAO interfaces
      1. Create and return an instance of the database
    19. The full code for TaskDatabase.kt
    20. MVVM revisited
      1. We’ve completed all the Model code…
      2. …so let’s move on to the ViewModel
    21. Create TasksViewModel
    22. Database operations can run in slooooow-moooo
      1. We’ll use coroutines to run data access code in the background
    23. 1. Mark TaskDao’s methods with suspend
    24. 2. Launch the insert() method in the background
    25. TasksViewModel needs a view model factory
      1. Create TasksViewModelFactory
    26. TasksViewModelFactory needs a TaskDao
      1. The TaskDatabase code has a TaskDao property
    27. The updated code for TasksFragment.kt
    28. TasksFragment can use data binding
    29. We’ll use data binding to insert a record
      1. Set TasksViewModel’s newTaskName property
      2. Call TasksViewModel’s addTask() method
    30. The full fragment_tasks.xml code
    31. The full TasksFragment.kt code
    32. What happens when the code runs
    33. Test Drive
    34. TasksFragment needs to display records
    35. Use getAll() to get all tasks from the database
    36. A LiveData<List<Task>> is a more complex type
      1. Use Transformations.map() to transform live data objects
    37. Let’s update the TasksViewModel code
    38. We’ll bind the tasksString property to the layout’s text view
      1. We need to make the layout respond to live data updates
    39. The full TasksFragment.kt code
    40. What happens when the code runs
    41. Test Drive
    42. Mixed Messages
    43. Mixed Messages Solution
    44. Your Android Toolbox
  18. 15. Recycler Views: Reduce, Reuse, Recycle
    1. What the Tasks app currently looks like
    2. We can turn the list into a recycler view
    3. Why use a recycler view?
      1. Recycler views get their data from an adapter
    4. Here’s what we’re going to do
      1. Add a recycler view dependency to the app build.gradle file
    5. Tell the recycler view how to display each item…
      1. …by defining a layout file
    6. The adapter adds data to the recycler view
      1. Create the adapter file
    7. Tell the adapter what data it should work with
      1. Override the getItemCount() method
    8. Define the adapter’s view holder
    9. Override the onCreateViewHolder() method
    10. Add data to the layout’s view
    11. The full code for TaskItemAdapter.kt
    12. The adapter code is complete
    13. We need to display the recycler view
      1. How to add a recycler view to a layout
    14. The full code for fragment_tasks.xml
      1. Tell the recycler view to use the adapter
    15. The updated code for TasksFragment.kt
    16. We’ve added a recycler view to TasksFragment’s layout
      1. We’ll make TasksFragment add task data to TaskItemAdapter
    17. Update the TasksViewModel.kt code
    18. TasksFragment needs to update TaskItemAdapter’s data property
    19. The full code for TasksFragment.kt
      1. We’ve finished writing all the recycler view code
    20. What happens when the code runs
    21. Test Drive
    22. Adapter Magnets
    23. Adapter Magnets Solution
    24. Recycler views are very flexible
    25. Recycler view 2.0
      1. Add a card view dependency to the app build.gradle file
    26. How to create a card view
    27. The full code for task_item.xml
    28. The adapter’s view holder needs to work with the new layout code
    29. The full code for TaskItemAdapter.kt
    30. What the recycler view looks like so far
    31. The layout manager gallery
      1. Display items in a horizontal row
      2. Use GridLayoutManager to display items in a grid
      3. Arrange items in a staggered grid
    32. Update fragment_tasks.xml to arrange items in a grid
    33. What happens when the code runs
    34. Test Drive
    35. Pool Puzzle
    36. Pool Puzzle Solution
    37. Your Android Toolbox
  19. 16. Diffutil and Data Binding: Life in the Fast Lane
    1. The recycler view displays task data correctly…
      1. …but when the data’s updated, the recycler view jumps
    2. The Tasks app revisited
    3. How the recycler view gets its data
    4. The data property’s setter calls notifyDataSetChanged()
      1. notifyDataSetChanged() redraws the entire list
    5. Tell the recycler view what needs to change
      1. DiffUtil works out differences between lists
    6. Here’s what we’re going to do
    7. We need to implement DiffUtil.ItemCallback
      1. Create TaskDiffItemCallback.kt
    8. A ListAdapter accepts a DiffUtil.ItemCallback argument
      1. We can simplify the rest of TaskItemAdapter’s code
    9. The updated code for TaskItemAdapter.kt
    10. Populate the ListAdapter’s list…
      1. …using submitList()
    11. The updated code for TasksFragment.kt
    12. What happens when the code runs
    13. Test Drive
    14. BE the ListAdapter
    15. Recycler views can use data binding
      1. How we’ll implement data binding
    16. Add a data binding variable to task_item.xml
    17. The layout gets inflated in the adapter’s view holder code
    18. Use the binding class to inflate the layout
      1. Set the layout’s data binding variable to a Task
    19. The full code for TaskItemAdapter.kt
      1. Use data binding to set the layout’s views
    20. The full code for task_item.xml
    21. What happens when the code runs
    22. Test Drive
    23. Adapter Magnets
    24. Adapter Magnets Solution
    25. BE the ListAdapter Solution
    26. Your Android Toolbox
  20. 17. Recycler View Navigation: Pick a Card
    1. Recycler views can be used for navigation
    2. How the Tasks app is currently structured
    3. We’re going to make the recycler view navigate to a new fragment
    4. Here’s what we’re going to do
    5. Make each item clickable
    6. Where should we create the toast?
      1. We’ll make TasksFragment pass the toast code in a lambda
    7. How the code will work
    8. The full code for TaskItemAdapter.kt
    9. We’ll pass a lambda to TaskItemAdapter
    10. The full code for TasksFragment.kt
    11. What happens when the code runs
    12. Test Drive
    13. Adapter Magnets
    14. Adapter Magnets Solution
    15. We want to use the recycler view to navigate to a new fragment
    16. Update the project build.gradle file…
      1. …then update the app build.gradle file
    17. Create EditTaskFragment…
      1. …and create a navigation graph
    18. Update the navigation graph
    19. Add a NavHostFragment to MainActivity’s layout
    20. Make TasksFragment navigate to EditTaskFragment
    21. Add a new property to TasksViewModel
      1. Add methods to update the new property
    22. The full code for TasksViewModel.kt
    23. Make TasksFragment navigate to EditTaskFragment
      1. Call onTaskClicked() when the user clicks a task
      2. Navigate to EditTaskFragment when navigateToTask gets updated
    24. The full code for TasksFragment.kt
    25. Make EditTaskFragment display the task ID
      1. We also need to update EditTaskFragment.kt
    26. The full code for EditTaskFragment.kt
    27. What happens when the code runs
    28. Test Drive
    29. We want to use EditTaskFragment to update task records
    30. Use TaskDao to interact with database records
      1. We’ll create a view model to access TaskDao’s methods
    31. Create EditTaskViewModel
      1. The view model needs to get a task record…
      2. …and include methods to update and delete the task
    32. EditTaskViewModel will tell EditTaskFragment when to navigate
    33. The full code for EditTaskViewModel.kt
    34. EditTaskViewModel needs a view model factory
      1. Create EditTaskViewModelFactory
    35. fragment_edit_task.xml needs to display the task
      1. We need to update EditTaskFragment.kt too
    36. The full code for EditTaskFragment.kt
    37. What happens when the code runs
    38. Test Drive
    39. Your Android Toolbox
  21. 18. Jetpack Compose: Compose Yourself
    1. UI components don’t have to be Views
      1. We’re going to build a Compose app
    2. Here’s what we’re going to do
    3. Create a new Compose project
    4. Configure the project
    5. Compose projects have no layout files
    6. What Compose activity code looks like
      1. Compose activities extend ComponentActivity
    7. Use a Text composable to display text
    8. Use composables in composable functions
      1. Call the Hello composable from setContent()
    9. Most UIs have multiple composables
      1. You can arrange composables using Row or Column
    10. The full code for MainActivity.kt
    11. Test Drive
    12. You can preview composable functions
      1. Add MainActivityPreview to MainActivity.kt
    13. Preview composables with the Design or Split option
    14. Pool Puzzle
    15. Pool Puzzle Solution
    16. Let’s make the app convert temperatures
    17. Add a MainActivityContent composable function
    18. Display the header image…
      1. …with an Image composable
    19. Add an Image to MainActivity.kt
    20. Let’s display the temperature text
    21. Test Drive
    22. Use a Button composable to add a button
      1. Let’s write a ConvertButton composable function
    23. We need to pass a lambda to ConvertButton
    24. We need to change the value of TemperatureText’s argument
      1. Use remember to store celsius in memory
    25. The full code for MainActivity.kt
    26. What happens when the app runs
    27. Test Drive
    28. Compose Magnets
    29. Compose Magnets Solution
    30. Let the user enter a temperature
      1. We’ll use a TextField composable
    31. Add a TextField to a composable function
      1. Call the function in MainActivityContent
    32. The full code for MainActivity.kt
    33. What happens when the app runs
    34. Test Drive
    35. We’ll tweak the app’s appearance
    36. Add padding to the Column composable
    37. You can center composables in Columns or Rows
      1. Center all of a Column’s content
      2. Center the content of a single Row
    38. Applying themes: revisited
      1. How to apply a theme to composables
    39. Android Studio includes extra theme code
      1. How to apply the theme
    40. The full code for MainActivity.kt
    41. Test Drive
    42. BE Compose
    43. BE Compose Solution
    44. Your Android Toolbox
  22. 19. Integrating Compose with views: Perfect Harmony
    1. You can add composables to View-based UIs
      1. The Guessing Game app revisited
    2. The Guessing Game app structure
    3. Here’s what we’re going to do
    4. Update the project build.gradle file…
      1. …and update the app build.gradle file, too
    5. We’ll replace ResultFragment’s views with composables
    6. A ComposeView lets you add composables to a layout
      1. We’ll add a ComposeView to fragment_result.xml
    7. Add composables using Kotlin code
    8. Add a composable function for the fragment’s content
    9. Reproduce the Start New Game button
    10. Reproduce ResultFragment’s TextView
    11. The updated code for ResultFragment.kt
    12. Test Drive
    13. We need to remove ResultFragment’s views…
      1. …and update ResultFragment.kt
    14. onCreateView() returns the UI’s root view
      1. Return a ComposeView for Compose UIs
    15. The full code for ResultFragment.kt
      1. We can delete fragment_result.xml
    16. What happens when the app runs
    17. Test Drive
    18. Pool Puzzle
    19. Next we’ll make GameFragment use composables too
    20. We’ll add a ComposeView to fragment_game.xml
    21. Add a composable function for GameFragment’s content
    22. Reproduce the Finish Game button
    23. Reproduce the EditText with a TextField
    24. Reproduce the Guess button
    25. The updated code for GameFragment.kt
    26. Test Drive
    27. We’ll display the incorrect guesses in a Text composable
      1. Respond to LiveData using observeAsState()
    28. Create an IncorrectGuessesText composable function
      1. Run IncorrectGuessesText from GameFragmentContent
    29. Compose Magnets
    30. Compose Magnets Solution
    31. The updated code for GameFragment.kt
    32. Test Drive
    33. Remove views from GameFragment.kt
    34. Delete fragment_game.xml
      1. We can also switch off data binding
    35. Test Drive
    36. Pool Puzzle Solution
    37. Your Android Toolbox
    38. Leaving town
    39. It’s been great having you here in Androidville
  23. A. Leftovers: The Top Ten Things (we didn’t cover)
    1. 1. Sharing data with other apps
      1. Sharing data with Android’s intent resolver
      2. Sharing data with the Android Sharesheet
    2. 2. WorkManager
      1. Use WorkManager to schedule deferrable tasks
    3. 3. Dialogs and notifications
      1. Use dialogs to prompt the user for a decision
      2. Notifications appear outside the app’s UI
    4. 4. Automated testing
      1. Unit tests
      2. Instrumented tests
      3. An instrumented test example
    5. 5. Supporting different screen sizes
      1. Provide alternate layouts
      2. Use a SlidingPaneLayout
    6. 6. More Compose features
      1. The Compose ViewModel library
      2. Compose constraint layout library
      3. The Compose Navigation component
    7. 7. Retrofit
    8. 8. Android Game Development Kit
    9. 9. CameraX
    10. 10. Publishing your app
      1. Preparing your app for release
      2. Releasing your app
  24. Index

Product information

  • Title: Head First Android Development, 3rd Edition
  • Author(s): Dawn Griffiths, David Griffiths
  • Release date: November 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492076476