Creating Android Applications: Develop and Design
Chris Haseman
Creating Android Applications: Develop and Design
Chris Haseman
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 (fax)
Find us on the Web at: www.peachpit.com
To report errors, please send a note to
Peachpit Press is a division of Pearson Education.
Copyright 2012 by Chris Haseman
Editor: Clifford Colby
Development editor: Robyn Thomas
Production editor: Myrna Vladic
Copyeditor: Scout Festa
Technical editor: Jason LeBrun
Cover design: Aren Howell Straiger
Interior design: Mimi Heft
Compositor: Danielle Foster
Indexer: Valerie Haynes Perry
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact .
Notice of Liability
The information in this book is distributed on an As Is basis without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it.
Trademarks
Android is a trademark of Google Inc., registered in the United States and other countries. 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 Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
ISBN-13: 978-0-321-78409-4
ISBN-10: 0-321-78409-x
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
To my wife, Meghan, whos made me the teacher, writer, and man I am today.
Bio
Chris Haseman has been writing mobile software in various forms since 2003. He was involved in several large-scale BREW projects, from MMS messaging to Major League Baseball. More recently, he was an early Android engineer behind the doubleTwist media player, and he is now the lead Android developer for the website Tumblr. Hes a faculty member of General Assembly in NYC, where he teaches Android development. He lives in Brooklyn, where he constantly debates shaving his beard.
Acknowledgments
As always, I could spend more pages thanking people than are in the work itself. Here are a few who stand out:
David and Susanne H for their support. Ellen Y. for believing so early that I could do this. JBL for fixing my code. Robyn T. for her patience. Cliff C. for finding me. Scout F. for her tolerance of my grammar. Sharon H. for her harassment IMs. Dan C. for his backing. Edwin and Susan K. for their care. Thomas K. for his subtle and quiet voice. Sparks for his humor. Cotton for being there. Lee for the place to write. The teams at both Tumblr and doubleTwist for all their encouragement. The Android team at Google for all their hard work. Most of all, Peachpit for giving me the opportunity to write for you.
Introduction
If youve got a burning idea for an application that youre dying to share, or if you recognize the power and possibilities of the Android platform, youve come to the right place. This is a short book on an immense topic.
I dont mean to alarm anyone right off the bat here, but let me be honest: Android development is hard. Its architecture is dissimilar to that of many existing platforms (especially other mobile SDKs), there are many traps for beginners to fall into, and the documentation is frequently sparse at best. In exchange for its difficulty, however, Googles Android offers unprecedented power, control, andyesresponsibility to those who are brave enough to develop for it.
This is where my job comes in. Im here to make the process of learning to write amazing Android software as simple as possible.
Who am I to ask such things of you? Ive been writing mobile software in a professional capacity for more than eight years, and for three of those years, Ive been developing software for Android. Ive written code that runs on millions of handsets throughout the world. Also, I have a beard. We all know that people with ample facial hair appear to be more authoritative on all subjects.
In return for making this learning process as easy as possible, I ask for a few things:
You have a computer. My third-grade teacher taught me never to take anything for granted; maybe you dont have a computer. If you dont already have a computer, youll need onepreferably a fast one, because the Android emulator and Eclipse can use up a fair amount of resources quickly.
Note
Android is an equal opportunity development platform. While I personally develop on a Mac, you can use any of the three major platforms (Mac, PC, or Linux).
Youre fluent in Java. Notice that I say fluent, not expert. Because youll be writing usable applications (rather than production libraries, at least to start), I expect you to know the differences between classes and interfaces. You should be able to handle threads and concurrency without batting an eyelash. Further, the more you know about what happens under the hood (in terms of object creation and garbage collection), the faster and better your mobile applications will be.
Yes, you can get through the book and even put together rudimentary applications without knowing much about the Java programming language. However, when you encounter problemsin both performance and possibilitiesa weak foundation in the programming language may leave you without a solution.
You have boundless patience and endless curiosity. Your interest in and passion for Android will help you through the difficult subjects covered in this book and let you glide through the easy ones.
Throughout this book, I focus on how to write features, debug problems, and make interesting software. I hope that when youve finished the book, youll have a firm grasp of the fundamentals of Android software development.
Note
If youre more interested in the many whys behind Android, this book is a good one to start with, but it wont answer every question you may have.
All right, thats quite enough idle talking. Lets get started.
Who This Book Is For
This book is for people who have some programming experience and are curious about the wild world of Android development.
Who This Book Is Not For
This book is not for people who have never seen a line of Java before. It is also not for expert Android engineers with several applications under their belt.
How You Will Learn
In this book, youll learn by doing. Each chapter comes with companion sample code and clear, concise instructions for how to build that code for yourself. Youll find the code samples on the books website (www.peachpit.com/androiddevelopanddesign).
What You Will Learn