📄 Page
1
Global edition G lo b a l ed it io n this is a special edition of an established title widely used by colleges and universities throughout the world. Pearson published this exclusive edition for the benefit of students outside the United States and Canada. if you purchased this book within the United States or Canada you should be aware that it has been imported without the approval of the Publisher or author. Pearson Global Edition Global edition For these Global editions, the editorial team at Pearson has collaborated with educators across the world to address a wide range of subjects and requirements, equipping students with the best possible learning tools. this Global edition preserves the cutting-edge approach and pedagogy of the original, but also features alterations, customization and adaptation from the north american version. Paul Deitel • Harvey Deitel • Abbey Deitel android™ How to Program SeCond edition a ndroid ™ H ow to Program D eitel • D eitel • D eitel SeC o n d ed it io n Deitel_027379339X_mech.indd 1 01/07/14 8:17 am
📄 Page
2
ONLINE ACCESS Thank you for purchasing a new copy of Android™ How to Program, Second Edition. Your textbook includes 12 months of prepaid access to the book’s Companion Website. This prepaid subscription provides you with full access to the following student support areas: • Source code • Premium web chapters Use a coin to scratch off the coating and reveal your student access code. Do not use a knife or other sharp object as it may damage the code. To access the Android How to Program, Second Edition, Companion Website for the first time, you will need to register online using a computer with an Internet connection and a web browser. The process takes just a couple of minutes and only needs to be completed once. 1. Go to www.pearsonglobaleditions.com/deitel 2. Click on Companion Website. 3. Click on the Register button. 4. On the registration page, enter your student access code* found beneath the scratch- off panel. Do not type the dashes. You can use lower- or uppercase. 5. Follow the on-screen instructions. If you need help at any time during the online registration process, simply click the Need Help? icon. 6. Once your personal Login Name and Password are confirmed, you can begin using the Android How to Program Companion Website! To log in after you have registered: You only need to register for this Companion Website once. After that, you can log in any time at www.pearsonglobaleditions.com/deitel by providing your Login Name and Password when prompted. *Important: The access code can only be used once. This subscription is valid for 12 months upon activation and is not transferable. If this access code has already been revealed, it may no longer be valid. If this is the case, you can purchase a subscription by going to www.pearsonglobaleditions.com/deitel going to the Android book and following the on-screen instructions. Deitel_027379339X_ifc.indd 1 01/07/14 5:29 pm
📄 Page
3
Global Edition A01_DEIT3397_02_SE_TP.fm Page 1 Monday, July 7, 2014 8:26 AM
📄 Page
4
Deitel® Ser ies Page How To Program Series Android How to Program, 2/e C++ How to Program, 9/E C How to Program, 7/E Java™ How to Program, 10/E Java™ How to Program, Late Objects Version, 10/E Internet & World Wide Web How to Program, 5/E Visual C++® 2008 How to Program, 2/E Visual Basic® 2012 How to Program, 6/E Visual C#® 2012 How to Program, 5/E Simply Series Simply C++: An App-Driven Tutorial Approach Simply Java™ Programming: An App-Driven Tutorial Approach Simply C#: An App-Driven Tutorial Approach Simply Visual Basic® 2010: An App-Driven Approach, 4/E CourseSmart Web Books www.deitel.com/books/CourseSmart/ C++ How to Program, 8/E and 9/E Simply C++: An App-Driven Tutorial Approach Java™ How to Program, 9/E and 10/E Simply Visual Basic 2010: An App-Driven Approach, 4/E (continued from previous column) Visual Basic® 2012 How to Program, 6/E Visual Basic® 2010 How to Program, 5/E Visual C#® 2012 How to Program, 5/E Visual C#® 2010 How to Program, 4/E Deitel® Developer Series Android for Programmers: An App-Driven Approach, 2/e, Volume 1 C for Programmers with an Introduction to C11 C++11 for Programmers C# 2012 for Programmers Dive Into® iOS 6 for Programmers: An App-Driven Approach Java™ for Programmers, 2/e JavaScript for Programmers LiveLessons Video Learning Products www.deitel.com/books/LiveLessons/ Android App Development Fundamentals, C++ Fundamentals Java™ Fundamentals C# 2012 Fundamentals C# 2010 Fundamentals iOS® 6 App Development Fundamentals JavaScript Fundamentals Visual Basic Fundamentals To receive updates on Deitel publications, Resource Centers, training courses, partner offers and more, please join the Deitel communities on • Facebook®—facebook.com/DeitelFan • Twitter®—@deitel • Google+™—google.com/+DeitelFan • YouTube™—google.com/+DeitelFan • LinkedIn®—linkedin.com/company/deitel-&-associates and register for the free Deitel® Buzz Online e-mail newsletter at: www.deitel.com/newsletter/subscribe.html To communicate with the authors, send e-mail to: deitel@deitel.com For information on Dive-Into® Series on-site seminars offered by Deitel & Associates, Inc. worldwide, write to us at deitel@deitel.com or visit: www.deitel.com/training/ For continuing updates on Pearson/Deitel publications visit: www.deitel.com www.pearsonglobaleditions.com/Deitel Visit the Deitel Resource Centers that will help you master programming languages, software develop- ment, Android and iOS app development, and Internet- and web-related topics: www.deitel.com/ResourceCenters.html A01_DEIT3397_02_SE_TP.fm Page 2 Monday, July 7, 2014 8:26 AM
📄 Page
5
Paul Deitel • Harvey Deitel • Abbey Deitel Deitel & Associates, Inc. Global Edition contributions by Muthuraj M. Global Edition A01_DEIT3397_02_SE_TP.fm Page 3 Monday, July 7, 2014 8:26 AM
📄 Page
6
Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsonglobaleditions.com © Pearson Education Limited 2015 The rights of Paul Deitel, Harvey Deitel, and Abbey Deitel to be identified as the authors of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988. Authorized adaptation from the United States edition, entitled Android: How to Program, 2nd edition, ISBN 978-0-13- 376403-1, by Paul Deitel, Harvey Deitel, and Abbey Deitel, published by Pearson Education © 2015. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, withouteither the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6-10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners.The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. ISBN 10: 0-273-79339-X ISBN 13: 978-0-273-79339-7 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 14 13 12 11 10 Typeset in Adobe Garamond by GEX Publishing Services. Printed and bound by Courier Westford in the United States of America. Editorial Director, ECS: Marcia Horton Head of Learning Asset Acquisition, Global Edition: Laura Dent Executive Editor: Tracy Johnson (Dunkelberger) Director of Marketing: Christy Lesko Marketing Manager: Yez Alayan Marketing Assistant: Jon Bryant Director of Program Management: Erin Gregg Program Management-Team Lead: Scott Disanno Program Manager: Carole Snyder Project Management-Team Lead: Laura Burgess Project Manager: Robert Engelhardt Publishing Administrator and Business Analyst, Global Edition: Shokhi Shah Khandelwal Acquisitions Editor, Global Edition: Karthik Subramanian Assistant Project Editor, Global Edition: Sinjita Basu Media Producer, Global Edition: M. Vikram Kumar Senior Manufacturing Controller, Production, Global Edition: Trudy Kimber Procurement Specialist: Linda Sager Permissions Supervisor: Michael Joyce Permissions Administrator: Jenell Forschler Director, Image Asset Services: Annie Atherton Manager, Visual Research: Karen Sanatar Media Project Manager: Renata Butera Cover Designer: Shree Inbakumar Cover Photo: Kirill__M/ Shutterstock Cover Printer: Courier Westford A01_DEIT3397_02_SE_TP.fm Page 4 Monday, July 7, 2014 8:26 AM
📄 Page
7
In Memory of Amar G. Bose, MIT Professor and Founder and Chairman of the Bose Corporation: It was a privilege being your student—and members of the next generation of Deitels, who heard our dad say how your classes inspired him to do his best work. You taught us that if we go after the really hard prob- lems, then great things can happen. Harvey Deitel Paul and Abbey Deitel A01_DEIT3397_02_SE_TP.fm Page 5 Monday, July 7, 2014 8:26 AM
📄 Page
8
Trademarks DEITEL, the double-thumbs-up bug and DIVE-INTO are registered trademarks of Deitel & Associates, Inc. Java is a registered trademark of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Google, Android, Google Play, Google Maps, Google Wallet, Nexus, YouTube, AdSense and AdMob are trademarks of Google, Inc. Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the documents and related graphics published as part of the services for any purpose. All such documents and related graphics are provided “as is” without warranty of any kind. Microsoft and/ or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event shall Microsoft and/or its respective sup- pliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services. The documents and related graphics contained herein could include technical inaccuracies or typograph- ical errors. Changes are periodically added to the information herein. Microsoft and/or its respective sup- pliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may be viewed in full within the software version specified. Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. Screen shots and icons reprinted with permission from the Microsoft Corporation. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. Throughout this book, trademarks are used. Rather than put a trademark symbol in every occurrence of a trademarked name, we state that we are using the names in an editorial fashion only and to the benefit of the trademark owner, with no intention of infringement of the trademark. A01_DEIT3397_02_SE_TP.fm Page 6 Monday, July 7, 2014 8:26 AM
📄 Page
9
Preface 19 Before You Begin 31 1 Introduction to Android 39 1.1 Introduction 40 1.2 Android—The World’s Leading Mobile Operating System 41 1.3 Android Features 41 1.4 Android Operating System 45 1.4.1 Android 2.2 (Froyo) 45 1.4.2 Android 2.3 (Gingerbread) 46 1.4.3 Android 3.0 through 3.2 (Honeycomb) 46 1.4.4 Android 4.0 through 4.0.4 (Ice Cream Sandwich) 46 1.4.5 Android 4.1–4.3 (Jelly Bean) 47 1.4.6 Android 4.4 (KitKat) 48 1.5 Downloading Apps from Google Play 49 1.6 Packages 50 1.7 Android Software Development Kit (SDK) 51 1.8 Object-Oriented Programming: A Quick Refresher 54 1.8.1 The Automobile as an Object 55 1.8.2 Methods and Classes 55 1.8.3 Instantiation 55 1.8.4 Reuse 55 1.8.5 Messages and Method Calls 55 1.8.6 Attributes and Instance Variables 56 1.8.7 Encapsulation 56 1.8.8 Inheritance 56 1.8.9 Object-Oriented Analysis and Design (OOAD) 56 1.9 Test-Driving the Doodlz App in an Android Virtual Device (AVD) 57 1.9.1 Running the Doodlz App in the Nexus 4 Smartphone AVD 57 1.9.2 Running the Doodlz App in a Tablet AVD 66 1.9.3 Running the Doodlz App on an Android Device 68 1.10 Building Great Android Apps 68 1.11 Android Development Resources 70 1.12 Wrap-Up 73 Contents A02_DEIT3397_02_SE_TOC.fm Page 7 Tuesday, July 8, 2014 8:13 AM
📄 Page
10
8 Contents 2 Welcome App 76 Dive-Into® the Android Developer Tools: Introducing Visual GUI Design, Layouts, Accessibility and Internationalization 2.1 Introduction 77 2.2 Technologies Overview 78 2.2.1 Android Developer Tools IDE 78 2.2.2 TextViews and ImageViews 78 2.2.3 App Resources 78 2.2.4 Accessibility 78 2.2.5 Internationalization 78 2.3 Creating an App 79 2.3.1 Launching the Android Developer Tools IDE 79 2.3.2 Creating a New Project 79 2.3.3 New Android Application Dialog 80 2.3.4 Configure Project Step 81 2.3.5 Configure Launcher Icon Step 81 2.3.6 Create Activity Step 83 2.3.7 Blank Activity Step 84 2.4 Android Developer Tools Window 85 2.4.1 Package Explorer Window 86 2.4.2 Editor Windows 86 2.4.3 Outline Window 86 2.4.4 App Resource Files 86 2.4.5 Graphical Layout Editor 87 2.4.6 The Default GUI 87 2.5 Building the App’s GUI with the Graphical Layout Editor 89 2.5.1 Adding Images to the Project 89 2.5.2 Changing the Id Property of the RelativeLayout and the TextView 90 2.5.3 Configuring the TextView 91 2.5.4 Adding ImageViews to Display the Images 95 2.6 Running the Welcome App 97 2.7 Making Your App Accessible 98 2.8 Internationalizing Your App 100 2.9 Wrap-Up 104 3 Tip Calculator App 107 Introducing GridLayout, LinearLayout, EditText, SeekBar, Event Handling, NumberFormat and Defining App Functionality with Java 3.1 Introduction 108 3.2 Test-Driving the Tip Calculator App 109 3.3 Technologies Overview 110 3.3.1 Class Activity 110 3.3.2 Activity Lifecycle Methods 110 3.3.3 Arranging Views with LinearLayout and GridLayout 111 A02_DEIT3397_02_SE_TOC.fm Page 8 Monday, July 7, 2014 8:28 AM
📄 Page
11
Contents 9 3.3.4 Creating and Customizing the GUI with the Graphical Layout Editor and the Outline and Properties Windows 111 3.3.5 Formatting Numbers as Locale-Specific Currency and Percentage Strings 112 3.3.6 Implementing Interface TextWatcher for Handling EditText Text Changes 112 3.3.7 Implementing Interface OnSeekBarChangeListener for Handling SeekBar Thumb Position Changes 112 3.3.8 AndroidManifest.xml 113 3.4 Building the App’s GUI 113 3.4.1 GridLayout Introduction 113 3.4.2 Creating the TipCalculator Project 115 3.4.3 Changing to a GridLayout 115 3.4.4 Adding the TextViews, EditText, SeekBar and LinearLayouts 116 3.4.5 Customizing the Views to Complete the Design 118 3.5 Adding Functionality to the App 122 3.6 AndroidManifest.xml 130 3.7 Wrap-Up 131 4 Twitter® Searches App 135 SharedPreferences, Collections, ImageButton, ListView, ListActivity, ArrayAdapter, Implicit Intents and AlertDialogs 4.1 Introduction 136 4.2 Test-Driving the App 137 4.2.1 Importing the App and Running It 137 4.2.2 Adding a Favorite Search 138 4.2.3 Viewing Twitter Search Results 139 4.2.4 Editing a Search 140 4.2.5 Sharing a Search 142 4.2.6 Deleting a Search 142 4.2.7 Scrolling Through Saved Searches 143 4.3 Technologies Overview 143 4.3.1 ListView 143 4.3.2 ListActivity 144 4.3.3 Customizing a ListActivity’s Layout 144 4.3.4 ImageButton 144 4.3.5 SharedPreferences 144 4.3.6 Intents for Launching Other Activities 145 4.3.7 AlertDialog 145 4.3.8 AndroidManifest.xml 146 4.4 Building the App’s GUI 146 4.4.1 Creating the Project 146 4.4.2 activity_main.xml Overview 147 4.4.3 Adding the GridLayout and Components 148 A02_DEIT3397_02_SE_TOC.fm Page 9 Monday, July 7, 2014 8:28 AM
📄 Page
12
10 Contents 4.4.4 Graphical Layout Editor Toolbar 153 4.4.5 ListView Item’s Layout: list_item.xml 154 4.5 Building the MainActivity Class 155 4.5.1 package and import Statements 155 4.5.2 Extending ListActivity 157 4.5.3 Fields of Class MainActivity 157 4.5.4 Overriding Activity Method onCreate 158 4.5.5 Anonymous Inner Class That Implements the saveButton’s OnClickListener to Save a New or Updated Search 160 4.5.6 addTaggedSearch Method 162 4.5.7 Anonymous Inner Class That Implements the ListView’s OnItemClickListener to Display Search Results 163 4.5.8 Anonymous Inner Class That Implements the ListView’s OnItemLongClickListener to Share, Edit or Delete a Search 165 4.5.9 shareSearch Method 167 4.5.10 deleteSearch Method 168 4.6 AndroidManifest.xml 170 4.7 Wrap-Up 170 5 Flag Quiz App 174 Fragments, Menus, Preferences, AssetManager, Tweened Animations, Handler, Toasts, Explicit Intents, Layouts for Multiple Device Orientations 5.1 Introduction 175 5.2 Test-Driving the Flag Quiz App 177 5.2.1 Importing the App and Running It 177 5.2.2 Configuring the Quiz 177 5.2.3 Taking the Quiz 179 5.3 Technologies Overview 181 5.3.1 Menus 181 5.3.2 Fragments 181 5.3.3 Fragment Lifecycle Methods 182 5.3.4 Managing Fragments 182 5.3.5 Preferences 182 5.3.6 assets Folder 182 5.3.7 Resource Folders 183 5.3.8 Supporting Different Screen Sizes and Resolutions 183 5.3.9 Determining the Screen Size 184 5.3.10 Toasts for Displaying Messages 184 5.3.11 Using a Handler to Execute a Runnable in the Future 184 5.3.12 Applying an Animation to a View 184 5.3.13 Logging Exception Messages 185 5.3.14 Using an Explicit Intent to Launch Another Activity in the Same App 185 5.3.15 Java Data Structures 185 5.4 Building the GUI and Resource Files 185 A02_DEIT3397_02_SE_TOC.fm Page 10 Monday, July 7, 2014 8:28 AM
📄 Page
13
Contents 11 5.4.1 Creating the Project 185 5.4.2 strings.xml and Formatted String Resources 186 5.4.3 arrays.xml 187 5.4.4 colors.xml 188 5.4.5 dimens.xml 188 5.4.6 activity_settings.xml Layout 189 5.4.7 activity_main.xml Layout for Phone and Tablet Portrait Orientation 189 5.4.8 fragment_quiz.xml Layout 189 5.4.9 activity_main.xml Layout for Tablet Landscape Orientation 192 5.4.10 preferences.xml for Specifying the App’s Settings 193 5.4.11 Creating the Flag Shake Animation 194 5.5 MainActivity Class 196 5.5.1 package Statement, import Statements and Fields 196 5.5.2 Overridden Activity Method onCreate 197 5.5.3 Overridden Activity Method onStart 199 5.5.4 Overridden Activity Method onCreateOptionsMenu 199 5.5.5 Overridden Activity Method onOptionsItemSelected 200 5.5.6 Anonymous Inner Class That Implements OnSharedPreferenceChangeListener 201 5.6 QuizFragment Class 202 5.6.1 package Statement and import Statements 202 5.6.2 Fields 203 5.6.3 Overridden Fragment Method onCreateView 204 5.6.4 Method updateGuessRows 206 5.6.5 Method updateRegions 207 5.6.6 Method resetQuiz 207 5.6.7 Method loadNextFlag 209 5.6.8 Method getCountryName 211 5.6.9 Anonymous Inner Class That Implements OnClickListener 211 5.6.10 Method disableButtons 214 5.7 SettingsFragment Class 214 5.8 SettingsActivity Class 215 5.9 AndroidManifest.xml 215 5.10 Wrap-Up 216 6 Cannon Game App 220 Listening for Touches, Manual Frame-By-Frame Animation, Graphics, Sound, Threading, SurfaceView and SurfaceHolder 6.1 Introduction 221 6.2 Test-Driving the Cannon Game App 223 6.3 Technologies Overview 223 6.3.1 Attaching a Custom View to a Layout 223 6.3.2 Using the Resource Folder raw 223 6.3.3 Activity and Fragment Lifecycle Methods 223 A02_DEIT3397_02_SE_TOC.fm Page 11 Monday, July 7, 2014 8:28 AM
📄 Page
14
12 Contents 6.3.4 Overriding View Method onTouchEvent 224 6.3.5 Adding Sound with SoundPool and AudioManager 224 6.3.6 Frame-by-Frame Animation with Threads, SurfaceView and SurfaceHolder 224 6.3.7 Simple Collision Detection 225 6.3.8 Drawing Graphics Using Paint and Canvas 225 6.4 Building the App’s GUI and Resource Files 225 6.4.1 Creating the Project 225 6.4.2 strings.xml 226 6.4.3 fragment_game.xml 226 6.4.4 activity_main.xml 227 6.4.5 Adding the Sounds to the App 227 6.5 Class Line Maintains a Line’s Endpoints 227 6.6 MainActivity Subclass of Activity 228 6.7 CannonGameFragment Subclass of Fragment 228 6.8 CannonView Subclass of View 230 6.8.1 package and import Statements 230 6.8.2 Instance Variables and Constants 231 6.8.3 Constructor 232 6.8.4 Overriding View Method onSizeChanged 234 6.8.5 Method newGame 235 6.8.6 Method updatePositions 236 6.8.7 Method fireCannonball 239 6.8.8 Method alignCannon 240 6.8.9 Method drawGameElements 241 6.8.10 Method showGameOverDialog 243 6.8.11 Methods stopGame and releaseResources 244 6.8.12 Implementing the SurfaceHolder.Callback Methods 245 6.8.13 Overriding View Method onTouchEvent 246 6.8.14 CannonThread: Using a Thread to Create a Game Loop 247 6.9 Wrap-Up 248 7 Doodlz App 253 Two-Dimensional Graphics, Canvas, Bitmap, Accelerometer, SensorManager, Multitouch Events, MediaStore, Printing, Immersive Mode 7.1 Introduction 254 7.2 Technologies Overview 256 7.2.1 Using SensorManager to Listen for Accelerometer Events 256 7.2.2 Custom DialogFragments 256 7.2.3 Drawing with Canvas and Bitmap 257 7.2.4 Processing Multiple Touch Events and Storing Lines in Paths 257 7.2.5 Android 4.4 Immersive Mode 257 7.2.6 GestureDetector and SimpleOnGestureListener 257 7.2.7 Saving the Drawing to the Device’s Gallery 257 A02_DEIT3397_02_SE_TOC.fm Page 12 Monday, July 7, 2014 8:28 AM
📄 Page
15
Contents 13 7.2.8 Android 4.4 Printing and the Android Support Library’s PrintHelper Class 258 7.3 Building the App’s GUI and Resource Files 258 7.3.1 Creating the Project 258 7.3.2 strings.xml 258 7.3.3 dimens.xml 259 7.3.4 Menu for the DoodleFragment 260 7.3.5 activity_main.xml Layout for MainActivity 261 7.3.6 fragment_doodle.xml Layout for DoodleFragment 261 7.3.7 fragment_color.xml Layout for ColorDialogFragment 262 7.3.8 fragment_line_width.xml Layout for LineWidthDialogFragment 264 7.3.9 Adding Class EraseImageDialogFragment 265 7.4 MainActivity Class 266 7.5 DoodleFragment Class 267 7.6 DoodleView Class 274 7.7 ColorDialogFragment Class 286 7.8 LineWidthDialogFragment Class 289 7.9 EraseImageDialogFragment Class 293 7.10 Wrap-Up 294 8 Address Book App 298 ListFragment, FragmentTransactions and the Fragment Back Stack, Threading and AsyncTasks, CursorAdapter, SQLite and GUI Styles 8.1 Introduction 299 8.2 Test-Driving the Address Book App 302 8.3 Technologies Overview 302 8.3.1 Displaying Fragments with FragmentTransactions 303 8.3.2 Communicating Data Between a Fragment and a Host Activity 303 8.3.3 Method onSaveInstanceState 303 8.3.4 Defining Styles and Applying Them to GUI Components 303 8.3.5 Specifying a Background for a TextView 303 8.3.6 Extending Class ListFragment to Create a Fragment That Contains a ListView 304 8.3.7 Manipulating a SQLite Database 304 8.3.8 Performing Database Operations Outside the GUI Thread with AsyncTasks 304 8.4 Building the GUI and Resource Files 304 8.4.1 Creating the Project 304 8.4.2 Creating the App’s Classes 305 8.4.3 strings.xml 305 8.4.4 styles.xml 306 8.4.5 textview_border.xml 307 8.4.6 MainActivity’s Layout: activity_main.xml 308 8.4.7 DetailsFragment’s Layout: fragment_details.xml 308 8.4.8 AddEditFragment’s Layout: fragment_add_edit.xml 310 8.4.9 Defining the Fragments’ Menus 311 A02_DEIT3397_02_SE_TOC.fm Page 13 Monday, July 7, 2014 8:28 AM
📄 Page
16
14 Contents 8.5 MainActivity Class 312 8.6 ContactListFragment Class 318 8.7 AddEditFragment Class 325 8.8 DetailsFragment Class 331 8.9 DatabaseConnector Utility Class 339 8.10 Wrap-Up 344 9 Google Play and App Business Issues 348 9.1 Introduction 349 9.2 Preparing Your Apps for Publication 349 9.2.1 Testing Your App 350 9.2.2 End User License Agreement 350 9.2.3 Icons and Labels 350 9.2.4 Versioning Your App 351 9.2.5 Licensing to Control Access to Paid Apps 351 9.2.6 Obfuscating Your Code 351 9.2.7 Getting a Private Key for Digitally Signing Your App 352 9.2.8 Screenshots 352 9.2.9 Promotional App Video 353 9.3 Pricing Your App: Free or Fee 354 9.3.1 Paid Apps 355 9.3.2 Free Apps 355 9.4 Monetizing Apps with In-App Advertising 356 9.5 Monetizing Apps: Using In-App Billing to Sell Virtual Goods 357 9.6 Registering at Google Play 358 9.7 Setting Up a Google Wallet Merchant Account 359 9.8 Uploading Your Apps to Google Play 360 9.9 Launching the Play Store from Within Your App 361 9.10 Managing Your Apps in Google Play 362 9.11 Other Android App Marketplaces 362 9.12 Other Popular Mobile App Platforms 362 9.13 Marketing Your Apps 363 9.14 Wrap-Up 367 A Introduction to Java Applications 370 A.1 Introduction 371 A.2 Your First Program in Java: Printing a Line of Text 371 A.3 Modifying Your First Java Program 375 A.4 Displaying Text with printf 377 A.5 Another Application: Adding Integers 377 A.6 Memory Concepts 381 A.7 Arithmetic 382 A.8 Decision Making: Equality and Relational Operators 385 A.9 Wrap-Up 389 A02_DEIT3397_02_SE_TOC.fm Page 14 Monday, July 7, 2014 8:28 AM
📄 Page
17
Contents 15 B Introduction to Classes, Objects, Methods and Strings 394 B.1 Introduction 395 B.2 Declaring a Class with a Method and Instantiating an Object of a Class 395 B.3 Declaring a Method with a Parameter 398 B.4 Instance Variables, set Methods and get Methods 401 B.5 Primitive Types vs. Reference Types 405 B.6 Initializing Objects with Constructors 406 B.7 Floating-Point Numbers and Type double 408 B.8 Wrap-Up 412 C Control Statements 416 C.1 Introduction 417 C.2 Algorithms 417 C.3 Pseudocode 418 C.4 Control Structures 418 C.5 if Single-Selection Statement 419 C.6 if…else Double-Selection Statement 419 C.7 while Repetition Statement 422 C.8 Case Study: Counter-Controlled Repetition 422 C.9 Case Study: Sentinel-Controlled Repetition 426 C.10 Case Study: Nested Control Statements 431 C.11 Compound Assignment Operators 434 C.12 Increment and Decrement Operators 434 C.13 Primitive Types 436 C.14 Essentials of Counter-Controlled Repetition 437 C.15 for Repetition Statement 438 C.16 Examples Using the for Statement 440 C.17 do…while Repetition Statement 442 C.18 switch Multiple-Selection Statement 443 C.19 break and continue Statements 450 C.20 Logical Operators 450 C.21 Wrap-Up 453 D Methods: A Deeper Look 461 D.1 Introduction 462 D.2 Program Modules in Java 462 D.3 static Methods, static Fields and Class Math 463 D.4 Declaring Methods with Multiple Parameters 465 D.5 Notes on Declaring and Using Methods 468 D.6 Method-Call Stack and Activation Records 469 D.7 Argument Promotion and Casting 469 D.8 Java API Packages 470 A02_DEIT3397_02_SE_TOC.fm Page 15 Monday, July 7, 2014 8:28 AM
📄 Page
18
16 Contents D.9 Introduction to Random-Number Generation 471 D.9.1 Scaling and Shifting of Random Numbers 472 D.9.2 Random-Number Repeatability for Testing and Debugging 473 D.10 Case Study: A Game of Chance; Introducing Enumerations 474 D.11 Scope of Declarations 478 D.12 Method Overloading 480 D.13 Wrap-Up 483 E Arrays and ArrayLists 490 E.1 Introduction 491 E.2 Arrays 491 E.3 Declaring and Creating Arrays 492 E.4 Examples Using Arrays 493 E.5 Case Study: Card Shuffling and Dealing Simulation 502 E.6 Enhanced for Statement 506 E.7 Passing Arrays to Methods 507 E.8 Case Study: Class GradeBook Using an Array to Store Grades 511 E.9 Multidimensional Arrays 516 E.10 Case Study: Class GradeBook Using a Two-Dimensional Array 520 E.11 Class Arrays 526 E.12 Introduction to Collections and Class ArrayList 528 E.13 Wrap-Up 531 F Classes and Objects: A Deeper Look 536 F.1 Introduction 537 F.2 Time Class Case Study 537 F.3 Controlling Access to Members 541 F.4 Referring to the Current Object’s Members with the this Reference 542 F.5 Time Class Case Study: Overloaded Constructors 544 F.6 Default and No-Argument Constructors 550 F.7 Composition 551 F.8 Enumerations 554 F.9 Garbage Collection 556 F.10 static Class Members 557 F.11 final Instance Variables 561 F.12 Packages 561 F.13 Package Access 562 F.14 Wrap-Up 562 G Object-Oriented Programming: Inheritance and Polymorphism 565 G.1 Introduction to Inheritance 566 G.2 Superclasses and Subclasses 567 G.3 protected Members 568 A02_DEIT3397_02_SE_TOC.fm Page 16 Monday, July 7, 2014 8:28 AM
📄 Page
19
Contents 17 G.4 Relationship between Superclasses and Subclasses 569 G.4.1 Creating and Using a CommissionEmployee Class 569 G.4.2 Creating and Using a BasePlusCommissionEmployee Class 574 G.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy 579 G.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables 582 G.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables 585 G.5 Class Object 590 G.6 Introduction to Polymorphism 591 G.7 Polymorphism: An Example 592 G.8 Demonstrating Polymorphic Behavior 593 G.9 Abstract Classes and Methods 596 G.10 Case Study: Payroll System Using Polymorphism 597 G.10.1 Abstract Superclass Employee 598 G.10.2 Concrete Subclass SalariedEmployee 601 G.10.3 Concrete Subclass HourlyEmployee 603 G.10.4 Concrete Subclass CommissionEmployee 604 G.10.5 Indirect Concrete Subclass BasePlusCommissionEmployee 606 G.10.6 Polymorphic Processing, Operator instanceof and Downcasting 607 G.10.7 Summary of the Allowed Assignments Between Superclass and Subclass Variables 612 G.11 final Methods and Classes 613 G.12 Case Study: Creating and Using Interfaces 614 G.12.1 Developing a Payable Hierarchy 615 G.12.2 Interface Payable 616 G.12.3 Class Invoice 617 G.12.4 Modifying Class Employee to Implement Interface Payable 619 G.12.5 Modifying Class SalariedEmployee for Use in the Payable Hierarchy 621 G.12.6 Using Interface Payable to Process Invoices and Employees Polymorphically 623 G.13 Common Interfaces of the Java API 624 G.14 Wrap-Up 625 H Exception Handling: A Deeper Look 629 H.1 Introduction 630 H.2 Example: Divide by Zero without Exception Handling 630 H.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions 632 H.4 When to Use Exception Handling 637 H.5 Java Exception Hierarchy 637 H.6 finally Block 640 H.7 Stack Unwinding and Obtaining Information from an Exception Object 644 H.8 Wrap-Up 647 A02_DEIT3397_02_SE_TOC.fm Page 17 Monday, July 7, 2014 8:28 AM
📄 Page
20
18 Contents I GUI Components and Event Handling 650 I.1 Introduction 651 I.2 Nimbus Look-and-Feel 651 I.3 Text Fields and an Introduction to Event Handling with Nested Classes 652 I.4 Common GUI Event Types and Listener Interfaces 658 I.5 How Event Handling Works 659 I.6 JButton 660 I.7 JComboBox; Using an Anonymous Inner Class for Event Handling 665 I.8 Adapter Classes 668 I.9 Wrap-Up 669 J Other Java Topics 670 J.1 Introduction 671 J.2 Collections Overview 671 J.3 Type-Wrapper Classes for Primitive Types 672 J.4 Interface Collection and Class Collections 672 J.5 Lists 673 J.5.1 ArrayList and Iterator 673 J.5.2 LinkedList 675 J.5.3 Views into Collections and Arrays Method asList 678 J.6 Collections Methods 680 J.6.1 Method sort 680 J.6.2 Method shuffle 682 J.7 Interface Queue 683 J.8 Sets 683 J.9 Maps 684 J.10 Introduction to Files and Streams 687 J.11 Class File 688 J.12 Introduction to Object Serialization 689 J.13 Introduction to Multithreading 690 J.14 Creating and Executing Threads with the Executor Framework 691 J.15 Overview of Thread Synchronization 695 J.16 Concurrent Collections Overview 696 J.17 Multithreading with GUI 696 J.18 Wrap-Up 703 K Operator Precedence Chart 706 L Primitive Types 708 Index 709 A02_DEIT3397_02_SE_TOC.fm Page 18 Monday, July 7, 2014 8:28 AM