Head First Android Development, 3rd Edition

Head First Android Development, 3rd Edition

Read it now on the O’Reilly learning platform with a 10-day free trial.

O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

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.

Show and hide more

Publisher resources

Table of contents Product information

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?
      1. Here’s what YOU can do to bend your brain into submission
      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
        1. You need to install Android Studio
        1. 1. Create a new project
        2. 2. Select a project template
        3. 3. Configure your project
        1. The folder structure includes different types of files
        1. The code editor
        2. The design editor
        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
        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
        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
        1. The design editor
        2. The code editor
        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
          1. Buttons and text views are subclasses of the same Android View class
          1. The element
          2. The element
          3. The element
          1. …because there’s hardcoded text
          1. Android Studio helps you extract String resources
          1. A String resource has been added to strings.xml
          1. You also can extract String resources manually
          1. Adding an array resource is similar to adding a String
          1. Get the spinner to display the array’s values
          1. …using an OnClickListener
          1. …and call its setOnClickListener method
          1. How to get the spinner’s value
          1. It all starts with a layout
          2. Android has different types of layout
            1. Use the best layout for your screen design
            1. Create a new project
            1. The layout’s views are inflated into objects
            1. Create a new project
            2. How to define a frame layout
            1. …and a ViewGroup is a type of View
            1. Nested layouts revisited
            2. Nesting layouts comes at a price
            3. Introducing the constraint layout
              1. You design constraint layouts VISUALLY
              1. Create a new project
              1. The project build.gradle needs a Google repository line
              2. The app build.gradle includes the constraint layout’s library
              1. Show the layout in the blueprint
              1. We’ll add a horizontal constraint to the button
              1. You can use it to add margins too
              1. Make the view just big enough
              2. Match the view’s constraints
              1. Add a second button to the blueprint
              1. Let’s build a layout that uses a barrier
              1. Place the barrier beneath the views
              1. We’re going to create a horizontal chain
              1. Align the views we’re going to 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
              1. Use “chain” to create a multi-line chain
              2. Use “aligned” to line up the views
              1. How do activities really work?
                1. Introducing the Stopwatch app
                1. Add String resources for the text labels
                2. The layout includes a Chronometer view
                1. The Chronometer’s key properties and methods
                2. The base property
                3. The start() method
                4. The stop() method
                1. But there’s just one problem…
                1. Add values using put methods
                2. Retrieve values using get methods
                1. Restore the state using onCreate()
                1. Start, stop, and restart
                1. Implement onStop() to pause the stopwatch
                1. Most apps need more than one screen
                2. Each screen is a fragment
                  1. WelcomeFragment
                  2. MessageFragment
                  3. EncryptFragment
                  1. Add some String resources
                  1. Fragment code looks similar to activity code
                  1. Inflate the fragment’s layout, and return it
                  1. The full code for MainActivity.kt
                  1. Add a version number to the project build.gradle file
                  2. Add a dependency to the app build.gradle file
                  1. Every action needs a unique ID
                  1. We need to navigate between the fragments
                  1. Fragment OnClickListener code is a little different
                  1. Tell it where to navigate with an action
                  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
                    1. Add a classpath to the project build.gradle file
                    2. Add the plug-in to the app build.gradle file
                    1. You can pass arguments to navigation actions
                    1. Use a Directions class to add arguments to actions
                    1. Use an Args class to retrieve arguments
                    2. We need to encrypt the message
                    1. A back stack scenario
                    1. Different apps, different structures
                    2. Android includes navigation UI components
                      1. An app bar
                      2. The bottom navigation bar
                      3. A navigation drawer
                      1. A default app bar gets added…
                      2. …by applying a theme
                      1. The app build.gradle file needs a Material library dependency
                      1. Color resource files define a set of colors
                      1. Remove the default app bar using a theme
                      1. Where did the app name go?
                      1. How toolbar navigation will work
                      1. Add a version number to the project build.gradle file
                      2. Add dependencies to the app build.gradle file
                      1. How the bottom navigation bar will work
                      1. …as a separate submenu
                      1. Add the image file…
                      2. …and update the nav_header.xml code
                      1. We’ve added a navigation drawer to the layout
                      1. …and link the drawer to the navigation controller
                      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
                        1. We want the toolbar to respond to scrolls
                        1. The full code for fragment_order.xml
                        1. Use isChecked to find out if a chip is checked
                        1. Find out if a pizza type has been selected
                        1. Add a toast to the FAB OnClickListener
                        1. Snackbars can have actions
                        1. Behind the scenes of findViewById()
                          1. findViewById() looks for a view in the view hierarchy
                          1. View binding is safer and more efficient than findViewById()
                          1. Enabling view binding generates code for each layout
                          1. The layout code
                          2. The activity code
                          1. Let’s implement view binding in the Bits and Pizzas app
                          1. Binding classes are generated for every layout
                          1. Activities can access views from onCreate()
                          1. . and the binding property provides non-null access to it
                          1. Configuration changes revisited
                            1. There are other problems, too
                            1. Why use a view model?
                            1. Create the Guessing Game project
                            1. …and update the app build.gradle file too
                            1. Create GameFragment…
                            2. …and then create ResultFragment
                            1. Create the navigation graph
                            1. What GameFragment needs to do
                            1. We also need to update ResultFragment.kt
                            1. …and create a view model
                            1. Use a ViewModelProvider to create view models
                            1. ResultFragment needs a view model too
                            1. We need to link ResultViewModel to ResultFragment
                            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
                              1. Add a live data dependency to the app build.gradle file
                              1. Which view model properties should use live data?
                              1. The value property can be null
                              1. Keep your privates private
                              1. Make GameViewModel decide when the game is over
                              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
                                1. How we’ll implement data binding
                                1. Two of the views need to show extra text
                                1. String resources can have arguments
                                2. Add two new String resources to strings.xml
                                1. We also need to make the data binding use live data
                                1. Switch off view binding in the app’s build.gradle file
                                1. Most apps need to store data
                                  1. Apps can persist data using a database
                                  1. Room apps are usually structured using MVVM
                                  1. Create the Tasks project
                                  1. …and update the app build.gradle file, too
                                  1. Update TasksFragment.kt
                                  1. …and check the MainActivity.kt code
                                  1. 1. A database class
                                  2. 2. Data classes for the tables
                                  3. 3. Interfaces for data access
                                  1. You define tables with an annotated data class
                                  2. Create the Task data class
                                  1. Specify the primary key with @PrimaryKey
                                  2. Specify column names with @ColumnInfo
                                  1. Create the TaskDao interface
                                  2. Use @Dao to mark the interface for data access
                                  1. Use @Update to update a record
                                  1. Use @Query for everything else
                                  1. Annotate the class with @Database
                                  1. Create and return an instance of the database
                                  1. We’ve completed all the Model code…
                                  2. …so let’s move on to the ViewModel
                                  1. We’ll use coroutines to run data access code in the background
                                  1. Create TasksViewModelFactory
                                  1. The TaskDatabase code has a TaskDao property
                                  1. Set TasksViewModel’s newTaskName property
                                  2. Call TasksViewModel’s addTask() method
                                  1. Use Transformations.map() to transform live data objects
                                  1. We need to make the layout respond to live data updates
                                  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
                                    1. Add a recycler view dependency to the app build.gradle file
                                    1. …by defining a layout file
                                    1. Create the adapter file
                                    1. Override the getItemCount() method
                                    1. How to add a recycler view to a layout
                                    1. Tell the recycler view to use the adapter
                                    1. We’ll make TasksFragment add task data to TaskItemAdapter
                                    1. We’ve finished writing all the recycler view code
                                    1. Add a card view dependency to the app build.gradle file
                                    1. Display items in a horizontal row
                                    2. Use GridLayoutManager to display items in a grid
                                    3. Arrange items in a staggered grid
                                    1. The recycler view displays task data correctly…
                                      1. …but when the data’s updated, the recycler view jumps
                                      1. notifyDataSetChanged() redraws the entire list
                                      1. DiffUtil works out differences between lists
                                      1. Create TaskDiffItemCallback.kt
                                      1. We can simplify the rest of TaskItemAdapter’s code
                                      1. …using submitList()
                                      1. How we’ll implement data binding
                                      1. Set the layout’s data binding variable to a Task
                                      1. Use data binding to set the layout’s views
                                      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
                                        1. …then update the app build.gradle file
                                        1. …and create a navigation graph
                                        1. Add methods to update the new property
                                        1. Call onTaskClicked() when the user clicks a task
                                        2. Navigate to EditTaskFragment when navigateToTask gets updated
                                        1. We also need to update EditTaskFragment.kt
                                        1. We’ll create a view model to access TaskDao’s methods
                                        1. The view model needs to get a task record…
                                        2. …and include methods to update and delete the task
                                        1. Create EditTaskViewModelFactory
                                        1. We need to update EditTaskFragment.kt too
                                        1. UI components don’t have to be Views
                                          1. We’re going to build a Compose app
                                          1. Compose activities extend ComponentActivity
                                          1. Call the Hello composable from setContent()
                                          1. You can arrange composables using Row or Column
                                          1. Add MainActivityPreview to MainActivity.kt
                                          1. …with an Image composable
                                          1. Let’s write a ConvertButton composable function
                                          1. Use remember to store celsius in memory
                                          1. We’ll use a TextField composable
                                          1. Call the function in MainActivityContent
                                          1. Center all of a Column’s content
                                          2. Center the content of a single Row
                                          1. How to apply a theme to composables
                                          1. How to apply the theme
                                          1. You can add composables to View-based UIs
                                            1. The Guessing Game app revisited
                                            1. …and update the app build.gradle file, too
                                            1. We’ll add a ComposeView to fragment_result.xml
                                            1. …and update ResultFragment.kt
                                            1. Return a ComposeView for Compose UIs
                                            1. We can delete fragment_result.xml
                                            1. Respond to LiveData using observeAsState()
                                            1. Run IncorrectGuessesText from GameFragmentContent
                                            1. We can also switch off data binding
                                            1. 1. Sharing data with other apps
                                              1. Sharing data with Android’s intent resolver
                                              2. Sharing data with the Android Sharesheet
                                              1. Use WorkManager to schedule deferrable tasks
                                              1. Use dialogs to prompt the user for a decision
                                              2. Notifications appear outside the app’s UI
                                              1. Unit tests
                                              2. Instrumented tests
                                              3. An instrumented test example
                                              1. Provide alternate layouts
                                              2. Use a SlidingPaneLayout
                                              1. The Compose ViewModel library
                                              2. Compose constraint layout library
                                              3. The Compose Navigation component
                                              1. Preparing your app for release
                                              2. Releasing your app
                                              Show and hide more

                                              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