Head First Android Development
by Dawn Griffiths and David Griffiths
Copyright 2022 David Griffiths and Dawn Griffiths. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (.
Series Creators: Kathy Sierra, Bert Bates
Editor: Virginia Wilson
Cover Designer: Karen Montgomery
Production Editor: Katherine Tozer
Production Services: Charles Roumeliotis
Indexer: Potomac Indexing, LLC
Page Viewers: Mum and Dad, Rob and Lorraine
Printing History:
June 2015: First Edition.
August 2017: Second Edition.
November 2021: Third Edition.
The OReilly logo is a registered trademark of OReilly Media, Inc. The Head First series designations, Head First Android Development, and related trade dress are trademarks of OReilly Media, Inc.
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 OReilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
No kittens were harmed in the making of this book, but several pizzas were eaten.
ISBN: 978-1-492-07652-0
[LSI]
[2021-11-10]
To all the key workers who kept us and our loved ones safe over the past two years. Were immensely grateful.
Authors of Head First Android Development
Dawn Griffiths started life as a mathematician at a top UK university, where she was awarded a first-class honors degree in mathematics. She went on to pursue a career in software development and has over 20 years experience working in the IT industry as a senior software developer.
When Dawns not working, youll find her honing her Tai Chi skills, reading, running, making bobbin lace, or cooking. She particularly enjoys spending time with her wonderful husband, David.
David Griffiths began programming at age 12, when he saw a documentary on the work of Seymour Papert. At age 15, he wrote an implementation of Paperts computer language LOGO. Since then, he has worked as an agile coach, a software developer, and a garage attendant, but not in that order.
When Davids not working, he spends much of his spare time traveling with his lovely wife, Dawn.
Together, Dawn and David have written a whole host of books, including Head First Android Development, Head First Kotlin, Head First C, Head First Rails, Head First Statistics, and the React Cookbook. They contributed to 97 Things Every Java Programmer Should Know, and they created the video course The Agile Sketchpad as a way of teaching key concepts and techniques in a way that keeps your brain active and engaged. They also deliver live, online training through the OReilly learning platform at https://www.oreilly.com/live-events.
You can follow Dawn and David on Twitter at https://twitter.com/HeadFirstDroid.
Table of Contents (the real thing)
Your brain on Android. Here you are trying to learn something, while here your brain is, doing you a favor by making sure the learning doesnt stick. Your brains thinking, Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea. So how do you trick your brain into thinking that your life depends on knowing how to develop Android apps?
Android is the worlds most popular mobile operating system. And there are billions of Android users worldwide, all waiting to download your next great idea. In this chapter, youll find out how to start turning your ideas into reality by building a basic Android app, and updating it. Youll learn how to run it on physical and virtual devices. Along the way, youll meet two of the core components of all Android apps: activities and layouts.
Most apps need to respond to the user in some way. And in this chapter, youll see how you can make your apps more interactive. Youll discover how to add an OnClickListener to your activity code so that your app can listen to what the users doing, and make an appropriate response. Youll find out more about how to design layouts, and youll learn how each UI component you add to your layout is derived from a common View ancestor. Along the way, youll discover why String resources are so important for flexible, well-designed apps.
Weve only scratched the surface of using layouts. So far, youve seen how to arrange views in a simple linear layout, but theres so much more that layouts can do. In this chapter well go a little deeper and show you how layouts really work. Youll learn how to fine-tune your linear layouts. Youll discover how to use frame layouts and scroll views. And by the end of the chapter, youll learn that even though they might look a little different, all layoutsand the views you add to themhave more in common than you might think.
You dont build a house without a blueprint. And some layouts use blueprints to make sure they look exactly the way you want. In this chapter, well introduce you to Androids constraint layout: a flexible way of designing more complex UIs. Youll discover how constraints and bias let you position and size your views, irrespective of screen size and orientation. Youll find out how to keep views in their place with guidelines and barriers. Finally, youll learn how to pack or spread views with chains and flows.
Activities form the foundation of every Android app. So far youve seen how to create an activity, and use it to interact with the user. But if you dont know about the activity lifecycle, some of its behavior might take you by surprise. In this chapter, youll learn what happens when an activity is created and destroyed, and how this can lead to unexpected consequences. Youll find out how to control its behavior when its made visible, or hidden. Youll even discover ways of saving and restoring your activitys state, just when you need it.
Most apps require more than one screen. So far, weve just looked at how to create single-screen apps, which is fine for simple applications. But what if you have
Next page