™ ANDROID BOOT CAMP FOR ™ DEVELOPERS USING JAVA , COMPREHENSIVE: ’ A BEGINNER S GUIDE TO CREATING YOUR FIRST ANDROID APPS CORINNE HOISINGTON Australia (cid:129) Brazil (cid:129) Japan (cid:129) Korea (cid:129) Mexico (cid:129) Singapore (cid:129) Spain (cid:129) United Kingdom (cid:129) United States Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. AndroidBootCampforDevelopers ©2013CourseTechnology,CengageLearning UsingJava,Comprehensive: ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyrightherein ABeginner’sGuidetoCreatingYour maybereproduced,transmitted,storedorusedinanyformorbyanymeans FirstAndroidApps graphic,electronic,ormechanical,includingbutnotlimitedtophotocopying, CorinneHoisington recording,scanning,digitizing,taping,Webdistribution,informationnetworks, orinformationstorageandretrievalsystems,exceptaspermittedunderSection ExecutiveEditor:MarieLee 107or108ofthe1976UnitedStatesCopyrightAct,withoutthepriorwritten SeniorProductManager:AlyssaPratt permissionofthepublisher. DevelopmentEditor:LisaRuffolo Forproductinformationandtechnologyassistance,contactusat AssociateProductManager: CengageLearningCustomer&SalesSupport,www.cengage.com/support StephanieLorenz Forpermissiontousematerialfromthistextorproduct, ContentProjectManager: submitallrequestsonlineatcengage.com/permissions HeatherHopkins Furtherpermissionsquestionscanbeemailedto [email protected] ArtDirector:FaithBrosnan MarketingManager:ShannaShelton LibraryofCongressControlNumber:2012932024 Compositor:Integra ISBN-13:978-1-133-59720-9 CoverDesigner:Wing-ipNgan,Ink CourseTechnology design,inc.© 20ChannelCenterStreet CoverImageCredits: Boston,MA02210 istockphoto.com/zentilia CengageLearningisaleadingproviderofcustomizedlearningsolutionswith istockphoto.com/thesuperph officelocationsaroundtheglobe,includingSingapore,theUnitedKingdom, istockphoto.com/franckreporter Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat: iQoncept/Shutterstock.com international.cengage.com/region CengageLearningproductsarerepresentedinCanadabyNelsonEducation, Ltd. Foryourlifelonglearningsolutions,visitcourse.cengage.com Visitourcorporatewebsiteatcengage.com. Someoftheproductnamesandcompanynamesusedinthisbookhavebeen usedforidentificationpurposesonlyandmaybetrademarksorregistered trademarksoftheirrespectivemanufacturersandsellers. AnyfictionaldatarelatedtopersonsorcompaniesorURLsusedthroughout thisbookisintendedforinstructionalpurposesonly.Atthetimethisbookwas printed,anysuchdatawasfictionalandnotbelongingtoanyrealpersonsor companies. CourseTechnology,apartofCengageLearning,reservestherighttorevisethis publicationandmakechangesfromtimetotimeinitscontentwithoutnotice. Theprogramsinthisbookareforinstructionalpurposesonly. Theyhavebeentestedwithcarebutarenotguaranteedforanyparticular intentbeyondeducationalpurposes.Theauthorandthepublisherdonotoffer anywarrantiesorrepresentations,nordotheyacceptanyliabilitieswithrespect totheprograms. PrintedintheUnitedStatesofAmerica 123456718171615141312 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest. Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Brief Contents iii Preface . . . . . . . . . . . . . . . . . . xiii CHAPTER 1 Voilà! Meet the Android . . . . . . . . . . . . 1 CHAPTER 2 Simplify! The Android User Interface . . . . . 31 CHAPTER 3 Engage! Android User Input, Variables, and Operations . . . . . . . . . . . . . . . 69 CHAPTER 4 Explore! Icons and Decision-Making Controls . . . 109 CHAPTER 5 Investigate! Android Lists, Arrays, and Web Browsers . . . . . . . . . . . . . 145 CHAPTER 6 Jam! Implementing Audio in Android Apps . . 187 CHAPTER 7 Reveal! Displaying Pictures in a Gallery . . . 225 CHAPTER 8 Design! Using a DatePicker on a Tablet . . . 259 CHAPTER 9 Customize! Navigating with Tabs on a Tablet App . . . . . . . . . . . . . . . . 301 CHAPTER 10 Move! Creating Animation . . . . . . . . . 343 CHAPTER 11 Discover! Incorporating Google Maps . . . . 383 CHAPTER 12 Finale! Publishing Your Android App . . . . . 429 Glossary . . . . . . . . . . . . . . . . . 457 Index . . . . . . . . . . . . . . . . . . . 465 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Contents iv Preface . . . . . . . . . . . . . . . . . . . . . . . xiii CHAPTER 1 Voilà! Meet the Android . . . . . . . . . . . . . . . . 1 MeettheAndroid . . . . . . . . . . . . . . . . . . . . . . . . . . 2 AndroidPhoneDevice . . . . . . . . . . . . . . . . . . . . . . . 3 FeaturesoftheAndroid . . . . . . . . . . . . . . . . . . . . . . 5 WritingAndroidApps . . . . . . . . . . . . . . . . . . . . . . . 6 AndroidEmulator . . . . . . . . . . . . . . . . . . . . . . . . . 7 GettingOrientedwithMarketDeployment . . . . . . . . . . . . . . . 8 FirstVentureintotheAndroidWorld . . . . . . . . . . . . . . . . . . 9 OpeningEclipsetoCreateaNewProject . . . . . . . . . . . . . . . 9 CreatingtheHelloWorldProject . . . . . . . . . . . . . . . . . . .10 BuildingtheUserInterface . . . . . . . . . . . . . . . . . . . . .13 TakingaTourofthePackageExplorer . . . . . . . . . . . . . . . .14 DesigningtheUserInterfaceLayout . . . . . . . . . . . . . . . . .15 AddingaFormWidgettotheUserInterfaceLayout . . . . . . . . . . .17 TestingtheApplicationintheEmulator . . . . . . . . . . . . . . . .20 OpeningaSavedAppinEclipse . . . . . . . . . . . . . . . . . . .24 WrapItUp—ChapterSummary . . . . . . . . . . . . . . . . . . . .26 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 DeveloperFAQs . . . . . . . . . . . . . . . . . . . . . . . . . . .27 BeyondtheBook . . . . . . . . . . . . . . . . . . . . . . . . . .28 CaseProgrammingProjects . . . . . . . . . . . . . . . . . . . . .28 CaseProject QuoteoftheDayApp . . . . . . . . . . . . . . . . . . . . . .29 CaseProject AndroidTerminologyApp . . . . . . . . . . . . . . . . . . . .30 CaseProject BusinessCardApp . . . . . . . . . . . . . . . . . . . . . . .30 CHAPTER 2 Simplify! The Android User Interface . . . . . . . . . . 31 DesigninganAndroidApp . . . . . . . . . . . . . . . . . . . . . .32 TheBigPicture . . . . . . . . . . . . . . . . . . . . . . . . . .33 UsingtheAndroidUserInterface . . . . . . . . . . . . . . . . . . .33 LinearLayoutsandRelativeLayouts . . . . . . . . . . . . . . . . .35 DesigningtheHealthyRecipesOpeningUserInterface . . . . . . . . .37 AndroidTextProperties . . . . . . . . . . . . . . . . . . . . . .38 AddingaFiletotheResourcesFolder . . . . . . . . . . . . . . . .40 AddinganImageViewControl . . . . . . . . . . . . . . . . . . . .42 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. AddingaButtonControl . . . . . . . . . . . . . . . . . . . . . .43 PlanningaProgram . . . . . . . . . . . . . . . . . . . . . . . .44 CreatingActivities . . . . . . . . . . . . . . . . . . . . . . . . . .45 CreatinganXMLLayoutFile . . . . . . . . . . . . . . . . . . . .45 AddingaClassFile . . . . . . . . . . . . . . . . . . . . . . . .47 TheAndroidManifestFile . . . . . . . . . . . . . . . . . . . . . . .50 v AddinganActivitytotheAndroidManifest . . . . . . . . . . . . . . .50 CodingtheJavaActivity . . . . . . . . . . . . . . . . . . . . . . .53 CodinganonCreateMethod . . . . . . . . . . . . . . . . . . . .53 DisplayingtheUserInterface . . . . . . . . . . . . . . . . . . . .54 CreatingaButtonEventHandler . . . . . . . . . . . . . . . . . . .56 CodingaButtonEventHandler . . . . . . . . . . . . . . . . . . .58 CorrectingErrorsinCode . . . . . . . . . . . . . . . . . . . . .60 SavingandRunningtheApplication . . . . . . . . . . . . . . . . .61 WrapItUp—ChapterSummary . . . . . . . . . . . . . . . . . . . .61 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62 DeveloperFAQs . . . . . . . . . . . . . . . . . . . . . . . . . . .63 BeyondtheBook . . . . . . . . . . . . . . . . . . . . . . . . . .64 CaseProgrammingProjects . . . . . . . . . . . . . . . . . . . . .64 CaseProject RentalPropertyApp . . . . . . . . . . . . . . . . . . . . . . .65 CaseProject StarConstellationApp . . . . . . . . . . . . . . . . . . . . . .66 CaseProject YourSchoolApp . . . . . . . . . . . . . . . . . . . . . . . .67 CaseProject HostelAppforTravel . . . . . . . . . . . . . . . . . . . . . .67 CaseProject YourContactsApp–AddressBook . . . . . . . . . . . . . . . .68 CaseProject LatestNewsApp . . . . . . . . . . . . . . . . . . . . . . . .68 CHAPTER 3 Engage! Android User Input, Variables, and Operations . . 69 AndroidThemes . . . . . . . . . . . . . . . . . . . . . . . . . .71 PreviewingaTheme . . . . . . . . . . . . . . . . . . . . . . . .72 CodingaThemeintheAndroidManifestFile . . . . . . . . . . . . .74 SimplifyingUserInput . . . . . . . . . . . . . . . . . . . . . . . .76 AndroidTextFields . . . . . . . . . . . . . . . . . . . . . . . .76 AddingaTextField . . . . . . . . . . . . . . . . . . . . . . . .78 SettingtheHintPropertyfortheTextField . . . . . . . . . . . . . .79 CodingtheEditTextClassfortheTextField . . . . . . . . . . . . . .80 AndroidSpinnerControl . . . . . . . . . . . . . . . . . . . . . .81 AddingaSpinnerControlwithStringArrayEntries . . . . . . . . . . .85 CodingtheSpinnerControl . . . . . . . . . . . . . . . . . . . . .86 AddingtheButton,TextView,andImageViewControls . . . . . . . . .87 DeclaringVariables . . . . . . . . . . . . . . . . . . . . . . . . .92 PrimitiveDataTypes . . . . . . . . . . . . . . . . . . . . . . . .93 StringDataType . . . . . . . . . . . . . . . . . . . . . . . . .94 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CONTENTS DeclaringtheVariables . . . . . . . . . . . . . . . . . . . . . . .94 GetText()Method . . . . . . . . . . . . . . . . . . . . . . . . .95 WorkingwithMathematicalOperations . . . . . . . . . . . . . . . . .96 ArithmeticOperators . . . . . . . . . . . . . . . . . . . . . . . .96 FormattingNumbers . . . . . . . . . . . . . . . . . . . . . . . .97 vi DisplayingAndroidOutput . . . . . . . . . . . . . . . . . . . . . .98 GetSelectedItem()Method . . . . . . . . . . . . . . . . . . . . .98 SetText()Method . . . . . . . . . . . . . . . . . . . . . . . . .99 WrapItUp—ChapterSummary . . . . . . . . . . . . . . . . . . . 100 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 DeveloperFAQs . . . . . . . . . . . . . . . . . . . . . . . . . . 102 BeyondtheBook . . . . . . . . . . . . . . . . . . . . . . . . . 104 CaseProgrammingProjects . . . . . . . . . . . . . . . . . . . . 104 CaseProject StudyAbroadApp . . . . . . . . . . . . . . . . . . . . . . . 104 CaseProject TuitionApp . . . . . . . . . . . . . . . . . . . . . . . . . . 105 CaseProject NewYorkCityCabFareApp . . . . . . . . . . . . . . . . . . 106 CaseProject PaintCalculatorApp . . . . . . . . . . . . . . . . . . . . . . 107 CaseProject SplittheBillApp . . . . . . . . . . . . . . . . . . . . . . . 107 CaseProject PiggyBankChildren’sApp . . . . . . . . . . . . . . . . . . . 108 CHAPTER 4 Explore! Icons and Decision-Making Controls . . . . . 109 TheLauncherIcon . . . . . . . . . . . . . . . . . . . . . . . . . 112 CustomizingaLauncherIcon . . . . . . . . . . . . . . . . . . . 114 RadioButtonandRadioGroupControls . . . . . . . . . . . . . . . . 116 ChangingtheTextColorofAndroidControls . . . . . . . . . . . . 117 ChangingtheLayoutGravity . . . . . . . . . . . . . . . . . . . 117 ChangingtheMargins . . . . . . . . . . . . . . . . . . . . . . 118 AddingtheRadioButtonGroup . . . . . . . . . . . . . . . . . . 119 CodingaRadioButtonControl . . . . . . . . . . . . . . . . . . . 121 CompletingtheUserInterface . . . . . . . . . . . . . . . . . . . 123 MakingDecisionswithConditionalStatements . . . . . . . . . . . . . 125 UsinganIfStatement . . . . . . . . . . . . . . . . . . . . . . 125 UsingIfElseStatements . . . . . . . . . . . . . . . . . . . . . 126 RelationalOperators . . . . . . . . . . . . . . . . . . . . . . . 126 LogicalOperators . . . . . . . . . . . . . . . . . . . . . . . . 128 DataValidation . . . . . . . . . . . . . . . . . . . . . . . . . 129 ToastNotification . . . . . . . . . . . . . . . . . . . . . . . . 129 UsingtheisChecked()MethodofRadioButtonControls . . . . . . . . 130 NestedIfStatements . . . . . . . . . . . . . . . . . . . . . . 130 CodingtheButtonEvent . . . . . . . . . . . . . . . . . . . . . 131 CodingtheNestedIfStatements . . . . . . . . . . . . . . . . . 131 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.