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