Sams Teach Yourself Android Application Development In 24 .

Transcription

Praise for Sams Teach Yourself Android ApplicationDevelopment in 24 Hours, Fourth Edition“This latest edition of Sams Teach Yourself Android Application Development in 24 Hours isjust what you’ve been waiting for if you’ve been waiting to get into Android development.Freshly updated with what you need to know for developing applications using AndroidStudio for Android Lollipop (Android 5) with Material Design, this book covers what youneed to know to get started building applications for Android.”—Ray Rischpater, Author and Engineering Manager at Microsoft“The new edition of Sams Teach Yourself Android Application Development in 24 Hours covers alot of new features. The book takes you from the beginning through to uploading your ownapp into the store. All the screen shots in this edition use the new and official Android IDE(the amazing Android Studio IDE).”—Fady A. M. Ibrahim, Android Instructor, Benha Faculty of Computer and Information“Any developer who wants to get up to speed quickly on Android will appreciate this introduction. Beyond the SDK fundamentals, there’s plenty of good information on the thingsreal-world Android apps are made of, such as maps, images, and navigation. This is a greatway to dive head-first into Android development, or just to become Android-literatein record time.”—Jonathan Taylor, VP, Mobile Technology, Priceline.comThe authors knock it out of the park for new Android developers and experienced ones whowant to extend their prowess. This book is perfectly set-up for a sports technology orientedperson like me to teach me the basic principles, give me design knowledge, and then capthat off with how to add and manipulate data. Data-driven applications are the life’s bloodof every fantasy sports player and the authors’ ability to break down the path to successwith real-life exercises to put these principles into action is a Grand Slam!”—Rick Wolf, President, Fantasy Alarm, and Co-Founder, Fantasy Sports Trade Association

This page intentionally left blank

Carmen DelessioLauren DarceyShane ConderSams Teach Yourself AndroidApplicationDevelopmentFourth Edition24Hoursin800 East 96th Street, Indianapolis, Indiana, 46240 USA

Sams Teach Yourself Android Application Development in 24 Hours,Fourth EditionCopyright 2016 by Carmen Delessio, Lauren Darcey, and Shane ConderAll rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of theinformation contained herein. Although every precaution has been taken in the preparation of thisbook, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.Several images in this book use scenes from the online movie Big Buck Bunny to illustratethe use of online video and using a VideoView control. This movie and related material is distributed under a Creative Commons license. For more information on the movie, go tohttp://www.bigbuckbunny.org/.Blender Foundation http://www.blender.orgCopyright 2008, Blender Foundation / http://www.bigbuckbunny.orgSome images in this book are reproduced or are modifications based on work created and sharedby Google and used according to terms described in the Creative Commons 3.0 AttributionLicense.See shots of Google products follow these -product-graphics.htmlThe following are registered trademarks of Google:Android, Google Play, Android TV, Android Wear, Google, and the Google logo are registered trademarks of Google Inc., and are used here with permission.Flickr and Flickr API are registered trademarks of Yahoo!.No Flickr end-user images appear in this book.ISBN-13: 978-0-672-33739-0ISBN-10: 0-672-33739-8Library of Congress Control Number: 2015906279Printed in the United States of AmericaFirst Printing July 2015TrademarksAll terms mentioned in this book that are known to be trademarks or service marks have beenappropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Useof a term in this book should not be regarded as affecting the validity of any trademark or servicemark.Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, but nowarranty or fitness is implied. The information provided is on an “as is” basis. The authors andthe publisher shall have neither liability nor responsibility to any person or entity with respect toany loss or damages arising from the information contained in this book or from the use of the CDor programs accompanying it.Special SalesFor information about buying this title in bulk quantities, or for special sales opportunities (whichmay include electronic versions; custom cover designs; and content particular to your business,training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419.For government sales inquiries, please contact governmentsales@pearsoned.com.For questions about sales outside the U.S., please contact international@pearsoned.com.Acquisitions EditorLaura LewinDevelopmentEditorSheri CainManaging EditorKristy HartProject EditorAndy BeasterCopy EditorKeith ClineIndexerLarry SweazyProofreaderSarah KearnsTechnical EditorsRay RischpaterValerie ShipbaughPublishingCoordinatorOlivia BasegioInterior DesignerGary AdairCover DesignerMark ShirarCompositionNonie Ratcliff

Contents at a GlancePreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xivPart I Android FundamentalsHOUR 1 Introducing Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Understanding Intents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Understanding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Activities and Fragments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 Responsive Apps: Running in the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Part IICreating the User InterfaceHOUR 6 Using Basic UI Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917 Using Layouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058 ListViews and Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1199 Material Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13910 More Views and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16111 ImageViews and Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17912 Using VideoViews and Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19513 Adding Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Part III Working with DataHOUR 14 Using the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23315 Using SharedPreferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24516 Using SQLite and File Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26117 Accessing the Cloud: Working with a Remote API. . . . . . . . . . . . . . . . . . . . . . . . . . . 27718 Introducing Content Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29719 Creating a Content Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31120 Loaders and CursorAdapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

viSams Teach Yourself Android Application Development in 24 Hours, Fourth EditionPart IV Next StepsHOUR 21 Using Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33722 Android TV and Wear Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34723 More Features to Explore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36124 Publishing Your Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xivPart I: Android Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1HOUR 1: Introducing Android.3Defining Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Understanding Android Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Beginning Android Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Q&A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15HOUR 2: Understanding Intents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Using Intents to Start an Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Using Implicit Intents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Handling an Implicit Intent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Q&A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36HOUR 3: Understanding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Resources in Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Understanding Common Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Providing Alternative Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

duction. Beyond the SDK fundamentals, there’s plenty of good information on the things real-world Android apps are made of, such as maps, images, and navigation. This is a great way to dive head-first into Android development, or just to become Android-literate in record time.” — Jonathan Taylor, VP, Mobile Technology, Priceline.com