Android Programming: The Big Nerd Ranch Guide
by Bryan Sills , Brian Gardner , Kristin Marsicano and Chris Stewart
Copyright 2022 Big Nerd Ranch, LLC.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, contact
Big Nerd Ranch, LLC.
200 Arizona Ave NE
Atlanta, GA 30307
(770) 817-6373
http://www.bignerdranch.com/
book-comments@bignerdranch.com
The 10-gallon hat logo is a trademark of Big Nerd Ranch, Inc.
Exclusive worldwide distribution of the English edition of this book by
Pearson Technology Group
800 East 96th Street
Indianapolis, IN 46240 USA
http://www.informit.com
The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
ISBN-10 0137645732
ISBN-13 978-0137645732
Fifth edition, first printing, June 2022
Release E.5.1.1
Dedication
To my friends and family, for all of the love, support, and encouragement throughout my life. |
B.S. |
To my wife, Carley, for supporting me in all that I do and reminding me of whats important along the way. |
B.G. |
To Phil, Noah, and Sam for loving and supporting me through multiple editions of this book. |
K.M. |
To my dad, David, for teaching me the value of hard work. To my mom, Lisa, for pushing me to always do the right thing. |
C.S. |
Acknowledgments
With this being our fifth edition, we find ourselves used to saying this. It always needs to be said, though: Books are not created by authors alone. They are shepherded into existence by a community of collaborators, risk-takers, and other supporters, without whom the burden of comprehending and writing all this material would be overwhelming.
Brian Hardy, who, along with Bill Phillips, had the gusto to bring the very first edition of this book into the world.
Andrew Bailey, the most intelligent rubber duck we have ever met. You deserve a spot on the list of authors. In addition to writing all the chapters on Jetpack Compose, you were always the first person we reached out to to talk through tough conceptual decisions and figure out the most effective way to explain new concepts. Your architecture and teaching expertise was invaluable in crafting the solutions presented in this book. Your impact is felt throughout its pages.
Mark Duran, for proposing the updated solution for PhotoGallery and for feedback throughout the process.
Dave Severns, for his eagle eyes and for providing updated screenshots and solutions for all the projects.
Anthony Kiniyalocts, Bryan Lindsey, Brandon Himes, Ben Zweber, Ben Bradley, Daniel Cook, Donovan LaDuke, Christian Keur, Michael Yotive, Tony Kazanjian, and Max McKinley. All yall lent your time and expertise to make this the best resource for anyone to learn Android development.
Desire Johnson, one of our fantastically talented Big Nerd Ranch designers, who whipped together the nifty cheat sheet attached to this book.
Kar Loong Wang, another of our amazing designers, for his illustrations of pizza and toppings for Coda Pizza and the handcuffs icon for CriminalIntent.
Eric Maxwell, David Greenhalgh, Josh Skeen, Jeremy Sherman, Jamie Lee, Andrew Marshall, Zack Simon, Jeremy Kliphouse, Lixin Wang, Brett McCormick, and everyone else who has made a contribution to this book over its many editions.
Our editor, Elizabeth Holaday. The famous beat author William S. Burroughs sometimes wrote by cutting up his work into little pieces, throwing them in the air, and publishing the rearrangement. Without a strong editor like Liz, our confusion and simpleminded excitement may have caused us to resort to such techniques. We are thankful that she was there to impose focus, precision, and clarity on our drafts.
Ellie Volckhausen, who designed our cover.
Simone Payment, our proofreader. Thank you for sanding away the remaining rough edges of this book.
Chris Loper at IntelligentEnglish.com, who designed and produced the print and eBook versions of the book. His DocBook toolchain made life much easier, too.
Thanks to Aaron Hillegass and the leadership team at Big Nerd Ranch. As a practical matter, it is not possible to do this work without Big Nerd Ranch, the company Aaron founded. Thank you.
Finally, thanks to our students. There is a feedback loop between us and our students: We teach them out of these materials, and they respond to them. Without that loop, this book could never have existed, nor could it be maintained. If Big Nerd Ranch books are special (and we hope they are), it is that feedback loop that makes them so. Thank you.
Learning Android
As a beginning Android programmer, you face a steep learning curve. Learning Android is like moving to a foreign city: Even if you speak the language, it will not feel like home at first. Everyone around you seems to understand things that you are missing. Things you already knew turn out to be dead wrong in this new context.
Android has a culture. That culture speaks Kotlin or Java (or a bit of both), but knowing Kotlin or Java is not enough. Getting your head around Android requires learning many new ideas and techniques. It helps to have a guide through unfamiliar territory.
That is where we come in. At Big Nerd Ranch, we believe that to be an Android programmer, you must:
This guide will help you do both. We have trained thousands of professional Android programmers using it. We will lead you through writing several Android applications, introducing concepts and techniques as needed. When there are rough spots, or when some things are tricky or obscure, you will face them head on, and we will do our best to explain why things are the way they are.
This approach allows you to put what you have learned into practice in a working app right away rather than learning a lot of theory and then having to figure out how to apply it all later. You will come away with the experience and understanding you need to get going as an Android developer.
Prerequisites
To use this book, you need to be familiar with Kotlin, including classes and objects, interfaces, listeners, packages, inner classes, object expressions, and generic classes.
If these concepts do not ring a bell, you will be in the weeds by page 2. Start instead with an introductory Kotlin book and return to this book afterward. There are many excellent introductory books available, so you can choose one based on your programming experience and learning style. May we recommend