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
Table of contents
- Authors of Head First Android Development
- Table of Contents (the real thing)
- How to use this book: Intro
-
1. Getting Started: Diving In
- Welcome to Androidville
- Activities and layouts form the backbone of your app
- Hereâs what weâre going to do
- Android Studio: your development environment
- Install Android Studio
- Letâs build a basic app
- How to build the app
- Youâve created your first Android project
- Dissecting your new project
- Introducing the key files in your project
- Edit code with the Android Studio editors
- The story so far
- How to run the app on a physical device
- How to run the app on a virtual device
- Create an Android Virtual Device (AVD)
- Compile, package, deploy, run
- Test Drive
- What just happened?
- Letâs refine the app
- Whatâs in the layout?
- activity_main.xml has two elements
- Update the text displayed in the layout
- What the code does
- Test Drive
- Your Android Toolbox
-
2. Building Interactive Apps: Apps That Do Something
- Letâs build a Beer Adviser app
- Hereâs what weâre going to do
- Create the project
- Weâve created a default activity and layout
- A closer look at the design editor
- Add a button using the design editor
- activity_main.xml has a new button
- A closer look at the layout code
- Letâs update the layout XML
- The XML changes are reflected in the design editor
- Test Drive
- There are warnings in the layoutâ¦
- Put text in a String resource file
- Extract the String resource
- activity_main.xml uses the String resource
- Add and use a new String resource
- Test Drive
- Add values to the spinner
- Add the string-array to strings.xml
- The full code for activity_main.xml
- Test Drive
- We need to make the app interactive
- What the MainActivity code looks like
- A button can listen for on-click eventsâ¦
- Get a reference to the buttonâ¦
- Pass a lambda to the setOnClickListener method
- How to edit a text viewâs text
- Pool Puzzle
- Pool Puzzle Solution
- The updated code for MainActivity.kt
- What happens when you run the code
- Test Drive
- Add the getBeers() method
- Activity Magnets
- Activity Magnets Solution
- The full code for MainActivity.kt
- What happens when you run the code
- Test Drive
- Your Android Toolbox
-
3. Layouts: Being a layout
- It all starts with a layout
- Android has different types of layout
- Letâs build a linear layout
- How to define a linear layout
- Orientation can be vertical or horizontal
- Use padding to add space to the layoutâs edges
- The layout code so far
- An edit text lets you enter text
- Add views to the layout XML
- Make a view streeeeetch by adding weight
- How to add weight to one view
- How to add weight to multiple views
- The gravity attribute controls the position of a viewâs contents
- Values you can use with the android:gravity attribute
- Layout Magnets
- Layout Magnets Solution
- The story so far
- layout-gravity controls the position of a view within a layout
- More values you can use with the android:layout-gravity attribute
- Use margins to add space between views
- The full linear layout code
- Test Drive
- BE the Layout
- BE the Layout Solution
- Your activity code tells Android which layout it uses
- Layout inflation: an example
- A frame layout stacks its views
- Add an image to your project
- A frame layout stacks views in the order they appear in the layout XML
- Test Drive
- All layouts are a type of ViewGroupâ¦
- A scroll view inserts a vertical scrollbar
- How to add a scroll view
- Test Drive
- Your Android Toolbox
-
4. Constraint Layouts: Draw Up a Blueprint
- Nested layouts revisited
- Nesting layouts comes at a price
- Introducing the constraint layout
- Constraint layouts are part of Android Jetpack
- Hereâs what weâre going to do
- Use Gradle to include Jetpack libraries
- Letâs add a constraint layout to activity_main.xml
- Add a button to the blueprint
- Position views using constraints
- Add a vertical constraint too
- Use opposing constraints to center views
- You can delete constraints you no longer need
- Remove constraints with the constraint widget
- Changes to the blueprint appear in the XML
- Views can have bias
- Test Drive
- You can change a viewâs size
- BE the Constraint
- BE the Constraint Solution
- Most layouts need multiple views
- You can connect views to other views
- You can align views too
- Align views using guidelines
- Guidelines have a fixed position
- Create a movable barrier
- Add a horizontal barrier
- Constrain a button under the barrier
- The full code for activity_main.xml
- Test Drive
- Use a chain to control a linear group of views
- The chain will use three buttons
- Create the horizontal chain
- There are different styles of chain
- Test Drive
- A flow is like a multi-line chain
- How to add a flow
- You can control the flowâs appearance
- The full code for activity_main.xml
- Test Drive
- Your Android Toolbox
-
5. The Activity Lifecycle: Being an Activity
- How do activities really work?
- Create a new project
- The full code for activity_main.xml
- The activity code controls the stopwatch
- The full code for MainActivity.kt
- What happens when you run the app
- Test Drive
- What happens when the app runs
- Rotating the screen changes the device configuration
- An activityâs states
- The activity lifecycle: from create to destroy
- Your activity inherits the lifecycle methods
- Save the current state in a Bundle
- Save the state using onSaveInstanceState()
- The updated MainActivity.kt code
- What happens when you run the app
- Test Drive
- Activity Magnets
- Activity Magnets Solution
- Thereâs more to an activityâs life than create and destroy
- The visible lifecycle
- We need to implement two more lifecycle methods
- Restart the stopwatch when the app becomes visible
- The updated MainActivity.kt code
- What happens when you run the app
- Test Drive
- What if an activity is only partially visible?
- The foreground lifecycle
- Pause the stopwatch if the activityâs paused
- The complete MainActivity.kt code
- What happens when you run the app
- Test Drive
- Your handy guide to the activity lifecycle methods
- BE the Activity
- BE the Activity Solution
- Your Android Toolbox
-
6. Fragments and Navigation: Finding Your Way
- Most apps need more than one screen
- Each screen is a fragment
- Navigate between screens using the Navigation component
- Hereâs what weâre going to do
- Create a new project
- Add WelcomeFragment to the project
- What fragment code looks like
- The fragmentâs onCreateView() method
- Fragment layout code looks like activity layout code
- You display a fragment in a FragmentContainerView
- Update the activity_main.xml code
- What the code does
- Test Drive
- Pool Puzzle
- Pool Puzzle Solution
- Create MessageFragment
- Update MessageFragmentâs layout
- Update MessageFragment.kt
- Use the Navigation component to navigate between fragments
- Use Gradle to add the Navigation component to your project
- Create a navigation graph
- Add fragments to the navigation graph
- Connect fragments using an action
- Navigation graphs are XML resources
- Add a navigation host to the layout using a FragmentContainerView
- Add a NavHostFragment to activity_main.xml
- Add an OnClickListener to the button
- Get a navigation controller
- The full code for WelcomeFragment.kt
- What happens when the app runs
- Test Drive
- Your Android Toolbox
-
7. Safe Args: Passing Information
- The Secret Message app navigates between fragments
- MessageFragment needs to pass the message to a new fragment
- Hereâs what weâre going to do
- Create EncryptFragmentâ¦
- Update EncryptFragment.kt
- Add EncryptFragment to the navigation graph
- The updated nav_graph.xml code
- MessageFragment needs to navigate to EncryptFragment
- Test Drive
- Add Safe Args to the build.gradle files
- EncryptFragment needs to accept a String argument
- The updated nav_graph.xml code
- MessageFragment needs to pass a message to EncryptFragment
- Safe Args generates Directions classes
- Update the MessageFragment.kt code
- EncryptFragment needs to get the argumentâs value
- The full code for EncryptFragment.kt
- What happens when the app runs
- Test Drive
- Navigation Magnets
- Navigation Magnets Solution
- What if the user wants to go back?
- We could change the back behavior
- Welcome to the back stack
- Use the navigation graph to pop fragments off the back stack
- The updated nav_graph.xml code
- Test Drive
- BE the Safe Args Plug-in
- BE the Safe Args Plug-in Solution
- Your Android Toolbox
-
8. Navigation ui: Going Places
- Different apps, different structures
- Android includes navigation UI components
- How the CatChat app will work
- Hereâs what weâre going to do
- Create a new project
- The CatChat app will use a Material theme
- Apply a theme in AndroidManifest.xml
- Define styles in style resource files
- Styles can override theme colors
- Replace the default app bar with a toolbar
- A toolbar is a type of View
- Add the toolbar to activity_main.xml
- Set the toolbar as MainActivityâs app bar
- Test Drive
- Letâs use the toolbar to navigate
- Create InboxFragment
- Create HelpFragment
- Weâll use the Navigation component to navigate to HelpFragment
- Add the fragments to a navigation graph
- Add a navigation host to activity_main.xml
- Specify items in the toolbar with a menu resource file
- Letâs add a Help item to the menu
- onCreateOptionsMenu() adds menu items to the toolbar
- Respond to menu item clicks with onOptionsItemSelected()
- We need to configure the toolbar
- Configure the toolbar using an AppBarConfiguration
- The full code for MainActivity.kt
- What happens when the app runs
- Test Drive
- BE the Menu
- BE the Menu Solution
- Most types of UI navigation work with the Navigation component
- Create SentItemsFragment
- Add SentItemsFragment to the Navigation graph
- The bottom navigation bar needs a new menu resource file
- A bottom navigation bar is a type of View
- The full code for activity_main.xml
- Link the bottom navigation bar to the navigation controller
- The updated code for MainActivity.kt
- Test Drive
- A navigation drawer lets you display many navigation items
- Letâs replace the bottom navigation bar with a navigation drawer
- Navigation drawers deconstructed
- The drawer gets its items from a menu
- Add the support sectionâ¦
- Highlight the selected item with groups
- The full code for menu_main.xml
- Create the navigation drawerâs header
- How to create a navigation drawer
- The full code for activity_main.xml
- Configure the toolbarâs drawer iconâ¦
- The full code for MainActivity.kt
- Test Drive
- Layout Magnets
- Layout Magnets Solution
- Your Android Toolbox
-
9. Material Views: A Material World
- Material is used throughout Androidville
- The Bits and Pizzas app
- Hereâs what weâll do
- Create the Bits and Pizzas project
- Create OrderFragment
- Display OrderFragment in MainActivityâs layout
- Replace the default app bar with a toolbar
- Fragments donât have a setSupportActionBar() method
- Weâve added the toolbarâ¦now what?
- The coordinator layout coordinates animations between views
- The app bar layout enables toolbar animation
- Tell the toolbar how to respond to scroll events
- A nested scroll view makes layout content scrollable
- The full code for fragment_order.xml
- Test Drive
- BE the Layout
- BE the Layout Solution
- Letâs create a collapsing toolbar
- How to create a plain collapsing toolbar
- How to add an image to a collapsing toolbar
- Add a restaurant image drawable
- Test Drive
- Layout Magnets
- We need to build OrderFragmentâs main content
- Choose a pizza type using a radio button
- Radio buttons are a type of compound button
- A chip is a type of flexible compound button
- Add multiple chips to a chip group
- A FAB is a floating action button
- You can anchor a FAB to a collapsing toolbar
- We need to build OrderFragmentâs layout
- The full code for fragment_order.xml
- Test Drive
- Letâs make the FAB respond to clicks
- Add an OnClickListener to the FAB
- A toast is a simple pop-up message
- Display the pizza order in a snackbar
- The snackbar code for the pizza order
- The full code for OrderFragment.kt
- Test Drive
- Pool Puzzle
- Pool Puzzle Solution
- Layout Magnets Solution
- Your Android Toolbox
-
10. View Binding: Bound Together
- Behind the scenes of findViewById()
- Thereâs a downside to findViewById()
- View binding to the rescue
- Hereâs how weâll use view binding
- The Stopwatch app revisited
- Enable view binding in the app build.gradle file
- How to add view binding to an activity
- Use the binding property to interact with views
- The full code for MainActivity.kt
- What the code does
- Test Drive
- Activity Magnets
- Fragments can use view binding too (but the codeâs a little different)
- Enable view binding for Bits and Pizzas
- Fragment view binding code is a little different
- Fragments can access views from onCreateView() to onDestroyView()
- What fragment view binding code looks like
- _binding refers to the binding objectâ¦
- The full code for OrderFragment.kt
- Test Drive
- Activity Magnets Solution
- Your Android Toolbox
-
11. View Models: Model Behavior
- Configuration changes revisited
- Introducing the view model
- What the guessing game will do
- How the app will be structured
- Hereâs what weâre going to do
- Update the project build.gradle fileâ¦
- The Guessing Game app has two fragments
- How navigation should work
- Update the navigation graph
- Display the current fragment in MainActivityâs layout
- Update GameFragmentâs layout
- The GameFragment.kt code
- Update ResultFragmentâs layout
- The ResultFragment.kt code
- What happens when the app runs
- Test Drive
- The game loses state when the screen rotates
- A view model holds business logic
- Add a view model dependency to the app build.gradle fileâ¦
- The full code for GameViewModel.kt
- Create a GameViewModel object
- The updated code for GameFragment.kt
- What happens when the app runs
- Test Drive
- BE the View Model
- Weâve added a view model for GameFragment
- ResultViewModel needs to hold the result
- A view model factory creates view models
- Create the ResultViewModelFactory class
- Use the factory to create the view model
- The updated code for ResultFragment.kt
- What happens when the app runs
- Test Drive
- View Model Magnets
- View Model Magnets Solution
- BE the View Model Solution
- Your Android Toolbox
-
12. Live Data: Leaping into Action
- The Guessing Game app revisited
- The fragments decide when to update views
- Hereâs what weâre going to do
- GameViewModel and GameFragment need to use live data
- Live data objects use a value property
- The full code for GameViewModel.kt
- The fragment observes the view model properties and reacts to changes.
- The full code for GameFragment.kt
- What happens when the app runs
- Test Drive
- Fragments can update GameViewModelâs properties
- The full code for GameViewModel.kt
- What happens when the app runs
- Test Drive
- GameFragment still includes game logic
- Pool Puzzle
- Pool Puzzle Solution
- The full code for GameViewModel.kt
- Make GameFragment observe the new property
- What happens when the app runs
- Fragment Magnets
- Fragment Magnets Solution
- Test Drive
- Your Android Toolbox
-
13. Data Binding: Building Smart Layouts
- Back to the Guessing Game app
- The fragments update the views in their layouts
- Hereâs what weâre going to do
- Enable data binding in the app build.gradle file
- ResultFragment updates the text in its layout
- 1. Add <layout> and <data> elements
- 2. Set the layoutâs data binding variable
- 3. Use the layoutâs data binding variable to access the view model
- The full code for fragment_result.xml
- The full code for ResultFragment.kt
- What happens when the app runs
- Test Drive
- Layout Magnets
- Layout Magnets Solution
- GameFragment can use data binding too
- Add <layout> and <data> elements to fragment_game.xml
- Use the data binding variable to set the layoutâs text
- String resources revisited
- The layout can pass parameters to String resources
- The full code for fragment_game.xml
- We need to set the gameViewModel variable
- The full code for GameFragment.kt
- What happens when the app runs
- Test Drive
- You can use data binding to call methods
- Add finishGame() to GameViewModel.kt
- Use data binding to make a button call a method when clicked
- The full code for fragment_game.xml
- What happens when the app runs
- Test Drive
- Pool Puzzle
- Fireside Chats
- We can switch off view binding
- Test Drive
- Binding-Cross
- Binding-Cross Solution
- Pool Puzzle Solution
- Your Android Toolbox
-
14. Room Databases: Room with a View
- Most apps need to store data
- How the app will be structured
- Room is a database library that sits on top of SQLite
- Hereâs what weâre going to do
- Add a variable to the project build.gradle fileâ¦
- Create TasksFragment
- Update fragment_tasks.xml
- Display TasksFragment in MainActivityâs layoutâ¦
- How Room databases are created
- Weâll store tasks data in a table
- Specify a table name with @Entity
- The full code for Task.kt
- Use an interface to specify data operations
- Use @Insert to insert a record
- Use @Delete to delete a record
- The full code for TaskDao.kt
- Create a TaskDatabase abstract class
- Add properties for any DAO interfaces
- The full code for TaskDatabase.kt
- MVVM revisited
- Create TasksViewModel
- Database operations can run in slooooow-moooo
- 1. Mark TaskDaoâs methods with suspend
- 2. Launch the insert() method in the background
- TasksViewModel needs a view model factory
- TasksViewModelFactory needs a TaskDao
- The updated code for TasksFragment.kt
- TasksFragment can use data binding
- Weâll use data binding to insert a record
- The full fragment_tasks.xml code
- The full TasksFragment.kt code
- What happens when the code runs
- Test Drive
- TasksFragment needs to display records
- Use getAll() to get all tasks from the database
- A LiveData<List<Task>> is a more complex type
- Letâs update the TasksViewModel code
- Weâll bind the tasksString property to the layoutâs text view
- The full TasksFragment.kt code
- What happens when the code runs
- Test Drive
- Mixed Messages
- Mixed Messages Solution
- Your Android Toolbox
-
15. Recycler Views: Reduce, Reuse, Recycle
- What the Tasks app currently looks like
- We can turn the list into a recycler view
- Why use a recycler view?
- Hereâs what weâre going to do
- Tell the recycler view how to display each itemâ¦
- The adapter adds data to the recycler view
- Tell the adapter what data it should work with
- Define the adapterâs view holder
- Override the onCreateViewHolder() method
- Add data to the layoutâs view
- The full code for TaskItemAdapter.kt
- The adapter code is complete
- We need to display the recycler view
- The full code for fragment_tasks.xml
- The updated code for TasksFragment.kt
- Weâve added a recycler view to TasksFragmentâs layout
- Update the TasksViewModel.kt code
- TasksFragment needs to update TaskItemAdapterâs data property
- The full code for TasksFragment.kt
- What happens when the code runs
- Test Drive
- Adapter Magnets
- Adapter Magnets Solution
- Recycler views are very flexible
- Recycler view 2.0
- How to create a card view
- The full code for task_item.xml
- The adapterâs view holder needs to work with the new layout code
- The full code for TaskItemAdapter.kt
- What the recycler view looks like so far
- The layout manager gallery
- Update fragment_tasks.xml to arrange items in a grid
- What happens when the code runs
- Test Drive
- Pool Puzzle
- Pool Puzzle Solution
- Your Android Toolbox
-
16. Diffutil and Data Binding: Life in the Fast Lane
- The recycler view displays task data correctlyâ¦
- The Tasks app revisited
- How the recycler view gets its data
- The data propertyâs setter calls notifyDataSetChanged()
- Tell the recycler view what needs to change
- Hereâs what weâre going to do
- We need to implement DiffUtil.ItemCallback
- A ListAdapter accepts a DiffUtil.ItemCallback argument
- The updated code for TaskItemAdapter.kt
- Populate the ListAdapterâs listâ¦
- The updated code for TasksFragment.kt
- What happens when the code runs
- Test Drive
- BE the ListAdapter
- Recycler views can use data binding
- Add a data binding variable to task_item.xml
- The layout gets inflated in the adapterâs view holder code
- Use the binding class to inflate the layout
- The full code for TaskItemAdapter.kt
- The full code for task_item.xml
- What happens when the code runs
- Test Drive
- Adapter Magnets
- Adapter Magnets Solution
- BE the ListAdapter Solution
- Your Android Toolbox
-
17. Recycler View Navigation: Pick a Card
- Recycler views can be used for navigation
- How the Tasks app is currently structured
- Weâre going to make the recycler view navigate to a new fragment
- Hereâs what weâre going to do
- Make each item clickable
- Where should we create the toast?
- How the code will work
- The full code for TaskItemAdapter.kt
- Weâll pass a lambda to TaskItemAdapter
- The full code for TasksFragment.kt
- What happens when the code runs
- Test Drive
- Adapter Magnets
- Adapter Magnets Solution
- We want to use the recycler view to navigate to a new fragment
- Update the project build.gradle fileâ¦
- Create EditTaskFragmentâ¦
- Update the navigation graph
- Add a NavHostFragment to MainActivityâs layout
- Make TasksFragment navigate to EditTaskFragment
- Add a new property to TasksViewModel
- The full code for TasksViewModel.kt
- Make TasksFragment navigate to EditTaskFragment
- The full code for TasksFragment.kt
- Make EditTaskFragment display the task ID
- The full code for EditTaskFragment.kt
- What happens when the code runs
- Test Drive
- We want to use EditTaskFragment to update task records
- Use TaskDao to interact with database records
- Create EditTaskViewModel
- EditTaskViewModel will tell EditTaskFragment when to navigate
- The full code for EditTaskViewModel.kt
- EditTaskViewModel needs a view model factory
- fragment_edit_task.xml needs to display the task
- The full code for EditTaskFragment.kt
- What happens when the code runs
- Test Drive
- Your Android Toolbox
-
18. Jetpack Compose: Compose Yourself
- UI components donât have to be Views
- Hereâs what weâre going to do
- Create a new Compose project
- Configure the project
- Compose projects have no layout files
- What Compose activity code looks like
- Use a Text composable to display text
- Use composables in composable functions
- Most UIs have multiple composables
- The full code for MainActivity.kt
- Test Drive
- You can preview composable functions
- Preview composables with the Design or Split option
- Pool Puzzle
- Pool Puzzle Solution
- Letâs make the app convert temperatures
- Add a MainActivityContent composable function
- Display the header imageâ¦
- Add an Image to MainActivity.kt
- Letâs display the temperature text
- Test Drive
- Use a Button composable to add a button
- We need to pass a lambda to ConvertButton
- We need to change the value of TemperatureTextâs argument
- The full code for MainActivity.kt
- What happens when the app runs
- Test Drive
- Compose Magnets
- Compose Magnets Solution
- Let the user enter a temperature
- Add a TextField to a composable function
- The full code for MainActivity.kt
- What happens when the app runs
- Test Drive
- Weâll tweak the appâs appearance
- Add padding to the Column composable
- You can center composables in Columns or Rows
- Applying themes: revisited
- Android Studio includes extra theme code
- The full code for MainActivity.kt
- Test Drive
- BE Compose
- BE Compose Solution
- Your Android Toolbox
-
19. Integrating Compose with views: Perfect Harmony
- You can add composables to View-based UIs
- The Guessing Game app structure
- Hereâs what weâre going to do
- Update the project build.gradle fileâ¦
- Weâll replace ResultFragmentâs views with composables
- A ComposeView lets you add composables to a layout
- Add composables using Kotlin code
- Add a composable function for the fragmentâs content
- Reproduce the Start New Game button
- Reproduce ResultFragmentâs TextView
- The updated code for ResultFragment.kt
- Test Drive
- We need to remove ResultFragmentâs viewsâ¦
- onCreateView() returns the UIâs root view
- The full code for ResultFragment.kt
- What happens when the app runs
- Test Drive
- Pool Puzzle
- Next weâll make GameFragment use composables too
- Weâll add a ComposeView to fragment_game.xml
- Add a composable function for GameFragmentâs content
- Reproduce the Finish Game button
- Reproduce the EditText with a TextField
- Reproduce the Guess button
- The updated code for GameFragment.kt
- Test Drive
- Weâll display the incorrect guesses in a Text composable
- Create an IncorrectGuessesText composable function
- Compose Magnets
- Compose Magnets Solution
- The updated code for GameFragment.kt
- Test Drive
- Remove views from GameFragment.kt
- Delete fragment_game.xml
- Test Drive
- Pool Puzzle Solution
- Your Android Toolbox
- Leaving town
- Itâs been great having you here in Androidville
- A. Leftovers: The Top Ten Things (we didnât cover)
- Index
Product information
- Title: Head First Android Development, 3rd Edition
- Author(s):
- Release date: November 2021
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781492076476
You might also like
book
Head First Android Development, 2nd Edition
What will you learn from this book? If you have an idea for a killer Android …
book
Android Programming: The Big Nerd Ranch Guide, 5th Edition
Android Programming: The Big Nerd Ranch Guide is an introductory Android book for programmers with Kotlin …
book
Learning Java, 4th Edition
Java is the preferred language for many of today’s leading-edge technologies—everything from smartphones and game consoles …
book
Learning Java, 5th Edition
If you're new to Java—or new to programming—this best-selling book will guide you through the language …