Wrox press beginning android 4 application development (2012)
Upcoming SlideShare
Loading in...5
×
 

Wrox press beginning android 4 application development (2012)

on

  • 1,324 views

android dev

android dev

Statistics

Views

Total Views
1,324
Views on SlideShare
1,324
Embed Views
0

Actions

Likes
0
Downloads
55
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Wrox press beginning android 4 application development (2012) Wrox press beginning android 4 application development (2012) Document Transcript

  • ffirs.indd ii 25/01/12 8:34 AM
  • BEGINNING ANDROID™ 4 APPLICATION DEVELOPMENT INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi CHAPTER 1 Getting Started with Android Programming . . . . . . . . . . . . . . . . . . . . . . . . . 1 CHAPTER 2 Activities, Fragments, and Intents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 CHAPTER 3 Getting to Know the Android User Interface. . . . . . . . . . . . . . . . . . . . . . 105 CHAPTER 4 Designing Your User Interface with Views . . . . . . . . . . . . . . . . . . . . . . . 159 CHAPTER 5 Displaying Pictures and Menus with Views. . . . . . . . . . . . . . . . . . . . . . . 219 CHAPTER 6 Data Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 CHAPTER 7 Content Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 CHAPTER 8 Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 CHAPTER 9 Location-Based Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 CHAPTER 10 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 CHAPTER 11 Developing Android Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 CHAPTER 12 Publishing Android Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 APPENDIX A Using Eclipse for Android Development . . . . . . . . . . . . . . . . . . . . . . . . . 483 APPENDIX B Using the Android Emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 APPENDIX C Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 ffirs.indd i 25/01/12 8:34 AM
  • ffirs.indd ii 25/01/12 8:34 AM
  • BEGINNING Android™ 4 Application Development ffirs.indd iii 25/01/12 8:34 AM
  • ffirs.indd iv 25/01/12 8:34 AM
  • BEGINNING Android™ 4 Application Development Wei-Meng Lee John Wiley & Sons, Inc. ffirs.indd v 25/01/12 8:34 AM
  • Beginning Android™ 4 Application Development Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2012 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-118-19954-1 ISBN: 978-1-118-22824-1 (ebk) ISBN: 978-1-118-24067-0 (ebk) ISBN: 978-1-118-26538-3 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http:// booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2011945560 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. Android is a trademark of Google, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book. ffirs.indd vi 25/01/12 8:34 AM
  • To my family: Thanks for the understanding and support while I worked on getting this book ready. I love you all! ffirs.indd vii 25/01/12 8:34 AM
  • ffirs.indd viii 25/01/12 8:34 AM
  • ABOUT THE AUTHOR WEI-MENG LEE is a technologist and founder of Developer Learning Solutions (www.learn2develop .net), a technology company specializing in hands-on training on the latest mobile technologies. Wei-Meng has many years of training experience and his training courses place special emphasis on the learning-by-doing approach. This hands-on approach to learning programming makes understanding the subject much easier than reading books, tutorials, and other documentation. Wei-Meng is also the author of Beginning iOS 5 Application Development (Wrox, 2010) and Beginning Android Application Development (Wrox, 2011). Contact Wei-Meng at weimenglee@ learn2develop.net. ABOUT THE TECHNICAL EDITOR CHAIM KRAUSE is a Simulation Specialist at the US Army’s Command and General Staff College where he develops various software products on a multitude of platforms, from iOS and Android devices to Windows desktops and Linux servers, among other duties. Python is his preferred language, but he is multilingual and also codes in Java and JavaScript/HTML5/CSS, and others. He was fortunate to begin his professional career in the software field at Borland where he was a Senior Developer Support Engineer for Delphi. Outside of computer geek stuff, Chaim enjoys techno and dubstep music and scootering with his two sled dogs, Dasher and Minnie. ffirs.indd ix 25/01/12 8:34 AM
  • ffirs.indd x 25/01/12 8:34 AM
  • CREDITS EXECUTIVE EDITOR PRODUCTION MANAGER Robert Elliott Tim Tate SENIOR PROJECT EDITOR VICE PRESIDENT AND EXECUTIVE GROUP PUBLISHER Ami Sullivan Richard Swadley TECHNICAL EDITOR Chaim Krause PRODUCTION EDITOR VICE PRESIDENT AND EXECUTIVE PUBLISHER Neil Edde Kathleen Wisor ASSOCIATE PUBLISHER COPY EDITOR Jim Minatel Luann Rouff PROJECT COORDINATOR, COVER EDITORIAL MANAGER Katie Crocker Mary Beth Wakefield PROOFREADER FREELANCER EDITORIAL MANAGER Rosemarie Graham ASSOCIATE DIRECTOR OF MARKETING David Mayhew Nancy Carassco INDEXER Johnna VanHoose Dinse MARKETING MANAGER Ryan Sneed BUSINESS MANAGER COVER IMAGE Amy Knies ffirs.indd xi COVER DESIGNER Ashley Zurcher © Viktoriya Sukhanova / iStockPhoto 25/01/12 8:34 AM
  • ffirs.indd xii 25/01/12 8:34 AM
  • ACKNOWLEDGMENTS WRITING THIS BOOK HAS been a roller-coaster ride. Working with just-released software is always a huge challenge. When I fi rst started working on this book, the Android 4 SDK had just been released; and wading through the documentation was like fi nding a needle in a haystack. To add to the challenge, the Android emulator for the tablet is extremely slow and unstable, making the development process very laborious. Now that the book is done, I hope your journey will not be as eventful as mine. Like any good guide, my duty is to make your foray into Android tablet development an enjoyable and fruitful experience. The book you are now holding is the result of the collaborative efforts of many people, and I wish to take this opportunity to acknowledge them here. First, my personal gratitude to Bob Elliott, executive editor at Wrox. Bob is always ready to lend a listening ear and to offer help when it’s needed. It is a great pleasure to work with Bob, as he is one of the most responsive persons I have ever worked with! Thank you, Bob, for the help and guidance! Of course, I cannot forget Ami Sullivan, my editor (and friend!), who is always a pleasure to work with. After working together on four books, we now know each other so well that we know the content of incoming e-mail messages even before we open them! Thank you, Ami! Nor can I forget the heroes behind the scenes: copyeditor Luann Rouff and technical editor Chaim Krause. They have been eagle-eye editing the book, making sure that every sentence makes sense — both grammatically and technically. Thanks, Luann and Chaim! Last, but not least, I want to thank my parents and my wife, Sze Wa, for all the support they have given me. They have selflessly adjusted their schedules to accommodate my busy schedule when I was working on this book. My wife, as always, has stayed up with me on numerous nights as I was furiously working to meet the deadlines, and for this I would like to say to her and my parents, “I love you all!” Finally, to our lovely dog, Ookii, thanks for staying by our side. ffirs.indd xiii 25/01/12 8:34 AM
  • ffirs.indd xiv 25/01/12 8:34 AM
  • CONTENTS INTRODUCTION CHAPTER 1: GETTING STARTED WITH ANDROID PROGRAMMING What Is Android? Android Versions Features of Android Architecture of Android Android Devices in the Market The Android Market The Android Developer Community Obtaining the Required Tools Android SDK Installing the Android SDK Tools Configuring the Android SDK Manager Eclipse Android Development Tools (ADT) Creating Android Virtual Devices (AVDs) Creating Your First Android Application Anatomy of an Android Application Summary CHAPTER 2: ACTIVITIES, FRAGMENTS, AND INTENTS Understanding Activities Applying Styles and Themes to an Activity Hiding the Activity Title Displaying a Dialog Window Displaying a Progress Dialog Displaying a More Sophisticated Progress Dialog Linking Activities Using Intents Resolving Intent Filter Collision Returning Results from an Intent Passing Data Using an Intent Object Fragments Adding Fragments Dynamically Life Cycle of a Fragment Interactions between Fragments ftoc.indd xv xxi 1 2 2 3 4 6 8 9 9 10 11 12 14 15 17 20 29 33 35 36 41 41 42 47 50 53 58 59 63 69 73 76 80 25/01/12 8:35 AM
  • CONTENTS Calling Built-In Applications Using Intents Understanding the Intent Object Using Intent Filters Adding Categories Displaying Notifications Summary CHAPTER 3: GETTING TO KNOW THE ANDROID USER INTERFACE Understanding the Components of a Screen Views and ViewGroups LinearLayout AbsoluteLayout TableLayout RelativeLayout FrameLayout ScrollView Adapting to Display Orientation Anchoring Views Resizing and Repositioning 85 89 91 96 98 103 105 105 106 107 115 116 117 118 121 123 125 127 Managing Changes to Screen Orientation 130 Persisting State Information during Changes in Configuration Detecting Orientation Changes Controlling the Orientation of the Activity 133 135 135 Utilizing the Action Bar Adding Action Items to the Action Bar Customizing the Action Items and Application Icon Creating the User Interface Programmatically Listening for UI Notifications Overriding Methods Defined in an Activity Registering Events for Views Summary CHAPTER 4: DESIGNING YOUR USER INTERFACE WITH VIEWS Using Basic Views TextView View Button, ImageButton, EditText, CheckBox, ToggleButton, RadioButton, and RadioGroup Views ProgressBar View AutoCompleteTextView View Using Picker Views 136 139 144 146 148 149 152 156 159 160 160 161 171 177 179 xvi ftoc.indd xvi 25/01/12 8:35 AM
  • CONTENTS TimePicker View DatePicker View Using List Views to Display Long Lists ListView View Using the Spinner View Understanding Specialized Fragments Using a ListFragment Using a DialogFragment Using a PreferenceFragment Summary CHAPTER 5: DISPLAYING PICTURES AND MENUS WITH VIEWS Using Image Views to Display Pictures Gallery and ImageView Views ImageSwitcher GridView Using Menus with Views Creating the Helper Methods Options Menu Context Menu Some Additional Views AnalogClock and DigitalClock Views WebView Summary CHAPTER 6: DATA PERSISTENCE Saving and Loading User Preferences Accessing Preferences Using an Activity Programmatically Retrieving and Modifying the Preferences Values Changing the Default Name of the Preferences File Persisting Data to Files Saving to Internal Storage Saving to External Storage (SD Card) Choosing the Best Storage Option Using Static Resources Creating and Using Databases Creating the DBAdapter Helper Class Using the Database Programmatically Pre-Creating the Database Summary 179 184 191 191 199 202 202 207 210 214 219 219 220 226 231 234 235 238 240 242 242 243 249 251 251 252 259 261 263 263 268 271 272 273 273 279 285 289 xvii ftoc.indd xvii 25/01/12 8:35 AM
  • CONTENTS CHAPTER 7: CONTENT PROVIDERS Sharing Data in Android Using a Content Provider Predefined Query String Constants Projections Filtering Sorting Creating Your Own Content Providers Using the Content Provider Summary CHAPTER 8: MESSAGING SMS Messaging Sending SMS Messages Programmatically Getting Feedback after Sending a Message Sending SMS Messages Using Intent Receiving SMS Messages Caveats and Warnings Sending E-mail Summary CHAPTER 9: LOCATION-BASED SERVICES Displaying Maps Creating the Project Obtaining the Maps API Key Displaying the Map Displaying the Zoom Control Changing Views Navigating to a Specific Location Adding Markers Getting the Location That Was Touched Geocoding and Reverse Geocoding Getting Location Data Monitoring a Location Project — Building a Location Tracker Summary CHAPTER 10: NETWORKING Consuming Web Services Using HTTP Downloading Binary Data 293 293 294 300 303 304 305 305 314 319 321 321 322 325 328 329 344 345 347 351 352 352 353 355 358 361 363 366 369 371 375 384 385 390 393 393 396 xviii ftoc.indd xviii 25/01/12 8:35 AM
  • CONTENTS Downloading Text Content Accessing Web Services Using the GET Method Consuming JSON Services Sockets Programming Summary CHAPTER 11: DEVELOPING ANDROID SERVICES Creating Your Own Services 402 404 409 417 426 429 429 Performing Long-Running Tasks in a Service 433 Performing Repeated Tasks in a Service 439 Executing Asynchronous Tasks on Separate Threads Using IntentService 442 Establishing Communication between a Service and an Activity Binding Activities to Services Understanding Threading Summary 445 449 454 460 CHAPTER 12: PUBLISHING ANDROID APPLICATIONS 463 Preparing for Publishing Versioning Your Application Digitally Signing Your Android Applications Deploying APK Files Using the adb.exe Tool Using a Web Server Publishing on the Android Market Summary APPENDIX A: USING ECLIPSE FOR ANDROID DEVELOPMENT Getting Around in Eclipse Workspaces Package Explorer Using Projects from Other Workspaces Using Editors within Eclipse Understanding Eclipse Perspectives Automatically Importing Packages Using the Code Completion Feature Refactoring Debugging your Application Setting Breakpoints Dealing with Exceptions 463 464 466 471 471 474 476 481 483 483 483 485 486 487 490 490 491 492 494 495 497 xix ftoc.indd xix 25/01/12 8:35 AM
  • CONTENTS APPENDIX B: USING THE ANDROID EMULATOR 499 Uses of the Android Emulator Creating Snapshots SD Card Emulation Emulating Devices with Different Screen Sizes Emulating Physical Capabilities Sending SMS Messages to the Emulator Making Phone Calls Transferring Files into and out of the Emulator Resetting the Emulator 499 501 502 504 506 508 509 511 513 APPENDIX C: ANSWERS TO EXERCISES 515 INDEX 521 xx ftoc.indd xx 25/01/12 8:35 AM
  • INTRODUCTION I FIRST STARTED PLAYING WITH THE ANDROID SDK before it was officially released as version 1.0. Back then, the tools were unpolished, the APIs in the SDK were unstable, and the documentation was sparse. Fast-forward three and a half years, Android is now a formidable mobile operating system, with a following no less impressive than the iPhone. Having gone through all the growing pains of Android, I think now is the best time to start learning about Android programming — the APIs have stabilized, and the tools have improved. One challenge remains, however: Getting started is still an elusive goal for many. What’s more, Google has recently released their latest version of the Android SDK — 4.0, a unified mobile OS for both smartphones and tablets. The Android 4.0 SDK includes several new features for tablet developers, and understanding all these new features requires some effort on the part of beginners. It was with this challenge in mind that I was motivated to write this book, one that could benefit beginning Android programmers and enable them to write progressively more sophisticated applications. As a book written to help jump-start beginning Android developers, it covers the necessary topics in a linear manner so that you can build on your knowledge without being overwhelmed by the details. I adopt the philosophy that the best way to learn is by doing — hence, the numerous Try It Out sections in each chapter, which first show you how to build something and then explain how everything works. I have also taken this opportunity to further improve the previous edition of this book, addressing feedback from readers and adding additional topics that are important to beginning Android developers. Although Android programming is a huge topic, my aim for this book is threefold: to get you started with the fundamentals, to help you understand the underlying architecture of the SDK, and to appreciate why things are done in certain ways. It is beyond the scope of any book to cover everything under the sun related to Android programming, but I am confident that after reading this book (and doing the exercises), you will be well equipped to tackle your next Android programming challenge. WHO THIS BOOK IS FOR This book is targeted for the beginning Android developer who wants to start developing applications using Google’s Android SDK. To truly benefit from this book, you should have some background in programming and at least be familiar with object-oriented programming concepts. If you are totally new to Java — the language used for Android development — you might want to take a programming course in Java programming first, or grab one of many good books on Java programming. In my experience, if you already know C# or VB.NET, learning Java is not too much of an effort; you should be comfortable just following along with the Try It Outs. For those totally new to programming, I know the lure of developing mobile apps and making some money is tempting. However, before attempting to try out the examples in this book, I think a better starting point would be to learn the basics of programming fi rst. flast.indd xxi 25/01/12 8:35 AM
  • INTRODUCTION NOTE All the examples discussed in this book were written and tested using version 4.0 of the Android SDK. While every effort is made to ensure that all the tools used in this book are the latest, it is always possible that by the time you read this book, a newer version of the tools may be available. If so, some of the instructions and/or screenshots may differ slightly. However, any variations should be manageable. WHAT THIS BOOK COVERS This book covers the fundamentals of Android programming using the Android SDK. It is divided into 12 chapters and three appendixes. Chapter 1: Getting Started with Android Programming covers the basics of the Android OS and its current state. You will learn about the features of Android devices, as well as some of the popular devices on the market. You will also learn how to download and install all the required tools to develop Android applications and then test them on the Android emulator. Chapter 2: Activities, Fragments, and Intents gets you acquainted with these three fundamental concepts in Android programming. Activities and fragments are the building blocks of an Android application. You will learn how to link activities together to form a complete Android application using intents, one of the unique characteristics of the Android OS. Chapter 3: Getting to Know the Android User Interface covers the various components that make up the UI of an Android application. You will learn about the various layouts you can use to build the UI of your application, and the numerous events that are associated with the UI when users interact with the application. Chapter 4: Designing Your User Interface with Views walks you through the various basic views you can use to build your Android UI. You will learn three main groups of views: basic views, picker views, and list views. You will also learn about the specialized fragments available in Android 3.0 and 4.0. Chapter 5: Displaying Pictures and Menus with Views continues the exploration of views. Here, you will learn how to display images using the various image views, as well as display options and context menus in your application. This chapter ends with some additional cool views that you can use to spice up your application. Chapter 6: Data Persistence shows you how to save, or store, data in your Android application. In addition to learning the various techniques to store user data, you will also learn file manipulation and how to save fi les onto internal and external storage (SD card). In addition, you will learn how to create and use a SQLite database in your Android application. xxii flast.indd xxii 25/01/12 8:35 AM
  • INTRODUCTION Chapter 7: Content Providers discusses how data can be shared among different applications on an Android device. You will learn how to use a content provider and then build one yourself. Chapter 8: Messaging explores two of the most interesting topics in mobile programming — sending SMS messages and e-mail. You will learn how to programmatically send and receive SMS and e-mail messages, and how to intercept incoming SMS messages so that the built-in Messaging application will not be able to receive any messages. Chapter 9: Location-Based Services demonstrates how to build a location-based service application using Google Maps. You will also learn how to obtain geographical location data and then display the location on the map. Chapter 10: Networking explores how to connect to web servers to download data. You will see how XML and JSON web services can be consumed in an Android application. This chapter also explains sockets programming, and you will learn how to build a chat client in Android. Chapter 11: Developing Android Services demonstrates how you can write applications using services. Services are background applications that run without a UI. You will learn how to run your services asynchronously on a separate thread, and how your activities can communicate with them. Chapter 12: Publishing Android Applications discusses the various ways you can publish your Android applications when you are ready. You will also learn about the necessary steps to publishing and selling your applications on the Android Market. Appendix A: Using Eclipse for Android Development provides a brief overview of the many features in Eclipse. Appendix B: Using the Android Emulator provides some tips and tricks on using the Android emulator for testing your applications. Appendix C: Answers to Exercises contains the solutions to the end-of-chapter exercises found in every chapter. HOW THIS BOOK IS STRUCTURED This book breaks down the task of learning Android programming into several smaller chunks, enabling you to digest each topic before delving into a more advanced one. If you are a total beginner to Android programming, start with Chapter 1 fi rst. Once you have familiarized yourself with the basics, head over to the appendixes to read more about Eclipse and the Android emulator. When you are ready, continue with Chapter 2 and gradually move into more advanced topics. A feature of this book is that all the code samples in each chapter are independent of those discussed in previous chapters. This gives you the flexibility to dive into the topics that interest you and start working on the Try It Out projects. xxiii flast.indd xxiii 25/01/12 8:35 AM
  • INTRODUCTION WHAT YOU NEED TO USE THIS BOOK All the examples in this book run on the Android emulator (which is included as part of the Android SDK). However, to get the most out of this book, having a real Android device would be useful (though not absolutely necessary). CONVENTIONS To help you get the most from the text and keep track of what’s happening, a number of conventions are used throughout the book. TRY IT OUT These Are Exercises or Examples for You to Follow The Try It Out sections appear once or more per chapter. These are exercises to work through as you follow the related discussion in the text. 1. 2. They consist of a set of numbered steps. Follow the steps with your copy of the project files. How It Works After each Try It Out, the code you’ve typed is explained in detail. As for other conventions in the text: ➤ New terms and important words are highlighted in italics when first introduced. ➤ Keyboard combinations are treated like this: Ctrl+R. ➤ Filenames, URLs, and code within the text are treated like so: persistence.properties. ➤ Code is presented in two different ways: We use a monofont type with no highlighting for most code examples. We use bolding to emphasize code that is of particular importance in the present context. NOTE Notes, tips, hints, tricks, and asides to the current discussion look like this. xxiv flast.indd xxiv 25/01/12 8:35 AM
  • INTRODUCTION SOURCE CODE As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. When at the site, simply locate the book’s title (use the Search box or one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book. You’ll fi nd the fi lename of the project you need in a CodeNote such as this at the beginning of the Try it Out features: code snippet filename After you download the code, just decompress it with your favorite compression tool. Alternatively, go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and for all other Wrox books. NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-118-19954-1. ERRATA We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you fi nd an error in one of our books, such as a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration and at the same time help us provide even higher-quality information. To fi nd the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that has been submitted for this book and posted by Wrox editors. NOTE A complete book list, including links to each book’s errata, is also available at www.wrox.com/misc-pages/booklist.shtml. If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/ techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book. xxv flast.indd xxv 25/01/12 8:35 AM
  • INTRODUCTION P2P.WROX.COM For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and to interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums. At p2p.wrox.com, you will fi nd a number of different forums that will help you not only as you read this book but also as you develop your own applications. To join the forums, just follow these steps: 1. 2. 3. 4. Go to p2p.wrox.com and click the Register link. Read the terms of use and click Agree. Complete the required information to join as well as any optional information you want to provide and click Submit. You will receive an e-mail with information describing how to verify your account and complete the joining process. NOTE You can read messages in the forums without joining P2P, but in order to post your own messages, you must join. After you join, you can post new messages and respond to messages that other users post. You can read messages at any time on the web. If you want to have new messages from a particular forum e-mailed to you, click the Subscribe to This Forum icon by the forum name in the forum listing. For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page. xxvi flast.indd xxvi 25/01/12 8:35 AM
  • 1 Getting Started with Android Programming WHAT YOU WILL LEARN IN THIS CHAPTER ➤ What is Android? ➤ Android versions and its feature set ➤ The Android architecture ➤ The various Android devices on the market ➤ The Android Market application store ➤ How to obtain the tools and SDK for developing Android applications ➤ How to develop your first Android application Welcome to the world of Android! When I was writing my fi rst book on Android (which was just less than a year ago), I stated that Android was ranked second in the U.S. smartphone market, second to Research In Motion’s (RIM) BlackBerry, and overtaking Apple’s iPhone. Shortly after the book went to press, comScore (a global leader in measuring the digital world and the preferred source of digital marketing intelligence) reported that Android has overtaken BlackBerry as the most popular smartphone platform in the U.S. A few months later, Google released Android 3.0, code named Honeycomb. With Android 3.0, Google’s focus in the new Software Development Kit was the introduction of several new features c01.indd 1 25/01/12 1:04 PM
  • 2 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING designed for widescreen devices, specifically tablets. If you are writing apps for Android smartphones, Android 3.0 is not really useful, as the new features are not supported on smartphones. At the same time that Android 3.0 was released, Google began working on the next version of Android, which can be used on both smartphones and tablets. In October 2011, Google released Android 4.0, code named Ice Cream Sandwich, and that is the focus of this book. In this chapter you will learn what Android is, and what makes it so compelling to both developers and device manufacturers alike. You will also get started with developing your fi rst Android application, and learn how to obtain all the necessary tools and set them up so that you can test your application on an Android 4.0 emulator. By the end of this chapter, you will be equipped with the basic knowledge you need to explore more sophisticated techniques and tricks for developing your next killer Android application. WHAT IS ANDROID? Android is a mobile operating system that is based on a modified version of Linux. It was originally developed by a startup of the same name, Android, Inc. In 2005, as part of its strategy to enter the mobile space, Google purchased Android and took over its development work (as well as its development team). Google wanted Android to be open and free; hence, most of the Android code was released under the open source Apache License, which means that anyone who wants to use Android can do so by downloading the full Android source code. Moreover, vendors (typically hardware manufacturers) can add their own proprietary extensions to Android and customize Android to differentiate their products from others. This simple development model makes Android very attractive and has thus piqued the interest of many vendors. This has been especially true for companies affected by the phenomenon of Apple’s iPhone, a hugely successful product that revolutionized the smartphone industry. Such companies include Motorola and Sony Ericsson, which for many years have been developing their own mobile operating systems. When the iPhone was launched, many of these manufacturers had to scramble to find new ways of revitalizing their products. These manufacturers see Android as a solution — they will continue to design their own hardware and use Android as the operating system that powers it. The main advantage of adopting Android is that it offers a unified approach to application development. Developers need only develop for Android, and their applications should be able to run on numerous different devices, as long as the devices are powered using Android. In the world of smartphones, applications are the most important part of the success chain. Device manufacturers therefore see Android as their best hope to challenge the onslaught of the iPhone, which already commands a large base of applications. Android Versions Android has gone through quite a number of updates since its fi rst release. Table 1-1 shows the various versions of Android and their codenames. c01.indd 2 25/01/12 1:04 PM
  • What Is Android? ❘ 3 TABLE 1-1: A Brief History of Android Versions ANDROID VERSION RELEASE DATE CODENAME 1.1 9 February 2009 1.5 30 April 2009 Cupcake 1.6 15 September 2009 Donut 2.0/2.1 26 October 2009 Eclair 2.2 20 May 2010 Froyo 2.3 6 December 2010 Gingerbread 3.0/3.1/3.2 22 February 2011 Honeycomb 4.0 19 October 2011 Ice Cream Sandwich In February 2011, Google released Android 3.0, a tablet-only release supporting widescreen devices. The key changes in Android 3.0 are as follows. ➤ New user interface optimized for tablets ➤ 3D desktop with new widgets ➤ Refined multi-tasking ➤ New web browser features, such as tabbed browsing, form auto-fill, bookmark synchronization, and private browsing ➤ Support for multi-core processors Applications written for versions of Android prior to 3.0 are compatible with Android 3.0 devices, and they run without modifications. Android 3.0 tablet applications that make use of the newer features available in 3.0, however, will not be able to run on older devices. To ensure that an Android tablet application can run on all versions of devices, you must programmatically ensure that you only make use of features that are supported in specific versions of Android. In October 2011, Google released Android 4.0, a version that brought all the features introduced in Android 3.0 to smartphones, along with some new features such as facial recognition unlock, data usage monitoring and control, Near Field Communication (NFC), and more. Features of Android Because Android is open source and freely available to manufacturers for customization, there are no fixed hardware or software configurations. However, Android itself supports the following features: ➤ ➤ c01.indd 3 Storage — Uses SQLite, a lightweight relational database, for data storage. Chapter 6 discusses data storage in more detail. Connectivity — Supports GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth (includes A2DP and AVRCP), Wi-Fi, LTE, and WiMAX. Chapter 8 discusses networking in more detail. 25/01/12 1:04 PM
  • 4 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING ➤ Messaging — Supports both SMS and MMS. Chapter 8 discusses messaging in more detail. ➤ Web browser — Based on the open source WebKit, together with Chrome’s V8 JavaScript engine ➤ Media support — Includes support for the following media: H.263, H.264 (in 3GP or MP4 container), MPEG-4 SP, AMR, AMR-WB (in 3GP container), AAC, HE-AAC (in MP4 or 3GP container), MP3, MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF, and BMP ➤ Hardware support — Accelerometer Sensor, Camera, Digital Compass, Proximity Sensor, and GPS ➤ Multi-touch — Supports multi-touch screens ➤ Multi-tasking — Supports multi-tasking applications ➤ Flash support — Android 2.3 supports Flash 10.1. ➤ Tethering — Supports sharing of Internet connections as a wired/wireless hotspot Architecture of Android In order to understand how Android works, take a look at Figure 1-1, which shows the various layers that make up the Android operating system (OS). The Android OS is roughly divided into five sections in four main layers: ➤ ➤ Libraries — These contain all the code that provides the main features of an Android OS. For example, the SQLite library provides database support so that an application can use it for data storage. The WebKit library provides functionalities for web browsing. ➤ Android runtime — At the same layer as the libraries, the Android runtime provides a set of core libraries that enable developers to write Android apps using the Java programming language. The Android runtime also includes the Dalvik virtual machine, which enables every Android application to run in its own process, with its own instance of the Dalvik virtual machine (Android applications are compiled into Dalvik executables). Dalvik is a specialized virtual machine designed specifically for Android and optimized for battery-powered mobile devices with limited memory and CPU. ➤ Application framework — Exposes the various capabilities of the Android OS to application developers so that they can make use of them in their applications. ➤ c01.indd 4 Linux kernel — This is the kernel on which Android is based. This layer contains all the lowlevel device drivers for the various hardware components of an Android device. Applications — At this top layer, you will find applications that ship with the Android device (such as Phone, Contacts, Browser, etc.), as well as applications that you download and install from the Android Market. Any applications that you write are located at this layer. 25/01/12 1:04 PM
  • c01.indd 5 25/01/12 1:04 PM FIGURE 1-1 Contacts Phone FreeType SSL Camera Driver Wi-Fi Driver OpenGL / ES SGL Display Driver Keypad Driver Iibc WebKit SQLite Audio Drivers ... Power Management Binder (IPC) Driver Dalvik Virtual Machine Core Libraries ANDROID RUNTIME Notification Manager View System Location Manager Browser Flash Memory Driver LINUX KERNEL Media Framework Surface Manager LIBRARIES Resource Manager Content Providers APPLICATION FRAMEWORK Window Manager Telephony Manager Activity Manager Package Manager Home APPLICATIONS
  • 6 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING Android Devices in the Market Android devices come in all shapes and sizes. As of late November 2011, the Android OS powers the following types of devices: ➤ Smartphones ➤ Tablets ➤ E-reader devices ➤ Netbooks ➤ MP4 players ➤ Internet TVs Chances are good that you own at least one of the preceding devices. Figure 1-2 shows (left to right) the Samsung Galaxy S II, the Motorola Atrix 4G, and the HTC EVO 4G smartphones. FIGURE 1-2 Another popular category of devices that manufacturers are rushing out is the tablet. Tablets typically come in two sizes: seven inches and ten inches, measured diagonally. Figure 1-3 shows the Samsung Galaxy Tab 10.1 (left) and the Asus Eee Pad Transformer TF101 (right), both 10.1-inch tablets. Both the Samsung Galaxy 10.1 and the Asus Eee Pad Transfer TF101 run on Android 3. c01.indd 6 25/01/12 1:04 PM
  • What Is Android? ❘ 7 FIGURE 1-3 Besides smartphones and tablets, Android is also beginning to appear in dedicated devices, such as e-book readers. Figure 1-4 shows the Barnes and Noble’s NOOK Color (left) and Amazon’s Kindle Fire (right), both of which are color e-Book readers running the Android OS. FIGURE 1-4 In addition to these popular mobile devices, Android is also slowly fi nding its way into your living room. People of Lava, a Swedish company, has developed an Android-based TV, called the Scandinavia Android TV (see Figure 1-5). Google has also ventured into a proprietary smart TV platform based on Android and codeveloped with companies such as Intel, Sony, and Logitech. Figure 1-6 shows Sony’s Google TV. c01.indd 7 25/01/12 1:04 PM
  • 8 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING FIGURE 1-5 FIGURE 1-6 At the time of writing, the Samsung Galaxy Nexus (see Figure 1-7) is the only device running on Android 4.0. However, Google has promised that existing devices (such as the Nexus S) will be able to upgrade to Android 4.0. By the time you are reading this, there should be a plethora of devices running Android 4.0. FIGURE 1-7 The Android Market As mentioned earlier, one of the main factors determining the success of a smartphone platform is the applications that support it. It is clear from the success of the iPhone that applications play a very vital role in determining whether a new platform swims or sinks. In addition, making these applications accessible to the general user is extremely important. c01.indd 8 25/01/12 1:04 PM
  • Obtaining the Required Tools ❘ 9 As such, in August 2008, Google announced Android Market, an online application store for Android devices, and made it available to users in October 2008. Using the Market application that is preinstalled on their Android device, users can simply download third-party applications directly onto their devices. Both paid and free applications are supported on the Android Market, though paid applications are available only to users in certain countries due to legal issues. Similarly, in some countries, users can buy paid applications from the Android Market, but developers cannot sell in that country. As an example, at the time of writing, users in India can buy apps from the Android Market, but developers in India cannot sell apps on the Android Market. The reverse may also be true; for example, users in South Korea cannot buy apps, but developers in South Korea can sell apps on the Android Market. NOTE Chapter 12 discusses more about the Android Market and how you can sell your own applications in it. The Android Developer Community With Android in its fourth version, there is a large developer community all over the world. It is now much easier to get solutions to problems, and fi nd like-minded developers to share app ideas and exchange experiences. Here are some developer communities/sites that you can turn to for help if you run into problems while working with Android: ➤ Stack Overflow (www.stackoverflow.com) — Stack Overflow is a collaboratively edited question and answer site for developers. If you have a question about Android, chances are someone at Stack Overflow is probably already discussing the same question and someone else had already provided the answer. Best of all, other developers can vote for the best answer so that you can know which are the answers that are trustworthy. ➤ Google Android Training (http://developer.android.com/training/index .html) — Google has launched the Android Training site that contains a number of useful classes grouped by topics. At the time of writing, the classes mostly contain useful code snippets that are very useful to Android developers once they have started with the basics. Once you have learned the basics in this book, I strongly suggest you take a look at the classes. ➤ Android Discuss (http://groups.google.com/group/android-discuss) — Android Discuss is a discussion group hosted by Google using the Google Groups service. Here, you will be able to discuss the various aspects of Android programming. This group is monitored closely by the Android team at Google, and so this is good place to clarify your doubts and learn new tips and tricks. OBTAINING THE REQUIRED TOOLS Now that you know what Android is and what its feature set contains, you are probably anxious to get your hands dirty and start writing some applications! Before you write your fi rst app, however, you need to download the required tools and SDKs. c01.indd 9 25/01/12 1:04 PM
  • 10 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING For Android development, you can use a Mac, a Windows PC, or a Linux machine. All the tools needed are free and can be downloaded from the Web. Most of the examples provided in this book should work fi ne with the Android emulator, with the exception of a few examples that require access to the hardware. For this book, I am using a Windows 7 computer to demonstrate all the code samples. If you are using a Mac or Linux computer, the screenshots should look similar; some minor differences may be present, but you should be able to follow along without problems. Let the fun begin! JAVA JDK The Android SDK makes use of the Java SE Development Kit (JDK). If your computer does not have the JDK installed, you should start by downloading it from www.oracle.com/technetwork/java/javase/downloads/index.html and installing it prior to moving to the next section. Android SDK The fi rst and most important piece of software you need to download is, of course, the Android SDK. The Android SDK contains a debugger, libraries, an emulator, documentation, sample code, and tutorials. You can download the Android SDK from http://developer.android.com/sdk/index.html (see Figure 1-8). FIGURE 1-8 c01.indd 10 25/01/12 1:04 PM
  • Obtaining the Required Tools ❘ 11 The Android SDK is packaged in a zip fi le. You can download it and unzip its content (the android-sdk-windows folder) into a folder, say C:Android 4.0. For Windows user, Google recommends that you download the installer_r15-windows.exe fi le instead and use it to set up the tools for you automatically. The following steps walk you through the installation process using this approach. Installing the Android SDK Tools When you have downloaded the installer_r15-windows.exe fi le, double-click it to start the installation of the Android tools. In the welcome screen of the Setup Wizard, click Next to continue. If your computer does not have Java installed, you will see the error dialog shown in Figure 1-9. However, even if you have Java installed, you may still see this error. If this is the case, click the Report error button and then click Next. FIGURE 1-9 You will be asked to provide a destination folder to install the Android SDK tools. Enter a destination path (see Figure 1-10) and click Next. When you are asked to choose a Start Menu folder to create the program’s shortcut, take the default “Android SDK Tools” and click Install. When the setup is done, check the “Start SDK Manager (to download system images, etc.)” option and click Finish (see Figure 1-11). This will start the SDK Manager. c01.indd 11 25/01/12 1:04 PM
  • 12 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING FIGURE 1-10 FIGURE 1-11 Configuring the Android SDK Manager The Android SDK Manager manages the various versions of the Android SDK currently installed on your computer. When it is launched, you will see a list of items and whether or not they are currently installed on your computer (see Figure 1-12). Check the relevant tools, documentation, and platforms you need for your project. Once you have selected the items you want, click the Install button to download them. Because it takes a while to download from Google’s server, it is a good idea to download only what you need immediately, and download the rest when you have more time. For now, you may want to check the items shown in the figure. c01.indd 12 25/01/12 1:04 PM
  • Obtaining the Required Tools ❘ 13 FIGURE 1-12 NOTE For a start, you should at least select the latest Android 4.0 SDK platform and the Extras. At the time of writing, the latest SDK platform is SDK Platform Android 4.0, API 14. Each version of the Android OS is identified by an API level number. For example, Android 2.3.3 is level 10 (API 10), while Android 3.0 is level 11 (API 11), and so on. For each level, two platforms are available. For example, level 14 offers the following: ➤ SDK Platform ➤ Google APIs by Google Inc. The key difference between the two is that the Google APIs platform contains additional APIs provided by Google (such as the Google Maps library). Therefore, if the application you are writing requires Google Maps, you need to create an AVD using the Google APIs platform (more on this is provided in Chapter 9, “Location-Based Services.” c01.indd 13 25/01/12 1:04 PM
  • 14 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING You will be asked to choose the packages to install (see Figure 1-13). Check the Accept All option and click Install. FIGURE 1-13 The SDK Manager will proceed to download the packages that you have selected. The installation takes some time, so be patient. When all the packages are installed, you will be asked to restart the ADB (Android Debug Bridge). Click Yes. Eclipse The next step is to obtain the integrated development environment (IDE) for developing your Android applications. In the case of Android, the recommended IDE is Eclipse, a multi-language software development environment featuring an extensible plug-in system. It can be used to develop various types of applications, using languages such as Java, Ada, C, C++, COBOL, Python, and others. For Android development, you should download the Eclipse IDE for Java EE Developers (www.eclipse.org/downloads/). Six editions are available: Windows (32- and 64-bit), Mac OS X (Cocoa 32- and 64), and Linux (32- and 64-bit). Simply select the relevant one for your operating system. All the examples in this book were tested using the 32-bit version of Eclipse for Windows. Once the Eclipse IDE is downloaded, unzip its content (the eclipse folder) into a folder, say C:Android 4.0. Figure 1-14 shows the content of the eclipse folder. FIGURE 1-14 To launch Eclipse, double-click on the eclipse.exe fi le. You are fi rst asked to specify your workspace. In Eclipse, a workspace is a folder where you store all your projects. Take the default suggested (or you can specify your own folder as the workspace) and click OK. c01.indd 14 25/01/12 1:04 PM
  • Obtaining the Required Tools ❘ 15 Android Development Tools (ADT) When Eclipse is launched, select Help ➪ Install New Software (see Figure 1-15) to install the Android Development Tools (ADT) plug-in for Eclipse. FIGURE 1-15 The ADT is an extension to the Eclipse IDE that supports the creation and debugging of Android applications. Using the ADT, you will be able to do the following in Eclipse: ➤ Create new Android application projects. ➤ Access the tools for accessing your Android emulators and devices. ➤ Compile and debug Android applications. ➤ Export Android applications into Android Packages (APKs). ➤ Create digital certificates for code-signing your APK. In the Install dialog that appears, specify https://dl-ssl.google.com/android/eclipse/ and press Enter. After a while, you will see the Developer Tools item appear in the middle of the window (see Figure 1-16). Expand it to reveal its content: Android DDMS, Android Development Tools, Android Hierarchy Viewer, and Android Traceview. Check all of them and click Next twice. c01.indd 15 25/01/12 1:05 PM
  • 16 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING FIGURE 1-16 NOTE If you have any problems downloading the ADT, check out Google’s help at http://developer.android.com/sdk/eclipse-adt.html#installing. You will be asked to review and accept the licenses. Check the “I accept the terms of the license agreements” option and click Finish. Once the installation is completed, you will be asked to restart Eclipse. Go ahead and restart Eclipse now. When Eclipse is restarted, you are asked to configure your Android SDK (see Figure 1-17). As the Android SDK has already been downloaded earlier in the previous section, check the “Use existing SDKs” option and specify the directory where you have installed the Android SDK. Click Next. After this step, you are asked to send your usage statistics to Google. Once you have selected your choice, click Finish. c01.indd 16 FIGURE 1-17 25/01/12 1:05 PM
  • Obtaining the Required Tools ❘ 17 NOTE As each new version of the SDK is released, the installation steps tend to differ slightly. If you do not experience the same steps as described here, don’t worry — just follow the instructions on screen. Creating Android Virtual Devices (AVDs) The next step is to create an Android Virtual Device (AVD) to be used for testing your Android applications. An AVD is an emulator instance that enables you to model an actual device. Each AVD consists of a hardware profi le; a mapping to a system image; as well as emulated storage, such as a secure digital (SD) card. You can create as many AVDs as you want in order to test your applications with several different configurations. This testing is important to confi rm the behavior of your application when it is run on different devices with varying capabilities. NOTE Appendix B discusses some of the capabilities of the Android emulator. To create an AVD, select Window ➪ AVD Manager (see Figure 1-18). FIGURE 1-18 c01.indd 17 25/01/12 1:05 PM
  • 18 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING In the Android Virtual Device Manager dialog (see Figure 1-19), click the New... button to create a new AVD. FIGURE 1-19 In the Create new Android Virtual Device (AVD) dialog, enter the items as shown in Figure 1-20. Click the Create AVD button when you are done. FIGURE 1-20 c01.indd 18 25/01/12 1:05 PM
  • Obtaining the Required Tools ❘ 19 In this case, you have created an AVD (put simply, an Android emulator) that emulates an Android device running version 4.0 of the OS with a built-in 10-MB SD card. In addition to what you have created, you also have the option to emulate the device with different screen densities and resolutions. NOTE Appendix B explains how to emulate the different types of Android devices. It is preferable to create a few AVDs with different API levels and hardware configurations so that your application can be tested on different versions of the Android OS. Once your ADV has been created, it is time to test it. Select the AVD that you want to test and click the Start… button. The Launch Options dialog will appear (see Figure 1-21). If you have a small monitor, it is recommended that you check the “Scale display to real size” option so that you can set the emulator to a smaller size. Click the Launch button to start the emulator. FIGURE 1-21 The Android emulator will start, and after a while it will be ready for use (see Figure 1-22). Go ahead and try out the emulator. It will behave just like a real Android device. After that, in the next section you will learn how to write your fi rst Android application! c01.indd 19 25/01/12 1:05 PM
  • ❘ 20 CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING FIGURE 1-22 CREATING YOUR FIRST ANDROID APPLICATION With all the tools and the SDK downloaded and installed, it is now time to start your engine. As in all programming books, the fi rst example uses the ubiquitous Hello World application. This will give you a detailed look at the various components that make up an Android project. TRY IT OUT Creating Your First Android Application codefile HelloWorld.zip available for download at Wrox.com 1. c01.indd 20 Using Eclipse, create a new project by selecting File ➪ New ➪ Project . . . (see Figure 1-23). 25/01/12 1:05 PM
  • Creating Your First Android Application ❘ 21 FIGURE 1-23 NOTE After you have created your first Android application, subsequent Android projects can be created by selecting File ➪ New ➪ Android Project. 2. 3. Expand the Android folder and select Android Project (see Figure 1-24). Click Next. Name the Android project HelloWorld, as shown in Figure 1-25, and then click Next. FIGURE 1-24 c01.indd 21 25/01/12 1:05 PM
  • 22 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING FIGURE 1-25 4. 5. Select the Android 4.0 target and click Next. Fill in the Application Info details as shown in Figure 1-26. Click Finish. FIGURE 1-26 c01.indd 22 25/01/12 1:05 PM
  • Creating Your First Android Application ❘ 23 NOTE You need to have at least a period (.) in the package name. The recommended convention for the package name is to use your domain name in reverse order, followed by the project name. For example, my company’s domain name is learn2develop.net; hence, my package name would be net.learn2develop.HelloWorld. 6. The Eclipse IDE should now look like Figure 1-27. FIGURE 1-27 7. 8. c01.indd 23 In the Package Explorer (located on the left of the Eclipse IDE), expand the HelloWorld project by clicking on the various arrows displayed to the left of each item in the project (see Figure 1-28). In the res/layout folder, double-click the main.xml fi le. The main.xml fi le defi nes the user interface (UI) of your application. The default view is the Layout view, which lays out the activity graphically. To modify the UI by hand, click the main.xml tab located at the bottom (see Figure 1-29). 25/01/12 1:05 PM
  • 24 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING FIGURE 1-28 FIGURE 1-29 9. Add the following code in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/ android” android:layout_width=”fill_parent” c01.indd 24 25/01/12 1:05 PM
  • Creating Your First Android Application ❘ 25 android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is my first Android Application!” /> <Button android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”And this is a clickable button!” /> </LinearLayout> 10. 11. To save the changes made to your project, press Ctrl+S. You are now ready to test your application on the Android emulator. Right-click the project name in Eclipse and select Run As ➪ Android Application (see Figure 1-30). FIGURE 1-30 c01.indd 25 25/01/12 1:05 PM
  • 26 ❘ 12. CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING If you have not made any mistakes in the project, you should now be able to see the application installed and running on the Android emulator (see Figure 1-31). FIGURE 1-31 13. Click the Home button (the house icon in the lower-left corner above the keyboard) so that it now shows the Home screen (see Figure 1-32). 14. Click the application launcher icon to display the list of applications installed on the device. Note that the HelloWorld application is now installed in the application launcher (see Figure 1-33). c01.indd 26 25/01/12 1:05 PM
  • Creating Your First Android Application ❘ 27 FIGURE 1-32 FIGURE 1-33 c01.indd 27 25/01/12 1:05 PM
  • 28 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING WHICH AVD WILL BE USED TO TEST YOUR APPLICATION? Recall that earlier you created a few AVDs using the AVD Manager. So which one will be launched by Eclipse when you run an Android application? Eclipse checks the target that you specified (when you created a new project), comparing it against the list of AVDs that you have created. The first one that matches will be launched to run your application. If you have more than one suitable AVD running prior to debugging the application, Eclipse will display the Android Device Chooser dialog, which enables you to select the desired emulator/device to debug the application (see Figure 1-34). FIGURE 1-34 How It Works To create an Android project using Eclipse, you need to supply the information shown in Table 1-2. TABLE 1-2: Project Files Created by Default PROPERTIES Project name The name of the project Application name A user-friendly name for your application Package name The name of the package. You should use a reverse domain name for this. Create Activity The name of the first activity in your application Min SDK Version c01.indd 28 DESCRIPTION The minimum version of the SDK that your project is targeting 25/01/12 1:05 PM
  • ❘ 29 Anatomy of an Android Application In Android, an activity is a window that contains the user interface of your applications. An application can have zero or more activities; in this example, the application contains one activity: HelloWorldActivity. This HelloWorldActivity is the entry point of the application, which is displayed when the application is started. Chapter 2 discusses activities in more detail. In this simple example, you modified the main.xml fi le to display the string “This is my fi rst Android Application!” and a button. The main.xml fi le contains the user interface of the activity, which is displayed when HelloWorldActivity is loaded. When you debug the application on the Android emulator, the application is automatically installed on the emulator. And that’s it — you have developed your fi rst Android application! The next section unravels how all the various fi les in your Android project work together to make your application come alive. ANATOMY OF AN ANDROID APPLICATION Now that you have created your fi rst Hello World Android application, it is time to dissect the innards of the Android project and examine all the parts that make everything work. First, note the various fi les that make up an Android project in the Package Explorer in Eclipse (see Figure 1-35). The various folders and their files are as follows: ➤ src — Contains the .java source files for your project. In this example, there is one file, HelloWorldActivity .java. The HelloWorldActivity.java file is the source file for your activity. You write the code for your application in this file. The Java file is listed under the package name for your project, which in this case is net .learn2develop.HelloWorld. ➤ gen — Contains the R.java file, a compiler-generated file that references all the resources found in your project. You should not modify this file. All the resources in your project are automatically compiled into this class so that you can refer to them using the class. ➤ Android 4.0 library — This item contains one file, android.jar, which contains all the class libraries needed for an Android application. ➤ assets — This folder contains all the assets used by your application, such as HTML, text files, databases, etc. ➤ bin — This folder contains the files built by the ADT during the build process. In particular, it generates the .apk file (Android Package). An .apk file is the application binary of an FIGURE 1-35 Android application. It contains everything needed to run an Android application. c01.indd 29 25/01/12 1:05 PM
  • 30 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING ➤ res — This folder contains all the resources used in your application. It also contains a few other subfolders: drawable-<resolution>, layout, and values. Chapter 3 talks more about how you can support devices with different screen resolutions and densities. ➤ AndroidManifest.xml — This is the manifest file for your Android application. Here you specify the permissions needed by your application, as well as other features (such as intent-filters, receivers, etc.). Chapter 2 discusses the use of the AndroidManifest.xml file in more detail. The main.xml fi le defi nes the user interface for your activity. Observe the following in bold: <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> The @string in this case refers to the strings.xml fi le located in the res/values folder. Hence, @string/hello refers to the hello string defi ned in the strings.xml fi le, which is “Hello World, HelloWorldActivity!”: <?xml version=”1.0” encoding=”utf-8”?> <resources> <string name=”hello”>Hello World, HelloWorldActivity!</string> <string name=”app_name”>HelloWorld</string> </resources> It is recommended that you store all the string constants in your application in this strings.xml fi le and reference these strings using the @string identifier. That way, if you ever need to localize your application to another language, all you need to do is make a copy of the entire values folder and modify the values of strings.xml to contain the string in the language that you want to display. Figure 1-36 shows that I have another folder named values-fr with the strings.xml fi le containing the same hello string in French. FIGURE 1-36 c01.indd 30 25/01/12 1:05 PM
  • Anatomy of an Android Application ❘ 31 If the user loads the same application on a phone configured to display French as the default language, your application will automatically display the hello string in French. The next important fi le in an Android project is the manifest fi le. Note the content of the AndroidManifest.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.HelloWorld” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.HelloWorldActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> The AndroidManifest.xml fi le contains detailed information about the application: ➤ ➤ The version code of the application is 1 (set via the android:versionCode attribute). This value is used to identify the version number of your application. It can be used to programmatically determine whether an application needs to be upgraded. ➤ The version name of the application is 1.0 (set via the android:versionName attribute). This string value is mainly used for display to the user. You should use the format <major>.<minor>.<point> for this value. ➤ The android:minSdkVersion attribute of the <uses-sdk> element specifies the minimum version of the OS on which the application will run. ➤ The application uses the image named ic_launcher.png located in the drawable folders. ➤ The name of this application is the string named app_name defined in the strings.xml file. ➤ c01.indd 31 It defines the package name of the application as net.learn2develop.HelloWorld. There is one activity in the application represented by the HelloWorldActivity.java file. The label displayed for this activity is the same as the application name. 25/01/12 1:05 PM
  • 32 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING ➤ Within the definition for this activity, there is an element named <intent-filter>: ➤ The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry point for the application. ➤ The category for the intent-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device’s launcher icon. Chapter 2 discusses intents in more detail. As you add more fi les and folders to your project, Eclipse will automatically generate the content of R.java, which currently contains the following: /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package net.learn2develop.HelloWorld; public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } } You are not supposed to modify the content of the R.java fi le; Eclipse automatically generates the content for you when you modify your project. NOTE If you delete R.java manually, Eclipse will regenerate it for you immediately. Note that in order for Eclipse to generate the R.java file for you, the project must not contain any errors. If you realize that Eclipse has not regenerated R.java after you have deleted it, check your project again. The code may contain syntax errors, or your XML files (such as AndroidManifest .xml, main.xml, etc.) may not be well-formed. c01.indd 32 25/01/12 1:05 PM
  • ❘ 33 Summary Finally, the code that connects the activity to the UI (main.xml) is the setContentView() method, which is in the HelloWorldActivity.java fi le: package net.learn2develop.HelloWorld; import android.app.Activity; import android.os.Bundle; public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Here, R.layout.main refers to the main.xml fi le located in the res/layout folder. As you add additional XML fi les to the res/layout folder, the filenames will automatically be generated in the R.java fi le. The onCreate() method is one of many methods that are fi red when an activity is loaded. Chapter 2 discusses the life cycle of an activity in more detail. SUMMARY This chapter has provided a brief overview of Android, and highlighted some of its capabilities. If you have followed the sections on downloading the tools and the Android SDK, you should now have a working system — one that is capable of developing more interesting Android applications other than the Hello World application. In the next chapter, you will learn about the concepts of activities and intents, and the very important roles they play in Android. EXERCISES 1. What is an AVD? 2. What is the difference between the android:versionCode and android:versionName attributes in the AndroidManifest.xml file? 3. What is the use of the strings.xml file? Answers to the exercises can be found in Appendix C. c01.indd 33 25/01/12 1:05 PM
  • 34 ❘ CHAPTER 1 GETTING STARTED WITH ANDROID PROGRAMMING WHAT YOU LEARNED IN THIS CHAPTER TOPIC Android OS Android is an open source mobile operating system based on the Linux operating system. It is available to anyone who wants to adapt it to run on their own devices. Languages used for Android application development You use the Java programming language to develop Android applications. Written applications are compiled into Dalvik executables, which are then run on top of the Dalvik virtual machine. Android Market The Android Market hosts all the various Android applications written by third-party developers. Tools for Android application development Eclipse IDE, Android SDK, and the ADT Activities An activity is represented by a screen in your Android application. Each application can have zero or more activities. The Android manifest file c01.indd 34 KEY CONCEPTS The AndroidManifest.xml file contains detailed configuration information for your application. As your example application becomes more sophisticated, you will modify this file, and you will see the different information you can add to it as you progress through the chapters. 25/01/12 1:05 PM
  • 2 Activities, Fragments, and Intents WHAT YOU WILL LEARN IN THIS CHAPTER ➤ The life cycles of an activity ➤ Using fragments to customize your UI ➤ Applying styles and themes to activities ➤ How to display activities as dialog windows ➤ Understanding the concept of intents ➤ Using the Intent object to link activities ➤ How intent filters help you selectively connect to other activities ➤ Displaying alerts to the user using notifications In Chapter 1, you learned that an activity is a window that contains the user interface of your application. An application can have zero or more activities. Typically, applications have one or more activities; and the main purpose of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity’s life cycle. Understanding the life cycle of an activity is vital to ensuring that your application works correctly. In addition to activities, Android 4.0 also supports a feature that was introduced in Android 3.0 (for tablets): fragments. Think of fragments as “miniature” activities that can be grouped to form an activity. In this chapter, you will learn about how activities and fragments work together. Apart from activities, another unique concept in Android is that of an intent. An intent is basically the “glue” that enables different activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application. Later in this chapter, you will learn more about this very important concept and how you can use it to call built-in applications such as the Browser, Phone, Maps, and more. c02.indd 35 25/01/12 12:13 PM
  • 36 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS UNDERSTANDING ACTIVITIES This chapter begins by looking at how to create an activity. To create an activity, you create a Java class that extends the Activity base class: package net.learn2develop.Activity101; import android.app.Activity; import android.os.Bundle; public class Activity101Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Your activity class loads its UI component using the XML fi le defi ned in your res/layout folder. In this example, you would load the UI from the main.xml fi le: setContentView(R.layout.main); Every activity you have in your application must be declared in your AndroidManifest.xml file, like this: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.Activity101” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.Activity101Activity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> The Activity base class defi nes a series of events that govern the life cycle of an activity. The Activity class defi nes the following events: ➤ ➤ onStart() — Called when the activity becomes visible to the user ➤ c02.indd 36 onCreate() — Called when the activity is first created onResume() — Called when the activity starts interacting with the user 25/01/12 12:13 PM
  • Understanding Activities ➤ ❘ 37 onPause() — Called when the current activity is being paused and the previous activity is being resumed ➤ onStop() — Called when the activity is no longer visible to the user ➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve memory) ➤ onRestart() — Called when the activity has been stopped and is restarting again By default, the activity created for you contains the onCreate() event. Within this event handler is the code that helps to display the UI elements of your screen. Figure 2-1 shows the life cycle of an activity and the various stages it goes through — from when the activity is started until it ends. Activity starts onCreate() User navigates back to the activity Process is killed onStart() onRestart() onResume() Activity is running The activity comes to the foreground Another activity comes in front of the activity Other applications need memory onPause() The activity comes to the foreground The activity is no longer visible onStop() onDestroy() Activity is shut down Image reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. See http://developer.android .com/reference/android/app/Activity.html FIGURE 2-1 c02.indd 37 25/01/12 12:13 PM
  • ❘ 38 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS The best way to understand the various stages of an activity is to create a new project, implement the various events, and then subject the activity to various user interactions. TRY IT OUT Understanding the Life Cycle of an Activity codefile Activity101.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Activity101. In the Activity101Activity.java fi le, add the following statements in bold: package net.learn2develop.Activity101; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class Activity101Activity extends Activity { String tag = “Lifecycle”; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d(tag, “In the onCreate() event”); } public void onStart() { super.onStart(); Log.d(tag, “In the onStart() event”); } public void onRestart() { super.onRestart(); Log.d(tag, “In the onRestart() event”); } public void onResume() { super.onResume(); Log.d(tag, “In the onResume() event”); } public void onPause() { super.onPause(); Log.d(tag, “In the onPause() event”); } public void onStop() c02.indd 38 25/01/12 12:13 PM
  • Understanding Activities ❘ 39 { super.onStop(); Log.d(tag, “In the onStop() event”); } public void onDestroy() { super.onDestroy(); Log.d(tag, “In the onDestroy() event”); } } 3. 4. Press F11 to debug the application on the Android emulator. When the activity is fi rst loaded, you should see something very similar to the following in the LogCat window (click the Debug perspective; see also Figure 2-2): 11-16 06:25:59.396: D/Lifecycle(559): In the onCreate() event 11-16 06:25:59.396: D/Lifecycle(559): In the onStart() event 11-16 06:25:59.396: D/Lifecycle(559): In the onResume() event FIGURE 2-2 5. If you click the Back button on the Android emulator, the following is printed: 11-16 06:29:26.665: D/Lifecycle(559): In the onPause() event 11-16 06:29:28.465: D/Lifecycle(559): In the onStop() event 11-16 06:29:28.465: D/Lifecycle(559): In the onDestroy() event c02.indd 39 25/01/12 12:13 PM
  • 40 ❘ 6. CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Click the Home button and hold it there. Click the Activities icon and observe the following: 11-16 06:31:08.905: D/Lifecycle(559): In the onCreate() event 11-16 06:31:08.905: D/Lifecycle(559): In the onStart() event 11-16 06:31:08.925: D/Lifecycle(559): In the onResume() event 7. Click the Phone button on the Android emulator so that the activity is pushed to the background. Observe the output in the LogCat window: 11-16 06:32:00.585: D/Lifecycle(559): In the onPause() event 11-16 06:32:05.015: D/Lifecycle(559): In the onStop() event 8. Notice that the onDestroy() event is not called, indicating that the activity is still in memory. Exit the phone dialer by clicking the Back button. The activity is now visible again. Observe the output in the LogCat window: 11-16 06:32:50.515: D/Lifecycle(559): In the onRestart() event 11-16 06:32:50.515: D/Lifecycle(559): In the onStart() event 11-16 06:32:50.515: D/Lifecycle(559): In the onResume() event The onRestart() event is now fi red, followed by the onStart() and onResume() methods. How It Works As you can see from this simple example, an activity is destroyed when you click the Back button. This is crucial to know, as whatever state the activity is currently in will be lost; hence, you need to write additional code in your activity to preserve its state when it is destroyed (Chapter 3 shows you how). At this point, note that the onPause() method is called in both scenarios — when an activity is sent to the background, as well as when it is killed when the user presses the Back button. When an activity is started, the onStart() and onResume()methods are always called, regardless of whether the activity is restored from the background or newly created. When an activity is created for the fi rst time, the onCreate() method is called. From the preceding example, you can derive the following guidelines: ➤ Use the onCreate() method to create and instantiate the objects that you will be using in your application. ➤ Use the onResume() method to start any services or code that needs to run while your activity is in the foreground. ➤ Use the onPause() method to stop any services or code that does not need to run when your activity is not in the foreground. ➤ Use the onDestroy() method to free up resources before your activity is destroyed. NOTE Even if an application has only one activity and the activity is killed, the application will still be running in memory. c02.indd 40 25/01/12 12:13 PM
  • Understanding Activities ❘ 41 Applying Styles and Themes to an Activity By default, an activity occupies the entire screen. However, you can apply a dialog theme to an activity so that it is displayed as a floating dialog. For example, you might want to customize your activity to display as a pop-up, warning users about some actions that they are going to perform. In this case, displaying the activity as a dialog is a good way to get their attention. To apply a dialog theme to an activity, simply modify the <Activity> element in the AndroidManifest.xml fi le by adding the android:theme attribute: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.Activity101” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” android:theme=”@android:style/Theme.Dialog”> <activity android:label=”@string/app_name” android:name=”.Activity101Activity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> This will make the activity appear as a dialog, as shown in Figure 2-3. Hiding the Activity Title You can also hide the title of an activity if desired (such as when you just want to display a status update to the user). To do so, use the requestWindowFeature() method and pass it the Window .FEATURE_NO_TITLE constant, like this: import import import import android.app.Activity; android.os.Bundle; android.util.Log; android.view.Window; public class Activity101Activity extends Activity { c02.indd 41 FIGURE 2-3 25/01/12 12:14 PM
  • ❘ 42 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS String tag = “Lifecycle”; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---hides the title bar--requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); Log.d(tag, “In the onCreate() event”); } } This will hide the title bar, as shown in Figure 2-4. Displaying a Dialog Window There are times when you need to display a dialog window to get a confi rmation from the user. In this case, you can override the onCreateDialog() protected method defi ned in the Activity base FIGURE 2-4 class to display a dialog window. The following Try It Out shows you how. TRY IT OUT Displaying a Dialog Window Using an Activity codefile Dialog.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Dialog. Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn_dialog” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a dialog” android:onClick=”onClick” /> </LinearLayout> 3. Add the following statements in bold to the DialogActivity.java fi le: package net.learn2develop.Dialog; import android.app.Activity; c02.indd 42 25/01/12 12:14 PM
  • Understanding Activities import import import import import import ❘ 43 android.app.AlertDialog; android.app.Dialog; android.content.DialogInterface; android.os.Bundle; android.view.View; android.widget.Toast; public class DialogActivity extends Activity { CharSequence[] items = { “Google”, “Apple”, “Microsoft” }; boolean[] itemsChecked = new boolean [items.length]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v) { showDialog(0); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: return new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher) .setTitle(“This is a dialog with some simple text...”) .setPositiveButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); } } ) .setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “Cancel clicked!”, Toast.LENGTH_SHORT).show(); } } ) .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), items[which] + (isChecked ? “ checked!”:” unchecked!”), Toast.LENGTH_SHORT).show(); c02.indd 43 25/01/12 12:14 PM
  • 44 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS } } ).create(); } return null; } } 4. Press F11 to debug the application on the Android emulator. Click the button to display the dialog (see Figure 2-5). Checking the various checkboxes will cause the Toast class to display the text of the item checked/unchecked. To dismiss the dialog, click the OK or Cancel button. How It Works To display a dialog, you fi rst implement the onCreateDialog() method in the Activity class: FIGURE 2-5 @Override protected Dialog onCreateDialog(int id) { //... } This method is called when you call the showDialog() method: public void onClick(View v) { showDialog(0); } The onCreateDialog() method is a callback for creating dialogs that are managed by the activity. When you call the showDialog() method, this callback will be invoked. The showDialog() method accepts an integer argument identifying a particular dialog to display. In this case, we used a switch statement to identify the different types of dialogs to create, although the current example creates only one type of dialog. Subsequent Try It Out exercises will extend this example to create different types of dialogs. To create a dialog, you use the AlertDialog class’s Builder constructor. You set the various properties, such as icon, title, and buttons, as well as checkboxes: @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: return new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher) .setTitle(“This is a dialog with some simple text...”) .setPositiveButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) c02.indd 44 25/01/12 12:14 PM
  • Understanding Activities ❘ 45 { Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); } } ) .setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “Cancel clicked!”, Toast.LENGTH_SHORT).show(); } } ) .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), items[which] + (isChecked ? “ checked!”:” unchecked!”), Toast.LENGTH_SHORT).show(); } } ).create(); } return null; } The preceding code sets two buttons, OK and Cancel, using the setPositiveButton() and setNegativeButton() methods, respectively. You also set a list of checkboxes for users to choose via the setMultiChoiceItems() method. For the setMultiChoiceItems() method, you passed in two arrays: one for the list of items to display and another to contain the value of each item, to indicate if they are checked. When each item is checked, you use the Toast class to display a message indicating the item that was checked. The preceding code for creating the dialog looks complicated, but it could easily be rewritten as follows: package net.learn2develop.Dialog; import import import import import import import import android.app.Activity; android.app.AlertDialog; android.app.AlertDialog.Builder; android.app.Dialog; android.content.DialogInterface; android.os.Bundle; android.view.View; android.widget.Toast; public class DialogActivity extends Activity { c02.indd 45 25/01/12 12:14 PM
  • 46 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS CharSequence[] items = { “Google”, “Apple”, “Microsoft” }; boolean[] itemsChecked = new boolean [items.length]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v) { showDialog(0); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle(“This is a dialog with some simple text...”); builder.setPositiveButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); } } ); builder.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “Cancel clicked!”, Toast.LENGTH_SHORT).show(); } } ); builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), items[which] + (isChecked ? “ checked!”:” unchecked!”), Toast.LENGTH_SHORT).show(); } } ); return builder.create(); } return null; } } c02.indd 46 25/01/12 12:14 PM
  • Understanding Activities ❘ 47 THE CONTEXT OBJECT In Android, you often encounter the Context class and its instances. Instances of the Context class are often used to provide references to your application. For example, in the following code snippet, the first parameter of the Toast class takes in a Context object: .setPositiveButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); } } However, because the Toast() class is not used directly in the activity (it is used within the AlertDialog class), you need to return an instance of the Context class by using the getBaseContext() method. You also encounter the Context class when creating a view dynamically in an activity. For example, you may want to dynamically create a TextView from code. To do so, you instantiate the TextView class, like this: TextView tv = new TextView(this); The constructor for the TextView class takes a Context object; and because the Activity class is a subclass of Context, you can use the this keyword to represent the Context object. Displaying a Progress Dialog One common UI feature in an Android device is the “Please wait” dialog that you typically see when an application is performing a long-running task. For example, the application may be logging in to a server before the user is allowed to use it, or it may be doing a calculation before displaying the result to the user. In such cases, it is helpful to display a dialog, known as a progress dialog, so that the user is kept in the loop. The following Try It Out demonstrates how to display such a dialog. TRY IT OUT 1. Displaying a Progress (Please Wait) Dialog Using the same project created in the previous section, add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” c02.indd 47 25/01/12 12:14 PM
  • 48 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn_dialog” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a dialog” android:onClick=”onClick” /> <Button android:id=”@+id/btn_dialog2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a progress dialog” android:onClick=”onClick2” /> </LinearLayout> 2. Add the following statements in bold to the DialogActivity.java fi le: package net.learn2develop.Dialog; import import import import import import import import import android.app.Activity; android.app.AlertDialog; android.app.AlertDialog.Builder; android.app.Dialog; android.app.ProgressDialog; android.content.DialogInterface; android.os.Bundle; android.view.View; android.widget.Toast; public class DialogActivity extends Activity { CharSequence[] items = { “Google”, “Apple”, “Microsoft” }; boolean[] itemsChecked = new boolean [items.length]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v) { showDialog(0); } public void onClick2(View v) { //---show the dialog--final ProgressDialog dialog = ProgressDialog.show( this, “Doing something”, “Please wait...”, true); new Thread(new Runnable(){ c02.indd 48 25/01/12 12:14 PM
  • Understanding Activities ❘ 49 public void run(){ try { //---simulate doing something lengthy--Thread.sleep(5000); //---dismiss the dialog--dialog.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } @Override protected Dialog onCreateDialog(int id) { ... } } 3. Press F11 to debug the application on the Android emulator. Clicking the second button will display the progress dialog, as shown in Figure 2-6. It will go away after five seconds. How It Works Basically, to create a progress dialog, you created an instance of the ProgressDialog class and called its show() method: FIGURE 2-6 //---show the dialog--final ProgressDialog dialog = ProgressDialog.show( this, “Doing something”, “Please wait...”, true); This displays the progress dialog that you have just seen. Because this is a modal dialog, it will block the UI until it is dismissed. To perform a long-running task in the background, you created a Thread using a Runnable block (you will learn more about threading in Chapter 11). The code that you placed inside the run() method will be executed in a separate thread, and in this case you simulated it performing something for five seconds by inserting a delay using the sleep() method: new Thread(new Runnable(){ public void run(){ try { //---simulate doing something lengthy--Thread.sleep(5000); //---dismiss the dialog--dialog.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); After the five seconds elapse, you dismiss the dialog by calling the dismss() method. c02.indd 49 25/01/12 12:14 PM
  • ❘ 50 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Displaying a More Sophisticated Progress Dialog Besides the generic “please wait” dialog that you created in the previous section, you can also create a dialog that displays the progress of an operation, such as the status of a download. The following Try It Out shows you how to display a specialized progress dialog. TRY IT OUT 1. Displaying the Progress of an Operation Using the same project created in the previous section, add the following lines in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn_dialog” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a dialog” android:onClick=”onClick” /> <Button android:id=”@+id/btn_dialog2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a progress dialog” android:onClick=”onClick2” /> <Button android:id=”@+id/btn_dialog3” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a detailed progress dialog” android:onClick=”onClick3” /> </LinearLayout> 2. Add the following statements in bold to the DialogActivity.java fi le: package net.learn2develop.Dialog; import import import import import import import import c02.indd 50 android.app.Activity; android.app.AlertDialog; android.app.AlertDialog.Builder; android.app.Dialog; android.app.ProgressDialog; android.content.DialogInterface; android.os.Bundle; android.view.View; 25/01/12 12:14 PM
  • Understanding Activities ❘ 51 import android.widget.Toast; public class DialogActivity extends Activity { CharSequence[] items = { “Google”, “Apple”, “Microsoft” }; boolean[] itemsChecked = new boolean [items.length]; ProgressDialog progressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { ... } public void onClick(View v) { ... } public void onClick2(View v) { ... } public void onClick3(View v) { showDialog(1); progressDialog.setProgress(0); new Thread(new Runnable(){ public void run(){ for (int i=1; i<=15; i++) { try { //---simulate doing something lengthy--Thread.sleep(1000); //---update the dialog--progressDialog.incrementProgressBy((int)(100/15)); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog.dismiss(); } }).start(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: return new AlertDialog.Builder(this) //... ).create(); case 1: progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle(“Downloading files...”); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, “OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) c02.indd 51 25/01/12 12:14 PM
  • 52 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS { Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “Cancel clicked!”, Toast.LENGTH_SHORT).show(); } }); return progressDialog; } return null; } } 3. Press F11 to debug the application on the Android emulator. Click the third button to display the progress dialog (see Figure 2-7). Note that the progress bar will count up to 100%. How It Works To create a dialog that shows the progress of an operation, you fi rst create an instance of the ProgressDialog class and set its various properties, such as icon, title, and style: FIGURE 2-7 progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle(“Downloading files...”); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); You then set the two buttons that you want to display inside the progress dialog: progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, “OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel”, new DialogInterface.OnClickListener() { c02.indd 52 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 53 public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), “Cancel clicked!”, Toast.LENGTH_SHORT).show(); } }); return progressDialog; The preceding code causes a progress dialog to appear (see Figure 2-8). To display the progress status in the progress dialog, you can use a Thread object to run a Runnable block of code: progressDialog.setProgress(0); FIGURE 2-8 new Thread(new Runnable(){ public void run(){ for (int i=1; i<=15; i++) { try { //---simulate doing something lengthy--Thread.sleep(1000); //---update the dialog--progressDialog.incrementProgressBy((int)(100/15)); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog.dismiss(); } }).start(); In this case, you want to count from 1 to 15, with a delay of one second between each number. The incrementProgressBy() method increments the counter in the progress dialog. When the progress dialog reaches 100%, it is dismissed. LINKING ACTIVITIES USING INTENTS An Android application can contain zero or more activities. When your application has more than one activity, you often need to navigate from one to another. In Android, you navigate between activities through what is known as an intent. The best way to understand this very important but somewhat abstract concept in Android is to experience it fi rsthand and see what it helps you to achieve. The following Try It Out shows how to add another activity to an existing project and then navigate between the two activities. c02.indd 53 25/01/12 12:14 PM
  • ❘ 54 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS TRY IT OUT Linking Activities with Intents codefile UsingIntent.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it UsingIntent. Right-click on the package name under the src folder and select New ➪ Class (see Figure 2-9). FIGURE 2-9 3. 4. Name the new class SecondActivity and click Finish. Add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.UsingIntent” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.UsingIntentActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:label=”Second Activity” android:name=”.SecondActivity” > c02.indd 54 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 55 <intent-filter > <action android:name=”net.learn2develop.SecondActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> </application> </manifest> 5. Make a copy of the main.xml fi le (in the res/layout folder) by right-clicking on it and selecting Copy. Then, right-click on the res/layout folder and select Paste. Name the fi le secondactivity.xml. The res/layout folder will now contain the secondactivity.xml fi le (see Figure 2-10). 6. Modify the secondactivity.xml fi le as follows: FIGURE 2-10 <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is the Second Activity!” /> </LinearLayout> 7. In the SecondActivity.java fi le, add the following statements in bold: package net.learn2develop.UsingIntent; import android.app.Activity; import android.os.Bundle; public class SecondActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); } } 8. Add the following lines in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button c02.indd 55 25/01/12 12:14 PM
  • 56 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Display second activity” android:onClick=”onClick”/> </LinearLayout> 9. Modify the UsingIntentActivity.java fi le as shown in bold: package net.learn2develop.UsingIntent; import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; public class UsingIntentActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { startActivity(new Intent(“net.learn2develop.SecondActivity”)); } } 10. Press F11 to debug the application on the Android emulator. When the fi rst activity is loaded, click the button and the second activity will now be loaded (see Figure 2-11). FIGURE 2-11 c02.indd 56 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 57 How It Works As you have learned, an activity is made up of a UI component (for example, main.xml) and a class component (for example, UsingIntentActivity.java). Hence, if you want to add another activity to a project, you need to create these two components. In the AndroidManifest.xml fi le, specifically you have added the following: <activity android:label=” Second Activity” android:name=”.SecondActivity” > <intent-filter > <action android:name=”net.learn2develop.SecondActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> Here, you have added a new activity to the application. Note the following: ➤ The name (class) of the new activity added is SecondActivity. ➤ The label for the new activity is named Second Activity. ➤ The intent filter name for the new activity is net.learn2develop.SecondActivity. Other activities that wish to call this activity will invoke it via this name. Ideally, you should use the reverse domain name of your company as the intent filter name in order to reduce the chances of another application having the same intent filter name. (The next section discusses what happens when two or more activities have the same intent filter.) ➤ The category for the intent filter is android.intent.category.DEFAULT. You need to add this to the intent filter so that this activity can be started by another activity using the startActivity() method (more on this shortly). When the button is clicked, you use the startActivity() method to display SecondActivity by creating an instance of the Intent class and passing it the intent fi lter name of SecondActivity (which is net.learn2develop.SecondActivity): public void onClick(View view) { startActivity(new Intent(“net.learn2develop.SecondActivity”)); } Activities in Android can be invoked by any application running on the device. For example, you can create a new Android project and then display SecondActivity by using its net.learn2develop .SecondActivity intent fi lter. This is one of the fundamental concepts in Android that enables an application to invoke another easily. If the activity that you want to invoke is defi ned within the same project, you can rewrite the preceding statement like this: startActivity(new Intent(this, SecondActivity.class)); However, this approach is applicable only when the activity you want to display is within the same project as the current activity. c02.indd 57 25/01/12 12:14 PM
  • 58 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Resolving Intent Filter Collision In the previous section, you learned that the <intent-filter> element defi nes how your activity can be invoked by another activity. What happens if another activity (in either the same or a separate application) has the same fi lter name? For example, suppose your application has another activity named Activity3, with the following entry in the AndroidManifest.xml fi le: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.UsingIntent” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.UsingIntentActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:label=”Second Activity” android:name=”.SecondActivity” > <intent-filter > <action android:name=”net.learn2develop.SecondActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> <activity android:label=”Third Activity” android:name=”.ThirdActivity” > <intent-filter > <action android:name=”net.learn2develop.SecondActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> </application> </manifest> If you call the startActivity() method with the following intent, then the Android OS will display a selection of activities, as shown in Figure 2-12: startActivity(new Intent(“net.learn2develop.SecondActivity”)); c02.indd 58 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 59 If you check the “Use by default for this action” item and then select an activity, then the next time the intent “net.learn2develop.SecondActivity” is called again, it will launch the previous activity that you have selected. To clear this default, go to the Settings application in Android and select Apps ➪ Manage applications, and then select the application name (see Figure 2-13). When the details of the application are shown, scroll down to the bottom and click the Clear defaults button. FIGURE 2-12 FIGURE 2-13 Returning Results from an Intent The startActivity() method invokes another activity but does not return a result to the current activity. For example, you may have an activity that prompts the user for user name and password. The information entered by the user in that activity needs to be passed back to the calling activity for further processing. If you need to pass data back from an activity, you should instead use the startActivityForResult() method. The following Try It Out demonstrates this. TRY IT OUT 1. Obtaining a Result from an Activity Using the same project from the previous section, add the following statements in bold to the secondactivity.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” c02.indd 59 25/01/12 12:14 PM
  • 60 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is the Second Activity!” /> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Please enter your name” /> <EditText android:id=”@+id/txt_username” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <Button android:id=”@+id/btn_OK” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”OK” android:onClick=”onClick”/> </LinearLayout> 2. Add the following statements in bold to SecondActivity.java: package net.learn2develop.UsingIntent; import import import import import import android.app.Activity; android.content.Intent; android.net.Uri; android.os.Bundle; android.view.View; android.widget.EditText; public class SecondActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); } public void onClick(View view) { Intent data = new Intent(); //---get the EditText view--EditText txt_username = (EditText) findViewById(R.id.txt_username); //---set the data to pass back--data.setData(Uri.parse( c02.indd 60 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 61 txt_username.getText().toString())); setResult(RESULT_OK, data); //---closes the activity--finish(); } } 3. Add the following statements in bold to the UsingIntentActivity.java fi le: package net.learn2develop.UsingIntent; import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Toast; public class UsingIntentActivity extends Activity { int request_Code = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { //startActivity(new Intent(“net.learn2develop.SecondActivity”)); //or //startActivity(new Intent(this, SecondActivity.class)); startActivityForResult(new Intent( “net.learn2develop.SecondActivity”), request_Code); } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == request_Code) { if (resultCode == RESULT_OK) { Toast.makeText(this,data.getData().toString(), Toast.LENGTH_SHORT).show(); } } } } 4. c02.indd 61 Press F11 to debug the application on the Android emulator. When the first activity is loaded, click the button. SecondActivity will now be loaded. Enter your name (see Figure 2-14) and click the OK button. The first activity will display the name you have entered using the Toast class. 25/01/12 12:14 PM
  • 62 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS FIGURE 2-14 How It Works To call an activity and wait for a result to be returned from it, you need to use the startActivityForResult() method, like this: startActivityForResult(new Intent( “net.learn2develop.SecondActivity”), request_Code); In addition to passing in an Intent object, you need to pass in a request code as well. The request code is simply an integer value that identifies an activity you are calling. This is needed because when an activity returns a value, you must have a way to identify it. For example, you may be calling multiple activities at the same time, and some activities may not return immediately (for example, waiting for a reply from a server). When an activity returns, you need this request code to determine which activity is actually returned. NOTE If the request code is set to -1, then calling it using the startActivityForResult() method is equivalent to calling it using the startActivity() method. That is, no result will be returned. In order for an activity to return a value to the calling activity, you use an Intent object to send data back via the setData() method: Intent data = new Intent(); //---get the EditText view--- c02.indd 62 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 63 EditText txt_username = (EditText) findViewById(R.id.txt_username); //---set the data to pass back--data.setData(Uri.parse( txt_username.getText().toString())); setResult(RESULT_OK, data); //---closes the activity--finish(); The setResult() method sets a result code (either RESULT_OK or RESULT_CANCELLED) and the data (an Intent object) to be returned back to the calling activity. The finish() method closes the activity and returns control back to the calling activity. In the calling activity, you need to implement the onActivityResult() method, which is called whenever an activity returns: public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == request_Code) { if (resultCode == RESULT_OK) { Toast.makeText(this,data.getData().toString(), Toast.LENGTH_SHORT).show(); } } } Here, you check for the appropriate request and result codes and display the result that is returned. The returned result is passed in via the data argument; and you obtain its details through the getData() method. Passing Data Using an Intent Object Besides returning data from an activity, it is also common to pass data to an activity. For example, in the previous example you may want to set some default text in the EditText view before the activity is displayed. In this case, you can use the Intent object to pass the data to the target activity. The following Try It Out shows you the various ways in which you can pass data between activities. TRY IT OUT 1. 2. Passing Data to the Target Activity Using Eclipse, create a new Android project and name it PassingData. Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” c02.indd 63 25/01/12 12:14 PM
  • 64 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS android:orientation=”vertical” > <Button android:id=”@+id/btn_SecondActivity” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to go to Second Activity” android:onClick=”onClick”/> </LinearLayout> 3. Add a new XML fi le to the res/layout folder and name it secondactivity.xml. Populate it as follows: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Welcome to Second Activity” /> <Button android:id=”@+id/btn_MainActivity” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to return to main activity” android:onClick=”onClick”/> </LinearLayout> 4. Add a new Class fi le to the package and name it SecondActivity. Populate the SecondActivity .java fi le as follows: package net.learn2develop.PassingData; import import import import import import android.app.Activity; android.content.Intent; android.net.Uri; android.os.Bundle; android.view.View; android.widget.Toast; public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); //---get the data passed in using getStringExtra()--Toast.makeText(this,getIntent().getStringExtra(“str1”), Toast.LENGTH_SHORT).show(); //---get the data passed in using getIntExtra()--- c02.indd 64 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 65 Toast.makeText(this,Integer.toString( getIntent().getIntExtra(“age1”, 0)), Toast.LENGTH_SHORT).show(); //---get the Bundle object passed in--Bundle bundle = getIntent().getExtras(); //---get the data using the getString()--Toast.makeText(this, bundle.getString(“str2”), Toast.LENGTH_SHORT).show(); //---get the data using the getInt() method--Toast.makeText(this,Integer.toString(bundle.getInt(“age2”)), Toast.LENGTH_SHORT).show(); } public void onClick(View view) { //---use an Intent object to return data--Intent i = new Intent(); //---use the putExtra() method to return some // value--i.putExtra(“age3”, 45); //---use the setData() method to return some value--i.setData(Uri.parse( “Something passed back to main activity”)); //---set the result with OK and the Intent object--setResult(RESULT_OK, i); //---destroy the current activity--finish(); } } 5. Add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.PassingData” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.PassingDataActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> c02.indd 65 25/01/12 12:14 PM
  • 66 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS <activity android:label=”Second Activity” android:name=”.SecondActivity” > <intent-filter > <action android:name=”net.learn2develop.PassingDataSecondActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> </application> </manifest> 6. Add the following statements in bold to the PassingDataActivity.java fi le: package net.learn2develop.PassingData; import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Toast; public class PassingDataActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { Intent i = new Intent(“net.learn2develop.PassingDataSecondActivity”); //---use putExtra() to add new name/value pairs--i.putExtra(“str1”, “This is a string”); i.putExtra(“age1”, 25); //---use a Bundle object to add new name/values // pairs--Bundle extras = new Bundle(); extras.putString(“str2”, “This is another string”); extras.putInt(“age2”, 35); //---attach the Bundle object to the Intent object--i.putExtras(extras); //---start the activity to get a result back--startActivityForResult(i, 1); } public void onActivityResult(int requestCode, int resultCode, Intent data) { //---check if the request code is 1--if (requestCode == 1) { //---if the result is OK--- c02.indd 66 25/01/12 12:14 PM
  • Linking Activities Using Intents ❘ 67 if (resultCode == RESULT_OK) { //---get the result using getIntExtra()--Toast.makeText(this, Integer.toString( data.getIntExtra(“age3”, 0)), Toast.LENGTH_SHORT).show(); //---get the result using getData()--Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show(); } } } } 7. Press F11 to debug the application on the Android emulator. Click the button on each activity and observe the values displayed. How It Works While this application is not visually exciting, it does illustrate some important ways to pass data between activities. First, you can use the putExtra() method of an Intent object to add a name/value pair: //---use putExtra() to add new name/value pairs--i.putExtra(“str1”, “This is a string”); i.putExtra(“age1”, 25); The preceding statements add two name/value pairs to the Intent object: one of type string and one of type integer. Besides using the putExtra() method, you can also create a Bundle object and then attach it using the putExtras() method. Think of a Bundle object as a dictionary object — it contains a set of name/ value pairs. The following statements create a Bundle object and then add two name/value pairs to it. It is then attached to the Intent object: //---use a Bundle object to add new name/values pairs--Bundle extras = new Bundle(); extras.putString(“str2”, “This is another string”); extras.putInt(“age2”, 35); //---attach the Bundle object to the Intent object--i.putExtras(extras); On the second activity, to obtain the data sent using the Intent object, you fi rst obtain the Intent object using the getIntent() method. Then, call its getStringExtra() method to get the string value set using the putExtra() method: //---get the data passed in using getStringExtra()--Toast.makeText(this,getIntent().getStringExtra(“str1”), Toast.LENGTH_SHORT).show(); c02.indd 67 25/01/12 12:14 PM
  • 68 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS In this case, you have to call the appropriate method to extract the name/value pair based on the type of data set. For the integer value, use the getIntExtra() method (the second argument is the default value in case no value is stored in the specified name): //---get the data passed in using getIntExtra()--Toast.makeText(this,Integer.toString( getIntent().getIntExtra(“age1”, 0)), Toast.LENGTH_SHORT).show(); To retrieve the Bundle object, use the getExtras() method: //---get the Bundle object passed in--Bundle bundle = getIntent().getExtras(); To get the individual name/value pairs, use the appropriate method. For the string value, use the getString() method: //---get the data using the getString()--Toast.makeText(this, bundle.getString(“str2”), Toast.LENGTH_SHORT).show(); Likewise, use the getInt() method to retrieve an integer value: //---get the data using the getInt() method--Toast.makeText(this,Integer.toString(bundle.getInt(“age2”)), Toast.LENGTH_SHORT).show(); Another way to pass data to an activity is to use the setData() method (as used in the previous section), like this: //---use the setData() method to return some value--i.setData(Uri.parse( “Something passed back to main activity”)); Usually, you use the setData() method to set the data on which an Intent object is going to operate (such as passing a URL to an Intent object so that it can invoke a web browser to view a web page; see the section “Calling Built-In Applications Using Intents” later in this chapter for more examples). To retrieve the data set using the setData() method, use the getData() method (in this example data is an Intent object): //---get the result using getData()--Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show(); c02.indd 68 25/01/12 12:14 PM
  • Fragments ❘ 69 FRAGMENTS In the previous section you learned what an activity is and how to use it. In a small-screen device (such as a smartphone), an activity typically fi lls the entire screen, displaying the various views that make up the user interface of an application. The activity is essentially a container for views. However, when an activity is displayed in a large-screen device, such as on a tablet, it is somewhat out of place. Because the screen is much bigger, all the views in an activity must be arranged to make full use of the increased space, resulting in complex changes to the view hierarchy. A better approach is to have “mini-activities,” each containing its own set of views. During runtime, an activity can contain one or more of these mini-activities, depending on the screen orientation in which the device is held. In Android 3.0 and later, these mini-activities are known as fragments. Think of a fragment as another form of activity. You create fragments to contain views, just like activities. Fragments are always embedded in an activity. For example, Figure 2-15 shows two fragments. Fragment 1 might contain a ListView showing a list of book titles. Fragment 2 might contain some TextViews and ImageViews showing some text and images. Now imagine the application is running on an Android tablet in portrait mode (or on an Android smartphone). In this case, Fragment 1 may be embedded in one activity, while Fragment 2 may be embedded in another activity (see Figure 2-16). When users select an item in the list in Fragment 1, Activity 2 will be started. Fragment 1 Fragment 2 Fragment 1 Activity 1 FIGURE 2-15 Fragment 2 Activity 2 FIGURE 2-16 If the application is now displayed in a tablet in landscape mode, both fragments can be embedded within a single activity, as shown in Figure 2-17. Fragment 1 Fragment 2 From this discussion, it becomes apparent that fragments present a versatile way in which you can create the user interface of an Android application. Fragments form the atomic unit of your user interface, and they can be dynamically added (or removed) to activities in order to create the best user experience possible for the target device. The following Try It Out shows you the basics of working with fragments. c02.indd 69 Activity 1 FIGURE 2-17 25/01/12 12:14 PM
  • ❘ 70 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS TRY IT OUT Using Fragments codefile Fragments.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Fragments. In the res/layout folder, add a new file and name it fragment1.xml. Populate it with the following: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”#00FF00” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is fragment #1” android:textColor=”#000000” android:textSize=”25sp” /> </LinearLayout> 3. Also in the res/layout folder, add another new fi le and name it fragment2.xml. Populate it as follows: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”#FFFE00” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is fragment #2” android:textColor=”#000000” android:textSize=”25sp” /> </LinearLayout> 4. In main.xml, add the following code in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <fragment android:name=”net.learn2develop.Fragments.Fragment1” android:id=”@+id/fragment1” c02.indd 70 25/01/12 12:14 PM
  • Fragments ❘ 71 android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> <fragment android:name=”net.learn2develop.Fragments.Fragment2” android:id=”@+id/fragment2” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> </LinearLayout> 5. 6. Under the net.learn2develop.Fragments package name, add two Java class fi les and name them Fragment1.java and Fragment2 .java (see Figure 2-18). FIGURE 2-18 Add the following code to Fragment1.java: package net.learn2develop.Fragments; import import import import import android.app.Fragment; android.os.Bundle; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //---Inflate the layout for this fragment--return inflater.inflate( R.layout.fragment1, container, false); } } 7. Add the following code to Fragment2.java: package net.learn2develop.Fragments; import import import import import android.app.Fragment; android.os.Bundle; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //---Inflate the layout for this fragment--return inflater.inflate( R.layout.fragment2, container, false); } } c02.indd 71 25/01/12 12:14 PM
  • 72 ❘ 8. CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Press F11 to debug the application on the Android emulator. Figure 2-19 shows the two fragments contained within the activity. FIGURE 2-19 How It Works A fragment behaves very much like an activity — it has a Java class and it loads its UI from an XML fi le. The XML fi le contains all the usual UI elements that you expect from an activity: TextView, EditText, Button, and so on. The Java class for a fragment needs to extend the Fragment base class: public class Fragment1 extends Fragment { } NOTE Besides the Fragment base class, a fragment can also extend a few other subclasses of the Fragment class, such as DialogFragment, ListFragment, and PreferenceFragment. Chapter 4 discusses these types of fragments in more detail. To draw the UI for a fragment, you override the onCreateView() method. This method needs to return a View object, like this: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //---Inflate the layout for this fragment--return inflater.inflate( c02.indd 72 25/01/12 12:14 PM
  • Fragments ❘ 73 R.layout.fragment1, container, false); } Here, you use a LayoutInflater object to inflate the UI from the specified XML fi le (R.layout .fragment1 in this case). The container argument refers to the parent ViewGroup, which is the activity in which you are trying to embed the fragment. The savedInstanceState argument enables you to restore the fragment to its previously saved state. To add a fragment to an activity, you use the <fragment> element: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <fragment android:name=”net.learn2develop.Fragments.Fragment1” android:id=”@+id/fragment1” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> <fragment android:name=”net.learn2develop.Fragments.Fragment2” android:id=”@+id/fragment2” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> </LinearLayout> Note that each fragment needs a unique identifier. You can assign one via the android:id or android:tag attribute. Adding Fragments Dynamically While fragments enable you to compartmentalize your UI into various configurable parts, the real power of fragments is realized when you add them dynamically to activities during runtime. In the previous section, you saw how you can add fragments to an activity by modifying the XML fi le during design time. In reality, it is much more useful if you create fragments and add them to activities during runtime. This enables you to create a customizable user interface for your application. For example, if the application is running on a smartphone, you might fi ll an activity with a single fragment; if the application is running on a tablet, you might then fill the activity with two or more fragments, as the tablet has much more screen real estate compared to a smartphone. The following Try It Out shows how fragments can be added programmatically to an activity during runtime. c02.indd 73 25/01/12 12:14 PM
  • ❘ 74 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS TRY IT OUT 1. Adding Fragments during Runtime Using the same project created in the previous section, modify the main.xml fi le by commenting out the two <fragment> elements: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <!-<fragment android:name=”net.learn2develop.Fragments.Fragment1” android:id=”@+id/fragment1” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> <fragment android:name=”net.learn2develop.Fragments.Fragment2” android:id=”@+id/fragment2” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> --> </LinearLayout> 2. Add the following code in bold to the FragmentsActivity.java fi le: package net.learn2develop.Fragments; import import import import import import android.app.Activity; android.app.FragmentManager; android.app.FragmentTransaction; android.os.Bundle; android.view.Display; android.view.WindowManager; public class FragmentsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //---get the current display info--WindowManager wm = getWindowManager(); Display d = wm.getDefaultDisplay(); if (d.getWidth() > d.getHeight()) { //---landscape mode--- c02.indd 74 25/01/12 12:14 PM
  • Fragments ❘ 75 Fragment1 fragment1 = new Fragment1(); // android.R.id.content refers to the content // view of the activity fragmentTransaction.replace( android.R.id.content, fragment1); } else { //---portrait mode--Fragment2 fragment2 = new Fragment2(); fragmentTransaction.replace( android.R.id.content, fragment2); } fragmentTransaction.commit(); } } 3. Press F11 to run the application on the Android emulator. Observe that when the emulator is in portrait mode, fragment 2 (yellow) is displayed (see Figure 2-20). If you press Ctrl+F11 to change the orientation of the emulator to landscape, fragment 1 (green) is shown instead (see Figure 2-21). FIGURE 2-20 FIGURE 2-21 How It Works To add fragments to an activity, you use the FragmentManager class by fi rst obtaining an instance of it: FragmentManager fragmentManager = getFragmentManager(); c02.indd 75 25/01/12 12:14 PM
  • ❘ 76 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS You also need to use the FragmentTransaction class to perform fragment transactions in your activity (such as add, remove or replace): FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); In this example, you used the WindowManager to determine whether the device is currently in portrait mode or landscape mode. Once that is determined, you add the appropriate fragment to the activity by creating the fragment and then calling the replace() method of the FragmentTransaction object to add the fragment to the specified view container (in this case, android.R.id.content refers to the content view of the activity): //---landscape mode--Fragment1 fragment1 = new Fragment1(); // android.R.id.content refers to the content // view of the activity fragmentTransaction.replace( android.R.id.content, fragment1); Using the replace() method is essentially the same as calling the remove() method followed by the add() method of the FragmentTransaction object. To ensure that the changes take effect, you need to call the commit() method: fragmentTransaction.commit(); Life Cycle of a Fragment Like activities, fragments have their own life cycle. Understanding the life cycle of a fragment enables you to properly save an instance of the fragment when it is destroyed, and restore it to its previous state when it is recreated. The following Try It Out examines the various states experienced by a fragment. TRY IT OUT Understanding the Life Cycle of a Fragment codefile Fragments.zip available for download at Wrox.com 1. Using the same project created in the previous section, add the following code in bold to the Fragment1.java fi le: package net.learn2develop.Fragments; import import import import import import c02.indd 76 android.app.Activity; android.app.Fragment; android.os.Bundle; android.util.Log; android.view.LayoutInflater; android.view.View; 25/01/12 12:14 PM
  • Fragments ❘ 77 import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(“Fragment 1”, “onCreateView”); //---Inflate the layout for this fragment--return inflater.inflate( R.layout.fragment1, container, false); } @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.d(“Fragment 1”, “onAttach”); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(“Fragment 1”, “onCreate”); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d(“Fragment 1”, “onActivityCreated”); } @Override public void onStart() { super.onStart(); Log.d(“Fragment 1”, “onStart”); } @Override public void onResume() { super.onResume(); Log.d(“Fragment 1”, “onResume”); } @Override public void onPause() { super.onPause(); Log.d(“Fragment 1”, “onPause”); } @Override public void onStop() { super.onStop(); Log.d(“Fragment 1”, “onStop”); c02.indd 77 25/01/12 12:14 PM
  • 78 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS } @Override public void onDestroyView() { super.onDestroyView(); Log.d(“Fragment 1”, “onDestroyView”); } @Override public void onDestroy() { super.onDestroy(); Log.d(“Fragment 1”, “onDestroy”); } @Override public void onDetach() { super.onDetach(); Log.d(“Fragment 1”, “onDetach”); } } 2. 3. 4. Switch the Android emulator to landscape mode by pressing Ctrl+F11. Press F11 in Eclipse to debug the application on the Android emulator. When the application is loaded on the emulator, the following is displayed in the LogCat window (Windows ➪ Show View ➪ LogCat): 12-09 12-09 12-09 12-09 12-09 12-09 5. 04:17:43.436: 04:17:43.466: 04:17:43.476: 04:17:43.506: 04:17:43.506: 04:17:43.537: D/Fragment D/Fragment D/Fragment D/Fragment D/Fragment D/Fragment 1(2995): 1(2995): 1(2995): 1(2995): 1(2995): 1(2995): onAttach onCreate onCreateView onActivityCreated onStart onResume Click the Home button on the emulator. The following output will be displayed in the LogCat window: 12-09 04:18:47.696: D/Fragment 1(2995): onPause 12-09 04:18:50.346: D/Fragment 1(2995): onStop 6. On the emulator, click the Home button and hold it. Launch the application again. This time, the following is displayed: 12-09 04:20:08.726: D/Fragment 1(2995): onStart 12-09 04:20:08.766: D/Fragment 1(2995): onResume 7. Finally, click the Back button on the emulator. Now you should see the following output: 12-09 04:21:01.426: D/Fragment 1(2995): onPause 12-09 04:21:02.346: D/Fragment 1(2995): onStop 12-09 04:21:02.346: D/Fragment 1(2995): onDestroyView c02.indd 78 25/01/12 12:14 PM
  • Fragments ❘ 79 12-09 04:21:02.346: D/Fragment 1(2995): onDestroy 12-09 04:21:02.346: D/Fragment 1(2995): onDetach How It Works Like activities, fragments in Android also have their own life cycle. As you have seen, when a fragment is being created, it goes through the following states: ➤ onAttach() ➤ onCreate() ➤ onCreateView() ➤ onActivityCreated() When the fragment becomes visible, it goes through these states: ➤ onStart() ➤ onResume() When the fragment goes into the background mode, it goes through these states: ➤ onPause() ➤ onStop() When the fragment is destroyed (when the activity it is currently hosted in is destroyed), it goes through the following states: ➤ onPause() ➤ onStop() ➤ onDestroyView() ➤ onDestroy() ➤ onDetach() Like activities, you can restore an instance of a fragment using a Bundle object, in the following states: ➤ onCreate() ➤ onCreateView() ➤ onActivityCreated() NOTE You can save a fragment’s state in the onSaveInstanceState() method. Chapter 3 discusses this topic in more detail. Most of the states experienced by a fragment are similar to those of activities. However, a few new states are specific to fragments: ➤ ➤ c02.indd 79 onAttached() — Called when the fragment has been associated with the activity onCreateView() — Called to create the view for the fragment 25/01/12 12:14 PM
  • 80 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS ➤ onActivityCreated() — Called when the activity’s onCreate() method has been returned ➤ onDestroyView() — Called when the fragment’s view is being removed ➤ onDetach() — Called when the fragment is detached from the activity Note one of the main differences between activities and fragments: When an activity goes into the background, the activity is placed in the back stack. This allows the activity to be resumed when the user presses the Back button. In the case of fragments, however, they are not automatically placed in the back stack when they go into the background. Rather, to place a fragment into the back stack, you need to explicitly call the addToBackStack() method during a fragment transaction, like this: //---get the current display info--WindowManager wm = getWindowManager(); Display d = wm.getDefaultDisplay(); if (d.getWidth() > d.getHeight()) { //---landscape mode--Fragment1 fragment1 = new Fragment1(); // android.R.id.content refers to the content // view of the activity fragmentTransaction.replace( android.R.id.content, fragment1); } else { //---portrait mode--Fragment2 fragment2 = new Fragment2(); fragmentTransaction.replace( android.R.id.content, fragment2); } //---add to the back stack--fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); The preceding code ensures that after the fragment has been added to the activity, the user can click the Back button to remove it. Interactions between Fragments Very often, an activity may contain one or more fragments working together to present a coherent UI to the user. In this case, it is very important for fragments to communicate with one another and exchange data. For example, one fragment might contain a list of items (such as postings from an RSS feed) and when the user taps on an item in that fragment, details about the selected item may be displayed in another fragment. The following Try It Out shows how one fragment can access the views contained within another fragment. c02.indd 80 25/01/12 12:14 PM
  • Fragments TRY IT OUT 1. ❘ 81 Communication between Fragments Using the same project created in the previous section, add the following statement in bold to the Fragment1.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”#00FF00” > <TextView android:id=”@+id/lblFragment1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is fragment #1” android:textColor=”#000000” android:textSize=”25sp” /> </LinearLayout> 2. Add the following lines in bold to fragment2.xml: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”#FFFE00” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is fragment #2” android:textColor=”#000000” android:textSize=”25sp” /> <Button android:id=”@+id/btnGetText” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Get text in Fragment #1” android:textColor=”#000000” android:onClick=”onClick” /> </LinearLayout> 3. Put back the two fragments in main.xml: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” c02.indd 81 25/01/12 12:14 PM
  • 82 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <fragment android:name=”net.learn2develop.Fragments.Fragment1” android:id=”@+id/fragment1” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> <fragment android:name=”net.learn2develop.Fragments.Fragment2” android:id=”@+id/fragment2” android:layout_weight=”1” android:layout_width=”0px” android:layout_height=”match_parent” /> </LinearLayout> 4. Modify the FragmentsActivity.java fi le by commenting out the code that you added in the earlier sections. It should look like this after modification: public class FragmentsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //---get the current display info--WindowManager wm = getWindowManager(); Display d = wm.getDefaultDisplay(); if (d.getWidth() > d.getHeight()) { //---landscape mode--Fragment1 fragment1 = new Fragment1(); // android.R.id.content refers to the content // view of the activity fragmentTransaction.replace( android.R.id.content, fragment1); } else { //---portrait mode--Fragment2 fragment2 = new Fragment2(); fragmentTransaction.replace( android.R.id.content, fragment2); } c02.indd 82 25/01/12 12:14 PM
  • Fragments ❘ 83 //---add to the back stack--fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); */ } } 5. Add the following statements in bold to the Fragment2.java fi le: package net.learn2develop.Fragments; import import import import import import import import android.app.Fragment; android.os.Bundle; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; android.widget.Button; android.widget.TextView; android.widget.Toast; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //---Inflate the layout for this fragment--return inflater.inflate( R.layout.fragment2, container, false); } @Override public void onStart() { super.onStart(); //---Button view--Button btnGetText = (Button) getActivity().findViewById(R.id.btnGetText); btnGetText.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView lbl = (TextView) getActivity().findViewById(R.id.lblFragment1); Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT).show(); } }); } } 6. c02.indd 83 Press F11 to debug the application on the Android emulator. In the second fragment on the right, click the button. You should see the Toast class displaying the text “This is fragment #1” (see Figure 2-22). 25/01/12 12:14 PM
  • 84 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS FIGURE 2-22 How It Works Because fragments are embedded within activities, you can obtain the activity in which a fragment is currently embedded by fi rst using the getActivity() method and then using the findViewById() method to locate the view(s) contained within the fragment: TextView lbl = (TextView) getActivity().findViewById(R.id.lblFragment1); Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT).show(); The getActivity() method returns the activity with which the current fragment is currently associated. Alternatively, you can also add the following method to the FragmentsActivity.java fi le: public void onClick(View v) { TextView lbl = (TextView) findViewById(R.id.lblFragment1); Toast.makeText(this, lbl.getText(), Toast.LENGTH_SHORT).show(); } c02.indd 84 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents ❘ 85 CALLING BUILT-IN APPLICATIONS USING INTENTS Until this point, you have seen how to call activities within your own application. One of the key aspects of Android programming is using the intent to call activities from other applications. In particular, your application can call the many built-in applications that are included with an Android device. For example, if your application needs to load a web page, you can use the Intent object to invoke the built-in web browser to display the web page, instead of building your own web browser for this purpose. The following Try It Out demonstrates how to call some of the built-in applications commonly found on an Android device. TRY IT OUT Calling Built-In Applications Using Intents codefile Intents.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Intents. Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn_webbrowser” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Web Browser” android:onClick=”onClickWebBrowser” /> <Button android:id=”@+id/btn_makecalls” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Make Calls” android:onClick=”onClickMakeCalls” /> <Button android:id=”@+id/btn_showMap” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Show Map” android:onClick=”onClickShowMap” /> </LinearLayout> c02.indd 85 25/01/12 12:14 PM
  • 86 ❘ 3. CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Add the following statements in bold to the IntentsActivity.java fi le: package net.learn2develop.Intents; import import import import import android.app.Activity; android.content.Intent; android.net.Uri; android.os.Bundle; android.view.View; public class IntentsActivity extends Activity { int request_Code = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClickWebBrowser(View view) { Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); startActivity(i); } public void onClickMakeCalls(View view) { Intent i = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse(“tel:+651234567”)); startActivity(i); } public void onClickShowMap(View view) { Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“geo:37.827500,-122.481670”)); startActivity(i); } } 4. 5. 6. c02.indd 86 Press F11 to debug the application on the Android emulator. Click the Web Browser button to load the Browser application on the emulator. Figure 2-23 shows the built-in Browser application displaying the site www.amazon.com. Click the Make Calls button and the Phone application, as shown in Figure 2-24, will load. 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents FIGURE 2-24 FIGURE 2-23 7. ❘ 87 Similarly, to load the Maps application, shown in Figure 2-25, click the Show Map button. NOTE In order to display the Maps application, you need to run the application on an AVD that supports the Google APIs. How It Works In this example, you saw how you can use the Intent class to invoke some of the built-in applications in Android (such as Maps, Phone, Contacts, and Browser). In Android, intents usually come in pairs: action and data. The action describes what is to be performed, such as editing an item, viewing the content of an item, and so on. The data specifies what is affected, such as a person in the Contacts database. The data is specified as an Uri object. Some examples of action are as follows: ➤ ➤ ACTION_DIAL ➤ c02.indd 87 ACTION_VIEW ACTION_PICK FIGURE 2-25 25/01/12 12:14 PM
  • 88 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Some examples of data include the following: ➤ www.google.com ➤ tel:+651234567 ➤ geo:37.827500,-122.481670 ➤ content://contacts NOTE The section “Using Intent Filters” explains the type of data you can define for use in an activity. Collectively, the action and data pair describes the operation to be performed. For example, to dial a phone number, you would use the pair ACTION_DIAL/tel:+651234567. To display a list of contacts stored in your phone, you use the pair ACTION_VIEW/content://contacts. To pick a contact from the list of contacts, you use the pair ACTION_PICK/content://contacts. In the fi rst button, you create an Intent object and then pass two arguments to its constructor, the action and the data: Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); startActivity(i); The action here is represented by the android.content.Intent.ACTION_VIEW constant. You use the parse() method of the Uri class to convert a URL string into a Uri object. The android.content.Intent.ACTION_VIEW constant actually refers to the “android.intent .action.VIEW” action, so the preceding could be rewritten as follows: Intent i = new Intent(“android.intent.action.VIEW”, Uri.parse(“http://www.amazon.com”)); startActivity(i); The preceding code snippet can also be rewritten like this: Intent i = new Intent(“android.intent.action.VIEW”); i.setData(Uri.parse(“http://www.amazon.com”)); startActivity(i); Here, you set the data separately using the setData() method. For the second button, you dial a specific number by passing in the telephone number in the data portion: Intent i = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse(“tel:+651234567”)); startActivity(i); c02.indd 88 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents ❘ 89 In this case, the dialer will display the number to be called. The user must still press the dial button to dial the number. If you want to directly call the number without user intervention, change the action as follows: Intent i = new Intent(android.content.Intent.ACTION_CALL, Uri.parse(“tel:+651234567”)); startActivity(i); NOTE If you want your application to directly call the specified number, you need to add the android.permission.CALL_PHONE permission to your application. To display the dialer without specifying any number, simply omit the data portion, like this: Intent i = new Intent(android.content.Intent.ACTION_DIAL); startActivity(i); The third button displays a map using the ACTION_VIEW constant: Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“geo:37.827500,-122.481670”)); startActivity(i); Here, instead of using “http” you use the “geo” scheme. Understanding the Intent Object So far, you have seen the use of the Intent object to call other activities. This is a good time to recap and gain a more detailed understanding of how the Intent object performs its magic. First, you learned that you can call another activity by passing its action to the constructor of an Intent object: startActivity(new Intent(“net.learn2develop.SecondActivity”)); The action (in this example “net.learn2develop.SecondActivity”) is also known as the component name. This is used to identify the target activity/application that you want to invoke. You can also rewrite the component name by specifying the class name of the activity if it resides in your project, like this: startActivity(new Intent(this, SecondActivity.class)); c02.indd 89 25/01/12 12:14 PM
  • 90 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS You can also create an Intent object by passing in an action constant and data, such as the following: Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); startActivity(i); The action portion defines what you want to do, while the data portion contains the data for the target activity to act upon. You can also pass the data to the Intent object using the setData() method: Intent i = new Intent(“android.intent.action.VIEW”); i.setData(Uri.parse(“http://www.amazon.com”)); In this example, you indicate that you want to view a web page with the specified URL. The Android OS will look for all activities that are able to satisfy your request. This process is known as intent resolution. The next section discusses in more detail how your activities can be the target of other activities. For some intents, there is no need to specify the data. For example, to select a contact from the Contacts application, you specify the action and then indicate the MIME type using the setType() method: Intent i = new Intent(android.content.Intent.ACTION_PICK); i.setType(ContactsContract.Contacts.CONTENT_TYPE); NOTE Chapter 7 discusses how to use the Contacts application from within your application. The setType() method explicitly specifies the MIME data type to indicate the type of data to return. The MIME type for ContactsContract.Contacts.CONTENT_TYPE is “vnd.android .cursor.dir/contact”. Besides specifying the action, the data, and the type, an Intent object can also specify a category. A category groups activities into logical units so that Android can use it for further fi ltering. The next section discusses categories in more detail. To summarize, an Intent object can contain the following information: ➤ ➤ Data ➤ Type ➤ c02.indd 90 Action Category 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents ❘ 91 Using Intent Filters Earlier, you saw how an activity can invoke another activity using the Intent object. In order for other activities to invoke your activity, you need to specify the action and category within the <intent-filter> element in the AndroidManifest.xml fi le, like this: <intent-filter > <action android:name=”net.learn2develop.SecondActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> This is a very simple example in which one activity calls another using the “net.learn2develop .SecondActivity” action. The following Try It Out shows you a more sophisticated example. TRY IT OUT Specifying Intent Filters in More Detail 1. Using the Intents project created earlier, add a new class to the project and name it MyBrowserActivity. Also add a new XML file to the res/layout folder and name it browser.xml. 2. Add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.Intents” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <uses-permission android:name=”android.permission.CALL_PHONE”/> <uses-permission android:name=”android.permission.INTERNET”/> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.IntentsActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:name=”.MyBrowserActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <action android:name=”net.learn2develop.MyBrowser” /> <category android:name=”android.intent.category.DEFAULT” /> <data android:scheme=”http” /> </intent-filter> </activity> </application> </manifest> c02.indd 91 25/01/12 12:14 PM
  • 92 ❘ 3. CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn_webbrowser” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Web Browser” android:onClick=”onClickWebBrowser” /> <Button android:id=”@+id/btn_makecalls” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Make Calls” android:onClick=”onClickMakeCalls” /> <Button android:id=”@+id/btn_showMap” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Show Map” android:onClick=”onClickShowMap” /> <Button android:id=”@+id/btn_launchMyBrowser” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Launch My Browser” android:onClick=”onClickLaunchMyBrowser” /> </LinearLayout> 4. Add the following statements in bold to the IntentsActivity.java fi le: package net.learn2develop.Intents; import import import import import android.app.Activity; android.content.Intent; android.net.Uri; android.os.Bundle; android.view.View; public class IntentsActivity extends Activity { int request_Code = 1; /** Called when the activity is first created. */ c02.indd 92 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents ❘ 93 @Override public void onCreate(Bundle savedInstanceState) { … } public void onClickWebBrowser(View view) { … } public void onClickMakeCalls(View view) { ... } public void onClickShowMap(View view) { ... } public void onClickLaunchMyBrowser(View view) { Intent i = new Intent(“net.learn2develop.MyBrowser”); i.setData(Uri.parse(“http://www.amazon.com”)); startActivity(i); } } 5. Add the following statements in bold to the browser.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” > <WebView android:id=”@+id/WebView01” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 6. Add the following statements in bold to the MyBrowserActivity.java fi le: package net.learn2develop.Intents; import import import import import android.app.Activity; android.net.Uri; android.os.Bundle; android.webkit.WebView; android.webkit.WebViewClient; public class MyBrowserActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browser); Uri url = getIntent().getData(); WebView webView = (WebView) findViewById(R.id.WebView01); webView.setWebViewClient(new Callback()); c02.indd 93 25/01/12 12:14 PM
  • 94 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS webView.loadUrl(url.toString()); } private class Callback extends WebViewClient { @Override public boolean shouldOverrideUrlLoading (WebView view, String url) { return(false); } } } 7. 8. Press F11 to debug the application on the Android emulator. Click the Launch my Browser button and you should see the new activity displaying the Amazon.com web page (see Figure 2-26). How It Works In this example, you created a new activity named MyBrowserActivity. You fi rst needed to declare it in the AndroidManifest.xml fi le: FIGURE 2-26 <activity android:name=”.MyBrowserActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <action android:name=”net.learn2develop.MyBrowser” /> <category android:name=”android.intent.category.DEFAULT” /> <data android:scheme=”http” /> </intent-filter> </activity> In the <intent-filter> element, you declared it to have two actions, one category, and one data. This means that all other activities can invoke this activity using either the “android.intent.action .VIEW” or the “net.learn2develop.MyBrowser” action. For all activities that you want others to call using the startActivity() or startActivityForResult() methods, they need to have the “android.intent.category.DEFAULT” category. If not, your activity will not be callable by others. The <data> element specifies the type of data expected by the activity. In this case, it expects the data to start with the “http://” prefi x. The preceding intent fi lter could also be rewritten as follows: <activity android:name=”.MyBrowserActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <category android:name=”android.intent.category.DEFAULT” /> <data android:scheme=”http” /> </intent-filter> <intent-filter> <action android:name=”net.learn2develop.MyBrowser” /> <category android:name=”android.intent.category.DEFAULT” /> c02.indd 94 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents ❘ 95 <data android:scheme=”http” /> </intent-filter> </activity> Writing the intent fi lter this way makes it much more readable, and it logically groups the action, category, and data within an intent fi lter. If you now use the ACTION_VIEW action with the data shown here, Android will display a selection (as shown in Figure 2-27): Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); You can choose between using the Browser application or the Intents application that you are currently building. FIGURE 2-27 Notice that when multiple activities match your Intent object, the dialog titled “Complete action using” appears. You can customize this by using the createChooser() method from the Intent class, like this: Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); startActivity(Intent.createChooser(i, “Open URL using...”)); The preceding will change the dialog title to “Open URL using…,” as shown in Figure 2-28. Note that the “Use by default for this action” option is now not available. The added benefit of using the createChooser() method is that in the event that no activity matches your Intent object, your application will not crash. Instead, it will display the message shown in Figure 2-29. FIGURE 2-28 c02.indd 95 FIGURE 2-29 25/01/12 12:14 PM
  • 96 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Adding Categories You can group your activities into categories by using the <category> element in the intent fi lter. Suppose you have added the following <category> element to the AndroidManifest.xml fi le: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.Intents” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <uses-permission android:name=”android.permission.CALL_PHONE”/> <uses-permission android:name=”android.permission.INTERNET”/> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.IntentsActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:name=”.MyBrowserActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <action android:name=”net.learn2develop.MyBrowser” /> <category android:name=”android.intent.category.DEFAULT” /> <category android:name=”net.learn2develop.Apps” /> <data android:scheme=”http” /> </intent-filter> </activity> </application> </manifest> In this case, the following code will directly invoke the MyBrowerActivity activity: Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); i.addCategory(“net.learn2develop.Apps”); startActivity(Intent.createChooser(i, “Open URL using...”)); You add the category to the Intent object using the addCategory() method. If you omit the addCategory() statement, the preceding code will still invoke the MyBrowerActivity activity because it will still match the default category android.intent.category.DEFAULT. c02.indd 96 25/01/12 12:14 PM
  • Calling Built-In Applications Using Intents ❘ 97 However, if you specify a category that does not match the category defi ned in the intent fi lter, it will not work (no activity will be launched): Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); //i.addCategory(“net.learn2develop.Apps”); //---this category does not match any in the intent-filter--i.addCategory(“net.learn2develop.OtherApps”); startActivity(Intent.createChooser(i, “Open URL using...”)); The preceding category (net.learn2develop.OtherApps) does not match any category in the intent fi lter, so a run-time exception will be raised (if you don’t use the createChoose() method of the Intent class). If you add the following category in the intent filter of MyBrowerActivity, then the preceding code will work: <activity android:name=”.MyBrowserActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <action android:name=”net.learn2develop.MyBrowser” /> <category android:name=”android.intent.category.DEFAULT” /> <category android:name=”net.learn2develop.Apps” /> <category android:name=”net.learn2develop.OtherApps” /> <data android:scheme=”http” /> </intent-filter> </activity> You can add multiple categories to an Intent object; for example, the following statements add the net.learn2develop.SomeOtherApps category to the Intent object: Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(“http://www.amazon.com”)); //i.addCategory(“net.learn2develop.Apps”); //---this category does not match any in the intent-filter--i.addCategory(“net.learn2develop.OtherApps”); i.addCategory(“net.learn2develop.SomeOtherApps”); startActivity(Intent.createChooser(i, “Open URL using...”)); Because the intent fi lter does not defi ne the net.learn2develop.SomeOtherApps category, the preceding code will not be able to invoke the MyBrowerActivity activity. To fi x this, you need to add the net.learn2develop.SomeOtherApps category to the intent filter again. From this example, it is evident that when using an Intent object with categories, all categories added to the Intent object must fully match those defi ned in the intent fi lter before an activity can be invoked. c02.indd 97 25/01/12 12:14 PM
  • ❘ 98 CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS DISPLAYING NOTIFICATIONS So far, you have been using the Toast class to display messages to the user. While the Toast class is a handy way to show users alerts, it is not persistent. It flashes on the screen for a few seconds and then disappears. If it contains important information, users may easily miss it if they are not looking at the screen. For messages that are important, you should use a more persistent method. In this case, you should use the NotificationManager to display a persistent message at the top of the device, commonly known as the status bar (sometimes also referred to as the notification bar). The following Try It Out demonstrates how. TRY IT OUT Displaying Notifications on the Status Bar codefile Notifi cations.zip available for download at Wrox.com 1. 2. 3. Using Eclipse, create a new Android project and name it Notifications. Add a new class fi le named NotificationView to the package. In addition, add a new notification.xml fi le to the res/layout folder. Populate the notification.xml fi le as follows: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Here are the details for the notification...” /> </LinearLayout> 4. Populate the NotificationView.java fi le as follows: package net.learn2develop.Notifications; import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; public class NotificationView extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification); //---look up the notification manager service--NotificationManager nm = (NotificationManager) c02.indd 98 25/01/12 12:14 PM
  • Displaying Notifications ❘ 99 getSystemService(NOTIFICATION_SERVICE); //---cancel the notification that we started--nm.cancel(getIntent().getExtras().getInt(“notificationID”)); } } 5. Add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.Notifications” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <uses-permission android:name=”android.permission.VIBRATE”/> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.NotificationsActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:name=”.NotificationView” android:label=”Details of notification”> <intent-filter> <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> </application> </manifest> 6. Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn_displaynotif” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Display Notification” android:onClick=”onClick”/> </LinearLayout> c02.indd 99 25/01/12 12:14 PM
  • 100 7. ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS Finally, add the following statements in bold to the NotificationsActivity.java fi le: package net.learn2develop.Notifications; import import import import import import import android.app.Activity; android.app.Notification; android.app.NotificationManager; android.app.PendingIntent; android.content.Intent; android.os.Bundle; android.view.View; public class NotificationsActivity extends Activity { int notificationID = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { displayNotification(); } protected void displayNotification() { //---PendingIntent to launch activity if the user selects // this notification--Intent i = new Intent(this, NotificationView.class); i.putExtra(“notificationID”, notificationID); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notif = new Notification( R.drawable.ic_launcher, “Reminder: Meeting starts in 5 minutes”, System.currentTimeMillis()); CharSequence from = “System Alarm”; CharSequence message = “Meeting with customer at 3pm...”; notif.setLatestEventInfo(this, from, message, pendingIntent); //---100ms delay, vibrate for 250ms, pause for 100 ms and // then vibrate for 500ms--notif.vibrate = new long[] { 100, 250, 100, 500}; nm.notify(notificationID, notif); } } c02.indd 100 25/01/12 12:14 PM
  • ❘ 101 Displaying Notifications 8. Press F11 to debug the application on the Android emulator. 9. Click the Display Notification button and a notification ticker text (set in the constructor of the Notification object) will appear on the status bar (see Figure 2-30). 10. Clicking and dragging the status bar down FIGURE 2-30 will reveal the notification details set using the setLatestEventInfo() method of the Notification object (see Figure 2-31). 11. Clicking on the notification will reveal the NotificationView activity (see Figure 2-32). This also causes the notification to be dismissed from the status bar. FIGURE 2-31 FIGURE 2-32 How It Works To display a notification, you fi rst created an Intent object to point to the NotificationView class: Intent i = new Intent(this, NotificationView.class); i.putExtra(“notificationID”, notificationID); This intent is used to launch another activity when the user selects a notification from the list of notifications. In this example, you added a name/value pair to the Intent object so that you can tag the notification ID, identifying the notification to the target activity. This ID will be used to dismiss the notification later. You also need to create a PendingIntent object. A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, regardless of whether your application is running or not. In this case, you initialized it as follows: PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0); c02.indd 101 25/01/12 12:14 PM
  • 102 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS The getActivity() method retrieves a PendingIntent object and you set it using the following arguments: ➤ context — Application context ➤ request code — Request code for the intent ➤ intent — The intent for launching the target activity ➤ flags — The flags in which the activity is to be launched You then obtain an instance of the NotificationManager class and create an instance of the Notification class: NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notif = new Notification( R.drawable.ic_launcher, “Reminder: Meeting starts in 5 minutes”, System.currentTimeMillis()); The Notification class enables you to specify the notification’s main information when the notification fi rst appears on the status bar. The second argument to the Notification constructor sets the “ticker text” on the status bar (see Figure 2-33). FIGURE 2-33 Next, you set the details of the notification using the setLatestEventInfo() method: CharSequence from = “System Alarm”; CharSequence message = “Meeting with customer at 3pm...”; notif.setLatestEventInfo(this, from, message, pendingIntent); //---100ms delay, vibrate for 250ms, pause for 100 ms and // then vibrate for 500ms--notif.vibrate = new long[] { 100, 250, 100, 500}; The preceding also sets the notification to vibrate the phone. Finally, to display the notification you use the notify() method: nm.notify(notificationID, notif); When the user clicks on the notification, the NotificationView activity is launched. Here, you dismiss the notification by using the cancel() method of the NotificationManager object and passing it the ID of the notification (passed in via the Intent object): c02.indd 102 25/01/12 12:14 PM
  • ❘ 103 Summary //---look up the notification manager service--NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //---cancel the notification that we started--nm.cancel(getIntent().getExtras().getInt(“notificationID”)); SUMMARY This chapter fi rst provided a detailed look at how activities and fragments work and the various forms in which you can display them. You also learned how to display dialog windows using activities. The second part of this chapter demonstrated a very important concept in Android — the intent. The intent is the “glue” that enables different activities to be connected, and it is a vital concept to understand when developing for the Android platform. EXERCISES 1. What will happen if you have two or more activities with the same intent filter action name? 2. Write the code to invoke the built-in Browser application. 3. Which components can you specify in an intent filter? 4. What is the difference between the Toast class and the NotificationManager class? 5. Name the two ways to add fragments to an activity. 6. Name one key difference between a fragment and an activity. Answers to the exercises can be found in Appendix C. c02.indd 103 25/01/12 12:14 PM
  • 104 ❘ CHAPTER 2 ACTIVITIES, FRAGMENTS, AND INTENTS WHAT YOU LEARNED IN THIS CHAPTER TOPIC KEY CONCEPTS Creating an activity All activities must be declared in the AndroidManifest.xml file. Key life cycle of an activity When an activity is started, the onStart() and onResume() events are always called. When an activity is killed or sent to the background, the onPause() event is always called. Displaying an activity as a dialog Fragments Fragments are “mini-activities” that can be added or removed from activities. Manipulating fragments programmatically You need to use the FragmentManager and FragmentTransaction classes when adding, removing, or replacing fragments during runtime. Life cycle of a fragment Similar to that of an activity — you save the state of a fragment in the onPause() event, and restore its state in one of the following events: onCreate(), onCreateView(), or onActivityCreated(). Intent The “glue” that connects different activities Intent filter The “filter” that enables you to specify how your activities should be called Calling an activity Use the startActivity() or startActivityForResult() method. Passing data to an activity Use the Bundle object. Components in an Intent object An Intent object can contain the following: action, data, type, and category. Displaying notifications Use the NotificationManager class. PendingIntent object c02.indd 104 Use the showDialog() method and implement the onCreateDialog() method. A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, regardless of whether or not your application is running. 25/01/12 12:14 PM
  • 3 Getting to Know the Android User Interface WHAT YOU WILL LEARN IN THIS CHAPTER ➤ The various ViewGroups you can use to lay out your views ➤ How to adapt and manage changes in screen orientation ➤ How to create the UI programmatically ➤ How to listen for UI notifications In Chapter 2, you learned about the Activity class and its life cycle. You learned that an activity is a means by which users interact with the application. However, an activity by itself does not have a presence on the screen. Instead, it has to draw the screen using Views and ViewGroups. In this chapter, you will learn the details about creating user interfaces in Android, and how users interact with them. In addition, you will learn how to handle changes in screen orientation on your Android devices. UNDERSTANDING THE COMPONENTS OF A SCREEN In Chapter 2, you learned that the basic unit of an Android application is an activity. An activity displays the user interface of your application, which may contain widgets such as buttons, labels, textboxes, and so on. Typically, you define your UI using an XML file (e.g., the main.xml file located in the res/layout folder of your project), which looks similar to the following: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” c03.indd 105 25/01/12 8:31 AM
  • 106 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> </LinearLayout> During runtime, you load the XML UI in the onCreate() method handler in your Activity class, using the setContentView() method of the Activity class: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } During compilation, each element in the XML fi le is compiled into its equivalent Android GUI class, with attributes represented by methods. The Android system then creates the UI of the activity when it is loaded. NOTE Although it is always easier to build your UI using an XML file, sometimes you need to build your UI dynamically during runtime (for example, when writing games). Hence, it is also possible to create your UI entirely using code. Later in this chapter you will see an example showing how this can be done. Views and ViewGroups An activity contains views and ViewGroups. A view is a widget that has an appearance on screen. Examples of views are buttons, labels, and textboxes. A view derives from the base class android .view.View. NOTE Chapters 4 and 5 discuss the various common views in Android. One or more views can be grouped together into a ViewGroup. A ViewGroup (which is itself a special type of view) provides the layout in which you can order the appearance and sequence of views. Examples of ViewGroups include LinearLayout and FrameLayout. A ViewGroup derives from the base class android.view.ViewGroup. Android supports the following ViewGroups: ➤ ➤ c03.indd 106 LinearLayout AbsoluteLayout 25/01/12 8:31 AM
  • Understanding the Components of a Screen ➤ TableLayout ➤ RelativeLayout ➤ FrameLayout ➤ ❘ 107 ScrollView The following sections describe each of these ViewGroups in more detail. Note that in practice it is common to combine different types of layouts to create the UI you want. LinearLayout The LinearLayout arranges views in a single column or a single row. Child views can be arranged either vertically or horizontally. To see how LinearLayout works, consider the following elements typically contained in the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> </LinearLayout> In the main.xml fi le, observe that the root element is <LinearLayout> and it has a <TextView> element contained within it. The <LinearLayout> element controls the order in which the views contained within it appear. Each View and ViewGroup has a set of common attributes, some of which are described in Table 3-1. TABLE 3-1: Common Attributes Used in Views and ViewGroups ATTRIBUTE DESCRIPTION layout_width Specifies the width of the View or ViewGroup layout_height Specifies the height of the View or ViewGroup layout_marginTop Specifies extra space on the top side of the View or ViewGroup layout_marginBottom Specifies extra space on the bottom side of the View or ViewGroup layout_marginLeft Specifies extra space on the left side of the View or ViewGroup layout_marginRight Specifies extra space on the right side of the View or ViewGroup continues c03.indd 107 25/01/12 8:31 AM
  • 108 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE TABLE 3-1 (continued) ATTRIBUTE DESCRIPTION layout_gravity Specifies how child Views are positioned layout_weight Specifies how much of the extra space in the layout should be allocated to the View layout_x Specifies the x-coordinate of the View or ViewGroup layout_y Specifies the y-coordinate of the View or ViewGroup NOTE Some of these attributes are applicable only when a View is in a specific ViewGroup. For example, the layout_weight and layout_gravity attributes are applicable only when a View is in either a LinearLayout or a TableLayout. For example, the width of the <TextView> element fi lls the entire width of its parent (which is the screen in this case) using the fill_parent constant. Its height is indicated by the wrap_content constant, which means that its height is the height of its content (in this case, the text contained within it). If you don’t want the <TextView> view to occupy the entire row, you can set its layout_ width attribute to wrap_content, like this: <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”@string/hello” /> The preceding code will set the width of the view to be equal to the width of the text contained within it. Consider the following layout: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”100dp” android:layout_height=”wrap_content” android:text=”@string/hello” /> <Button android:layout_width=”160dp” android:layout_height=”wrap_content” android:text=”Button” android:onClick=”onClick” /> </LinearLayout> c03.indd 108 25/01/12 8:31 AM
  • Understanding the Components of a Screen ❘ 109 UNITS OF MEASUREMENT When specifying the size of an element on an Android UI, you should be aware of the following units of measurement: dp — Density-independent pixel. 1 dp is equivalent to one pixel on a 160 dpi screen. This is the recommended unit of measurement when specifying the dimension of views in your layout. The 160 dpi screen is the baseline density assumed by Android. You can specify either “dp” or “dip” when referring to a density-independent pixel. sp — Scale-independent pixel. This is similar to dp and is recommended for specifying font sizes. pt — Point. A point is defi ned to be 1/72 of an inch, based on the physical screen size. px — Pixel. Corresponds to actual pixels on the screen. Using this unit is not recommended, as your UI may not render correctly on devices with a different screen resolution. Here, you set the width of both the TextView and Button views to an absolute value. In this case, the width for the TextView is set to 100 density-independent pixels wide, and the Button to 160 density-independent pixels wide. Before you see how the views will look like on different screens with different pixel density, it is important to understand how Android recognizes screens of varying sizes and density. Figure 3-1 shows the screen of the Nexus S. It has a 4-inch screen (diagonally), with a screen width of 2.04 inches. Its resolution is 480 (width) ϫ 800 (height) pixels. With 480 pixels spread across a width of 2.04 inches, the result is a pixel density of about 235 dots per inch (dpi). As you can see from the figure, the pixel density of a screen varies according to screen size and resolution. Android defi nes and recognizes four screen densities: ➤ Low density (ldpi) — 120 dpi ➤ Medium density (mdpi) — 160 dpi ➤ High density (hdpi) — 240 dpi ➤ Extra High density (xhdpi) — 320 dpi FIGURE 3-1 Your device will fall into one of the densities defi ned in the preceding list. For example, the Nexus S is regarded as a hdpi device, as its pixel density is closest to 240 dpi. The HTC Hero, however, has a c03.indd 109 25/01/12 8:31 AM
  • ❘ 110 CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE 3.2-inch (diagonal) screen size and a resolution of 320 ϫ 480. Its pixel density works out to be about 180 dpi. Therefore, it would be considered an mdpi device, as its pixel density is closest to 160 dpi. To test how the views defi ned in the XML fi le will look when displayed on screens of different densities, create two AVDs with different screen resolutions and abstracted LCD densities. Figure 3-2 shows an AVD with 480 ϫ 800 resolution and LCD density of 235, emulating the Nexus S. Figure 3-3 shows another AVD with 320 ϫ 480 resolution and LCD density of 180, emulating the HTC Hero. FIGURE 3-2 FIGURE 3-3 Figure 3-4 shows what the views look like when viewed on the emulator with a screen density of 235 dpi. Figure 3-5 shows what the views look like when viewed on the emulator with a screen density of 180 dpi. FIGURE 3-4 c03.indd 110 FIGURE 3-5 25/01/12 8:31 AM
  • ❘ 111 Understanding the Components of a Screen Using the dp unit ensures that your views are always displayed in the right proportion regardless of the screen density — Android automatically scales the size of the view depending on the density of the screen. Using the Button as an example, if it is displayed on a 180 dpi screen (a 180 dpi screen is treated just like a 160 dpi screen), its width would be 160 pixels. However, if it is displayed on a 235 dpi screen (which is treated as a 240 dpi screen), then its width would be 240 pixels. HOW TO CONVERT DP TO PX The formula for converting dp to px (pixels) is as follows: Actual pixels = dp * (dpi / 160), where dpi is either 120, 160, 240, or 320. Therefore, in the case of the Button on a 235 dpi screen, its actual width is 160 * (240/160) = 240 px. When run on the 180 dpi emulator (regarded as a 160 dpi device), its actual pixel is now 160 * (160/160) = 160 px. In this case, one dp is equivalent to one px. To prove that this is indeed correct, you can use the getWidth() method of a View object to get its width in pixels: public void onClick(View view) { Toast.makeText(this, String.valueOf(view.getWidth()), Toast.LENGTH_LONG).show(); } What if instead of using dp you now specify the size using pixels (px)? <TextView android:layout_width=”100px” android:layout_height=”wrap_content” android:text=”@string/hello” /> <Button android:layout_width=”160px” android:layout_height=”wrap_content” android:text=”Button” android:onClick=”onClick”/> Figure 3-6 shows what the Label and Button look like on a 235 dpi screen. Figure 3-7 shows the same views on a 180 dpi screen. In this case, Android does not perform any conversion, as all the c03.indd 111 25/01/12 8:31 AM
  • 112 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE sizes are specified in pixels. In general (with screen sizes being equal), if you use pixels for view sizes, the views will appear smaller on a device with a high dpi screen, compared to one with a lower dpi. FIGURE 3-6 FIGURE 3-7 The preceding example also specifies that the orientation of the layout is vertical: <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > The default orientation layout is horizontal, so if you omit the android:orientation attribute, the views will appear as shown in Figure 3-8. c03.indd 112 25/01/12 8:31 AM
  • Understanding the Components of a Screen ❘ 113 FIGURE 3-8 In LinearLayout, you can apply the layout_weight and layout_gravity attributes to views contained within it, as the following modifications to main.xml show: <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:layout_width=”160dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_gravity=”left” android:layout_weight=”1” /> <Button android:layout_width=”160dp” android:layout_height=”wrap_content” android:text=”Button” c03.indd 113 25/01/12 8:31 AM
  • 114 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE android:layout_gravity=”center” android:layout_weight=”2” /> <Button android:layout_width=”160dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_gravity=”right” android:layout_weight=”3” /> </LinearLayout> Figure 3-9 shows the positioning of the views as well as their heights. The layout_gravity attribute indicates the positions the views should gravitate towards, while the layout_weight attribute specifies the distribution of available space. In the preceding example, the three buttons occupy about 16.6% (1/ (1+2+3) * 100), 33.3% (2/(1+2+3) * 100), and 50% (3/(1+2+3) * 100) of the available height, respectively. If you change the orientation of the LinearLayout to horizontal, you need to change the width of each view to 0 dp, and the views will be displayed as shown in Figure 3-10: FIGURE 3-9 <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <Button android:layout_width=”0dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_gravity=”left” android:layout_weight=”1” /> <Button android:layout_width=”0dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_gravity=”center_horizontal” android:layout_weight=”2” /> <Button android:layout_width=”0dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_gravity=”right” android:layout_weight=”3” /> </LinearLayout> c03.indd 114 FIGURE 3-10 25/01/12 8:31 AM
  • Understanding the Components of a Screen ❘ 115 AbsoluteLayout The AbsoluteLayout enables you to specify the exact location of its children. Consider the following UI defi ned in main.xml: <AbsoluteLayout android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android” > <Button android:layout_width=”188dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_x=”126px” android:layout_y=”361px” /> <Button android:layout_width=”113dp” android:layout_height=”wrap_content” android:text=”Button” android:layout_x=”12px” android:layout_y=”361px” /> </AbsoluteLayout> Figure 3-11 shows the two Button views (tested on a 180 dpi AVD) located at their specified positions using the android_layout_x and android_layout_y attributes. However, there is a problem with the AbsoluteLayout when the activity is viewed on a highresolution screen (see Figure 3-12). For this reason, the AbsoluteLayout has been deprecated since Android 1.5 (although it is still supported in the current version). You should avoid using the AbsoluteLayout in your UI, as it is not guaranteed to be supported in future versions of Android. You should instead use the other layouts described in this chapter. FIGURE 3-11 c03.indd 115 FIGURE 3-12 25/01/12 8:31 AM
  • 116 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE TableLayout The TableLayout groups views into rows and columns. You use the <TableRow> element to designate a row in the table. Each row can contain one or more views. Each view you place within a row forms a cell. The width of each column is determined by the largest width of each cell in that column. Consider the content of main.xml shown here: <TableLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_height=”fill_parent” android:layout_width=”fill_parent” > <TableRow> <TextView android:text=”User Name:” android:width =”120dp” /> <EditText android:id=”@+id/txtUserName” android:width=”200dp” /> </TableRow> <TableRow> <TextView android:text=”Password:” /> <EditText android:id=”@+id/txtPassword” android:password=”true” /> </TableRow> <TableRow> <TextView /> <CheckBox android:id=”@+id/chkRememberPassword” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Remember Password” /> </TableRow> <TableRow> <Button android:id=”@+id/buttonSignIn” android:text=”Log In” /> </TableRow> </TableLayout> Figure 3-13 shows what the preceding looks like when rendered on the Android emulator. c03.indd 116 25/01/12 8:31 AM
  • Understanding the Components of a Screen ❘ 117 Note that in the preceding example, there are two columns and four rows in the TableLayout. The cell directly under the Password TextView is populated with a <TextView/> empty element. If you don’t do this, the Remember Password checkbox will appear under the Password TextView, as shown in Figure 3-14. FIGURE 3-13 FIGURE 3-14 RelativeLayout The RelativeLayout enables you to specify how child views are positioned relative to each other. Consider the following main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <RelativeLayout android:id=”@+id/RLayout” android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android” > <TextView android:id=”@+id/lblComments” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Comments” android:layout_alignParentTop=”true” android:layout_alignParentLeft=”true” /> <EditText android:id=”@+id/txtComments” c03.indd 117 25/01/12 8:31 AM
  • 118 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE android:layout_width=”fill_parent” android:layout_height=”170px” android:textSize=”18sp” android:layout_alignLeft=”@+id/lblComments” android:layout_below=”@+id/lblComments” android:layout_centerHorizontal=”true” /> <Button android:id=”@+id/btnSave” android:layout_width=”125px” android:layout_height=”wrap_content” android:text=”Save” android:layout_below=”@+id/txtComments” android:layout_alignRight=”@+id/txtComments” /> <Button android:id=”@+id/btnCancel” android:layout_width=”124px” android:layout_height=”wrap_content” android:text=”Cancel” android:layout_below=”@+id/txtComments” android:layout_alignLeft=”@+id/txtComments” /> </RelativeLayout> Notice that each view embedded within the RelativeLayout has attributes that enable it to align with another view. These attributes are as follows: ➤ layout_alignParentTop ➤ layout_alignParentLeft ➤ layout_alignLeft ➤ layout_alignRight ➤ layout_below ➤ layout_centerHorizontal The value for each of these attributes is the ID for the view that you are referencing. The preceding XML UI creates the screen shown in Figure 3-15. FrameLayout The FrameLayout is a placeholder on screen that you can use to display a single view. Views that you add to a FrameLayout are always anchored to the top left of the layout. Consider the following content in main.xml: FIGURE 3-15 <?xml version=”1.0” encoding=”utf-8”?> <RelativeLayout android:id=”@+id/RLayout” c03.indd 118 25/01/12 8:31 AM
  • Understanding the Components of a Screen ❘ 119 android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android” > <TextView android:id=”@+id/lblComments” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Hello, Android!” android:layout_alignParentTop=”true” android:layout_alignParentLeft=”true” /> <FrameLayout android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_alignLeft=”@+id/lblComments” android:layout_below=”@+id/lblComments” android:layout_centerHorizontal=”true” > <ImageView android:src = “@drawable/droid” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </FrameLayout> </RelativeLayout> Here, you have a FrameLayout within a RelativeLayout. Within the FrameLayout, you embed an ImageView. The UI is shown in Figure 3-16. FIGURE 3-16 c03.indd 119 25/01/12 8:31 AM
  • 120 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE NOTE This example assumes that the res/drawable-mdpi folder has an image named droid.png. If you add another view (such as a Button view) within the FrameLayout, the view will overlap the previous view (see Figure 3-17): <?xml version=”1.0” encoding=”utf-8”?> <RelativeLayout android:id=”@+id/RLayout” android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android” > <TextView android:id=”@+id/lblComments” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Hello, Android!” android:layout_alignParentTop=”true” android:layout_alignParentLeft=”true” /> <FrameLayout android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_alignLeft=”@+id/lblComments” android:layout_below=”@+id/lblComments” android:layout_centerHorizontal=”true” > <ImageView android:src = “@drawable/droid” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <Button android:layout_width=”124dp” android:layout_height=”wrap_content” android:text=”Print Picture” /> </FrameLayout> </RelativeLayout> c03.indd 120 25/01/12 8:31 AM
  • Understanding the Components of a Screen ❘ 121 FIGURE 3-17 NOTE You can add multiple views to a FrameLayout, but each will be stacked on top of the previous one. This is when you want to animate a series of images, with only one visible at a time. ScrollView A ScrollView is a special type of FrameLayout in that it enables users to scroll through a list of views that occupy more space than the physical display. The ScrollView can contain only one child view or ViewGroup, which normally is a LinearLayout. NOTE Do not use a ListView (discussed in Chapter 4) together with the ScrollView. The ListView is designed for showing a list of related information and is optimized for dealing with large lists. The following main.xml content shows a ScrollView containing a LinearLayout, which in turn contains some Button and EditText views: <ScrollView android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android” > <LinearLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” > <Button android:id=”@+id/button1” c03.indd 121 25/01/12 8:31 AM
  • 122 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button 1” /> <Button android:id=”@+id/button2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button 2” /> <Button android:id=”@+id/button3” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button 3” /> <EditText android:id=”@+id/txt” android:layout_width=”fill_parent” android:layout_height=”600dp” /> <Button android:id=”@+id/button4” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button 4” /> <Button android:id=”@+id/button5” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button 5” /> </LinearLayout> </ScrollView> If you load the preceding code on the Android emulator, you will see something like Figure 3-18. FIGURE 3-18 Because the EditText automatically gets the focus, it fills up the entire activity (as the height was set to 600dp). To prevent it from getting the focus, add the following two attributes to the <LinearLayout> element: <LinearLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:focusable=”true” android:focusableInTouchMode=”true” > You will now be able to view the buttons and scroll through the list of views (see Figure 3-19). c03.indd 122 25/01/12 8:31 AM
  • ❘ 123 Adapting to Display Orientation FIGURE 3-19 Sometimes you may want the EditText to automatically get the focus, but you do not want the soft input panel (keyboard) to appear automatically (which happens on a real device). To prevent the keyboard from appearing, add the following attribute to the <activity> element in the AndroidManifest.xml fi le: <activity android:label=”@string/app_name” android:name=”.LayoutsActivity” android:windowSoftInputMode=”stateHidden” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> ADAPTING TO DISPLAY ORIENTATION One of the key features of modern smartphones is their ability to switch screen orientation, and Android is no exception. Android supports two screen orientations: portrait and landscape. By default, when you change the display orientation of your Android device, the current activity that is displayed automatically redraws its content in the new orientation. This is because the onCreate() method of the activity is fi red whenever there is a change in display orientation. c03.indd 123 25/01/12 8:31 AM
  • 124 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE NOTE When you change the orientation of your Android device, your current activity is actually destroyed and then recreated. However, when the views are redrawn, they may be drawn in their original locations (depending on the layout selected). Figure 3-20 shows one of the examples illustrated earlier displayed in both portrait and landscape mode. Note that in landscape mode, a lot of empty space on the right of the screen could be used. Furthermore, any additional views at the bottom of the screen would be hidden when the screen orientation is set to landscape. FIGURE 3-20 In general, you can employ two techniques to handle changes in screen orientation: ➤ c03.indd 124 Anchoring — The easiest way is to “anchor” your views to the four edges of the screen. When the screen orientation changes, the views can anchor neatly to the edges. 25/01/12 8:31 AM
  • Adapting to Display Orientation ➤ ❘ 125 Resizing and repositioning — Whereas anchoring and centralizing are simple techniques to ensure that views can handle changes in screen orientation, the ultimate technique is resizing each and every view according to the current screen orientation. Anchoring Views Anchoring can be easily achieved by using RelativeLayout. Consider the following main.xml fi le, which contains five Button views embedded within the <RelativeLayout> element: <RelativeLayout android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android”> <Button android:id=”@+id/button1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Top Left” android:layout_alignParentLeft=”true” android:layout_alignParentTop=”true” /> <Button android:id=”@+id/button2” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Top Right” android:layout_alignParentTop=”true” android:layout_alignParentRight=”true” /> <Button android:id=”@+id/button3” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Bottom Left” android:layout_alignParentLeft=”true” android:layout_alignParentBottom=”true” /> <Button android:id=”@+id/button4” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Bottom Right” android:layout_alignParentRight=”true” android:layout_alignParentBottom=”true” /> <Button android:id=”@+id/button5” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Middle” android:layout_centerVertical=”true” android:layout_centerHorizontal=”true” /> </RelativeLayout> c03.indd 125 25/01/12 8:31 AM
  • 126 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE Note the following attributes found in the various Button views: ➤ layout_alignParentLeft — Aligns the view to the left of the parent view ➤ layout_alignParentRight — Aligns the view to the right of the parent view ➤ layout_alignParentTop — Aligns the view to the top of the parent view ➤ layout_alignParentBottom — Aligns the view to the bottom of the parent view ➤ layout_centerVertical — Centers the view vertically within its parent view ➤ layout_centerHorizontal — Centers the view horizontally within its parent view Figure 3-21 shows the activity when viewed in portrait mode. When the screen orientation changes to landscape mode, the four buttons are aligned to the four edges of the screen, and the center button is centered in the middle of the screen with its width fully stretched (see Figure 3-22). FIGURE 3-21 FIGURE 3-22 c03.indd 126 25/01/12 8:31 AM
  • Adapting to Display Orientation ❘ 127 Resizing and Repositioning Apart from anchoring your views to the four edges of the screen, an easier way to customize the UI based on screen orientation is to create a separate res/layout folder containing the XML fi les for the UI of each orientation. To support landscape mode, you can create a new folder in the res folder and name it as layout-land (representing landscape). Figure 3-23 shows the new folder containing the fi le main.xml. Basically, the main.xml fi le contained within the layout folder defi nes the UI for the activity in portrait mode, whereas the main.xml fi le in the layout-land folder defi nes the UI in landscape mode. The following code shows the content of main.xml under the layout folder: FIGURE 3-23 <?xml version=”1.0” encoding=”utf-8”?> <RelativeLayout android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android”> <Button android:id=”@+id/button1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Top Left” android:layout_alignParentLeft=”true” android:layout_alignParentTop=”true” /> <Button android:id=”@+id/button2” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Top Right” android:layout_alignParentTop=”true” android:layout_alignParentRight=”true” /> <Button android:id=”@+id/button3” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Bottom Left” android:layout_alignParentLeft=”true” android:layout_alignParentBottom=”true” /> <Button android:id=”@+id/button4” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Bottom Right” android:layout_alignParentRight=”true” android:layout_alignParentBottom=”true” /> <Button android:id=”@+id/button5” android:layout_width=”fill_parent” c03.indd 127 25/01/12 8:31 AM
  • 128 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE android:layout_height=”wrap_content” android:text=”Middle” android:layout_centerVertical=”true” android:layout_centerHorizontal=”true” /> </RelativeLayout> The following shows the content of main.xml under the layout-land folder (the statements in bold are the additional views to display in landscape mode): <?xml version=”1.0” encoding=”utf-8”?> <RelativeLayout android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android”> <Button android:id=”@+id/button1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Top Left” android:layout_alignParentLeft=”true” android:layout_alignParentTop=”true” /> <Button android:id=”@+id/button2” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Top Right” android:layout_alignParentTop=”true” android:layout_alignParentRight=”true” /> <Button android:id=”@+id/button3” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Bottom Left” android:layout_alignParentLeft=”true” android:layout_alignParentBottom=”true” /> <Button android:id=”@+id/button4” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Bottom Right” android:layout_alignParentRight=”true” android:layout_alignParentBottom=”true” /> <Button android:id=”@+id/button5” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Middle” android:layout_centerVertical=”true” android:layout_centerHorizontal=”true” /> <Button android:id=”@+id/button6” android:layout_width=”180px” c03.indd 128 25/01/12 8:31 AM
  • Adapting to Display Orientation ❘ 129 android:layout_height=”wrap_content” android:text=”Top Middle” android:layout_centerVertical=”true” android:layout_centerHorizontal=”true” android:layout_alignParentTop=”true” /> <Button android:id=”@+id/button7” android:layout_width=”180px” android:layout_height=”wrap_content” android:text=”Bottom Middle” android:layout_centerVertical=”true” android:layout_centerHorizontal=”true” android:layout_alignParentBottom=”true” /> </RelativeLayout> When the activity is loaded in portrait mode, it will display five buttons, as shown in Figure 3-24. When the activity is loaded in landscape mode, seven buttons are displayed (see Figure 3-25), demonstrating that different XML fi les are loaded when the device is in different orientations. FIGURE 3-24 FIGURE 3-25 c03.indd 129 25/01/12 8:31 AM
  • 130 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE Using this method, when the orientation of the device changes, Android automatically loads the appropriate XML file for your activity depending on the current screen orientation. MANAGING CHANGES TO SCREEN ORIENTATION Now that you have looked at how to implement the two techniques for adapting to screen orientation changes, let’s explore what happens to an activity’s state when the device changes orientation. The following Try It Out demonstrates the behavior of an activity when the device changes orientation. TRY IT OUT Understanding Activity Behavior When Orientation Changes codefile Orientations.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Orientations. Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <EditText android:id=”@+id/txtField1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <EditText android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> </LinearLayout> 3. Add the following statements in bold to the OrientationsActivity.java fi le: package net.learn2develop.Orientations; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class OrientationsActivity extends Activity { /** Called when the activity is first created. */ @Override c03.indd 130 25/01/12 8:31 AM
  • Managing Changes to Screen Orientation ❘ 131 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d(“StateInfo”, “onCreate”); } @Override public void onStart() { Log.d(“StateInfo”, “onStart”); super.onStart(); } @Override public void onResume() { Log.d(“StateInfo”, “onResume”); super.onResume(); } @Override public void onPause() { Log.d(“StateInfo”, “onPause”); super.onPause(); } @Override public void onStop() { Log.d(“StateInfo”, “onStop”); super.onStop(); } @Override public void onDestroy() { Log.d(“StateInfo”, “onDestroy”); super.onDestroy(); } @Override public void onRestart() { Log.d(“StateInfo”, “onRestart”); super.onRestart(); } 4. 5. c03.indd 131 Press F11 to debug the application on the Android emulator. Enter some text into the two EditText views (see Figure 3-26). FIGURE 3-26 25/01/12 8:31 AM
  • 132 6. ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE Change the orientation of the Android emulator by pressing Ctrl+F11. Figure 3-27 shows the emulator in landscape mode. Note that the text in the fi rst EditText view is still visible, while the second EditText view is now empty. FIGURE 3-27 7. Observe the output in the LogCat window (you need to switch to the Debug perspective in Eclipse). You should see something like this: 12-15 12-15 12-15 ... 12-15 12-15 12-15 12-15 12-15 12-15 12:27:20.747: D/StateInfo(557): onCreate 12:27:20.747: D/StateInfo(557): onStart 12:27:20.747: D/StateInfo(557): onResume 12:39:37.846: 12:39:37.846: 12:39:37.866: 12:39:38.206: 12:39:38.216: 12:39:38.257: D/StateInfo(557): D/StateInfo(557): D/StateInfo(557): D/StateInfo(557): D/StateInfo(557): D/StateInfo(557): onPause onStop onDestroy onCreate onStart onResume How It Works From the output shown in the LogCat window, it is apparent that when the device changes orientation, the activity is destroyed: 12-15 12:39:37.846: D/StateInfo(557): onPause 12-15 12:39:37.846: D/StateInfo(557): onStop 12-15 12:39:37.866: D/StateInfo(557): onDestroy c03.indd 132 25/01/12 8:31 AM
  • Managing Changes to Screen Orientation ❘ 133 It is then recreated: 12-15 12:39:38.206: D/StateInfo(557): onCreate 12-15 12:39:38.216: D/StateInfo(557): onStart 12-15 12:39:38.257: D/StateInfo(557): onResume It is important to understand this behavior because you need to ensure that you take the necessary steps to preserve the state of your activity before it changes orientation. For example, your activity may have variables that contain values needed for some calculations in the activity. For any activity, you should save whatever state you need to save in the onPause() method, which is fi red every time the activity changes orientation. The following section demonstrates the different ways to save this state information. Another important behavior to understand is that only views that are named (via the android:id attribute) in an activity will have their state persisted when the activity they are contained in is destroyed. For example, the user may change orientation while entering some text into an EditText view. When this happens, any text inside the EditText view will be persisted and restored automatically when the activity is recreated. Conversely, if you do not name the EditText view using the android:id attribute, the activity will not be able to persist the text currently contained within it. Persisting State Information during Changes in Configuration So far, you have learned that changing screen orientation destroys an activity and recreates it. Keep in mind that when an activity is recreated, its current state may be lost. When an activity is killed, it will fi re one or both of the following two methods: ➤ onPause() — This method is always fired whenever an activity is killed or pushed into the background. ➤ onSaveInstanceState() — This method is also fired whenever an activity is about to be killed or put into the background (just like the onPause() method). However, unlike the onPause() method, the onSaveInstanceState() method is not fired when an activity is being unloaded from the stack (such as when the user pressed the back button), because there is no need to restore its state later. In short, to preserve the state of an activity, you could always implement the onPause() method, and then use your own ways to preserve the state of your activity, such as using a database, internal or external fi le storage, and so on. If you simply want to preserve the state of an activity so that it can be restored later when the activity is recreated (such as when the device changes orientation), a much simpler way is to implement the onSaveInstanceState() method, as it provides a Bundle object as an argument so that you can use it to save your activity’s state. The following code shows that you can save the string ID into the Bundle object during the onSaveInstanceState() method: @Override public void onSaveInstanceState(Bundle outState) { //---save whatever you need to persist--outState.putString(“ID”, “1234567890”); super.onSaveInstanceState(outState); } c03.indd 133 25/01/12 8:31 AM
  • 134 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE When an activity is recreated, the onCreate() method is fi rst fi red, followed by the onRestoreInstanceState() method, which enables you to retrieve the state that you saved previously in the onSaveInstanceState() method through the Bundle object in its argument: @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); //---retrieve the information persisted earlier--String ID = savedInstanceState.getString(“ID”); } Although you can use the onSaveInstanceState() method to save state information, note the limitation that you can only save your state information in a Bundle object. If you need to save more complex data structures, then this is not an adequate solution. Another method that you can use is the onRetainNonConfigurationInstance() method. This method is fi red when an activity is about to be destroyed due to a configuration change (such as a change in screen orientation, keyboard availability, etc.). You can save your current data by returning it in this method, like this: @Override public Object onRetainNonConfigurationInstance() { //---save whatever you want here; it takes in an Object type--return(“Some text to preserve”); } NOTE When screen orientation changes, this change is part of what is known as a configuration change. A configuration change will cause your current activity to be destroyed. Note that this method returns an Object type, which allows you to return nearly any data type. To extract the saved data, you can extract it in the onCreate() method, using the getLastNonConfigurationInstance() method: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d(“StateInfo”, “onCreate”); String str = (String) getLastNonConfigurationInstance(); } A good use for using the onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods is when you need to persist some data momentarily, such as when you have downloaded data from a web service and the c03.indd 134 25/01/12 8:31 AM
  • ❘ 135 Managing Changes to Screen Orientation user changes the screen orientation. In this scenario, saving the data using the preceding two methods is much more efficient than downloading the data again. Detecting Orientation Changes Sometimes you need to know the device’s current orientation during runtime. To determine that, you can use the WindowManager class. The following code snippet demonstrates how you can programmatically detect the current orientation of your activity: import android.view.Display; import android.view.WindowManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---get the current display info--WindowManager wm = getWindowManager(); Display d = wm.getDefaultDisplay(); if (d.getWidth() > d.getHeight()) { //---landscape mode--Log.d(“Orientation”, “Landscape mode”); } else { //---portrait mode--Log.d(“Orientation”, “Portrait mode”); } } The getDefaultDisplay() method returns a Display object representing the screen of the device. You can then get its width and height and deduce the current orientation. Controlling the Orientation of the Activity Occasionally, you might want to ensure that your application is displayed in only a certain orientation. For example, you may be writing a game that should be viewed only in landscape mode. In this case, you can programmatically force a change in orientation using the setRequestOrientation() method of the Activity class: import android.content.pm.ActivityInfo; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---change to landscape mode--setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } c03.indd 135 25/01/12 8:31 AM
  • 136 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant. Besides using the setRequestOrientation() method, you can also use the android:screenOrientation attribute on the <activity> element in AndroidManifest.xml as follows to constrain the activity to a certain orientation: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.Orientations” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.OrientationsActivity” android:screenOrientation=”landscape” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> The preceding example constrains the activity to a certain orientation (landscape in this case) and prevents the activity from being destroyed; that is, the activity will not be destroyed and the onCreate() method will not be fi red again when the orientation of the device changes. Following are two other values that you can specify in the android:screenOrientation attribute: ➤ portrait — Portrait mode ➤ sensor — Based on the accelerometer (default) UTILIZING THE ACTION BAR Besides fragments, another newer feature introduced in Android 3 and 4 is the Action Bar. In place of the traditional title bar located at the top of the device’s screen, the Action Bar displays the application icon together with the activity title. Optionally, on the right side of the Action Bar are action items. Figure 3-28 shows the built-in Email application displaying the application icon, the activity title, and some action items in the Action Bar. The next section discusses action items in more detail. c03.indd 136 25/01/12 8:31 AM
  • Utilizing the Action Bar ❘ 137 FIGURE 3-28 The following Try It Out shows how you can programmatically hide or display the Action Bar. TRY IT OUT 1. 2. 3. Showing and Hiding the Action Bar Using Eclipse, create a new Android project and name it MyActionBar. Press F11 to debug the application on the Android emulator. You should see the application and its Action Bar located at the top of the screen (containing the application icon and the application name “MyActionBar”; see Figure 3-29). To hide the Action Bar, add the following line in bold to the AndroidManifest .xml fi le: FIGURE 3-29 <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.MyActionBar” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > c03.indd 137 25/01/12 8:31 AM
  • 138 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE <activity android:label=”@string/app_name” android:name=”.MyActionBarActivity” android:theme=”@android:style/Theme.Holo.NoActionBar” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> 4. Select the project name in Eclipse and then press F11 to debug the application on the Android emulator again. This time, the Action Bar is not displayed (see Figure 3-30). 5. You can also programmatically remove FIGURE 3-30 the Action Bar using the ActionBar class. To do so, you fi rst need to remove the android:theme attribute you added in the previous step. This is important, otherwise the next step will cause the application to raise an exception. 6. Modify the MyActionBarActivity.java fi le as follows: package net.learn2develop.MyActionBar; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; public class MyActionBarActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar actionBar = getActionBar(); actionBar.hide(); //actionBar.show(); //---show it again--} } 7. Press F11 to debug the application on the emulator again. The Action Bar remains hidden. How It Works The android:theme attribute enables you to turn off the display of the Action Bar for your activity. Setting this attribute to “@android:style/Theme.Holo.NoActionBar” hides the Action Bar. Alternatively, you can programmatically get a reference to the Action Bar during runtime by using the getActionBar() method. Calling the hide() method hides the Action Bar, and calling the show() method displays it. c03.indd 138 25/01/12 8:31 AM
  • Utilizing the Action Bar ❘ 139 Note that if you use the android:theme attribute to turn off the Action Bar, calling the getActionBar() method returns a null during runtime. Hence, it is always better to turn the Action Bar on/off programmatically using the ActionBar class. Adding Action Items to the Action Bar Besides displaying the application icon and the activity title on the left of the Action Bar, you can also display additional items on the Action Bar. These additional items are called action items. Action items are shortcuts to some of the commonly performed operations in your application. For example, you might be building an RSS reader application, in which case some of the action items might be “Refresh feed,” “Delete feed” and “Add new feed.” The following Try It Out shows how you can add action items to the Action Bar. TRY IT OUT 1. Adding Action Items Using the MyActionBar project created in the previous section, add the following code in bold to the MyActionBarActivity.java fi le: package net.learn2develop.MyActionBar; import import import import import android.app.Activity; android.os.Bundle; android.view.Menu; android.view.MenuItem; android.widget.Toast; public class MyActionBarActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //ActionBar actionBar = getActionBar(); //actionBar.hide(); //actionBar.show(); //---show it again--} @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); CreateMenu(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return MenuChoice(item); c03.indd 139 25/01/12 8:31 AM
  • 140 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE } private void CreateMenu(Menu menu) { MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”); { mnu1.setIcon(R.drawable.ic_launcher); mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu2 = menu.add(0, 1, 1, “Item 2”); { mnu2.setIcon(R.drawable.ic_launcher); mnu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu3 = menu.add(0, 2, 2, “Item 3”); { mnu3.setIcon(R.drawable.ic_launcher); mnu3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu4 = menu.add(0, 3, 3, “Item 4”); { mnu4.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu5 = menu.add(0, 4, 4, “Item 5”); { mnu5.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } } private boolean MenuChoice(MenuItem item) { switch (item.getItemId()) { case 0: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 1: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 2: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 3: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 4: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; } return false; } on Item 1”, on Item 2”, on Item 3”, on Item 4”, on Item 5”, } c03.indd 140 25/01/12 8:31 AM
  • Utilizing the Action Bar ❘ 141 2. Press F11 to debug the application on the Android emulator. Observe the icons on the right side of the Action Bar (see Figure 3-31). If you click the MENU button on the emulator, you will see the rest of the menu items (see Figure 3-32). This is known as the overflow menu. On devices that do not have the MENU button, an overflow menu is represented by an icon with an arrow. Figure 3-33 shows the same application running on the Asus Eee Pad Transformer (Android 3.2.1). Clicking the overflow menu displays the rest of the menu items. 3. Clicking each menu item will cause the Toast class to display the name of the menu item selected (see Figure 3-34). FIGURE 3-31 FIGURE 3-33 c03.indd 141 FIGURE 3-32 FIGURE 3-34 25/01/12 8:31 AM
  • 142 4. ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE Press Control-F11 to change the display orientation of the emulator to landscape mode. You will now see four action items on the Action Bar, as shown in Figure 3-35, three with icons and one with text. FIGURE 3-35 How It Works The Action Bar populates its action items by calling the onCreateOptionsMenu() method of an activity: @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); CreateMenu(menu); return true; } In the preceding example, you called the CreateMenu() method to display a list of menu items: private void CreateMenu(Menu menu) { MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”); c03.indd 142 25/01/12 8:31 AM
  • ❘ 143 Utilizing the Action Bar { mnu1.setIcon(R.drawable.ic_launcher); mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu2 = menu.add(0, 1, 1, “Item 2”); { mnu2.setIcon(R.drawable.ic_launcher); mnu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu3 = menu.add(0, 2, 2, “Item 3”); { mnu3.setIcon(R.drawable.ic_launcher); mnu3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu4 = menu.add(0, 3, 3, “Item 4”); { mnu4.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } MenuItem mnu5 = menu.add(0, 4, 4, “Item 5”); { mnu5.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } } To make the menu item appear as an action item, you call its setShowAsAction() method using the SHOW_AS_ACTION_IF_ROOM constant. This tells the Android device to display the menu item as an action item if there is room for it. When a menu item is selected by the user, the onOptionsItemSelected() method is called: @Override public boolean onOptionsItemSelected(MenuItem item) { return MenuChoice(item); } Here, you called the self-defi ned MenuChoice() method to check which menu item was clicked, and then printed out a message: private boolean MenuChoice(MenuItem item) { switch (item.getItemId()) { case 0: Toast.makeText(this, “You clicked on Item 1”, Toast.LENGTH_LONG).show(); return true; case 1: Toast.makeText(this, “You clicked on Item 2”, Toast.LENGTH_LONG).show(); return true; case 2: Toast.makeText(this, “You clicked on Item 3”, Toast.LENGTH_LONG).show(); return true; c03.indd 143 25/01/12 8:31 AM
  • 144 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE case 3: Toast.makeText(this, “You clicked on Item 4”, Toast.LENGTH_LONG).show(); return true; case 4: Toast.makeText(this, “You clicked on Item 5”, Toast.LENGTH_LONG).show(); return true; } return false; } Customizing the Action Items and Application Icon In the previous example, the menu items are displayed without the text. If you want to display the text for the action item together with the icon, you could use the “|” operator together with the MenuItem.SHOW_AS_ACTION_WITH_TEXT constant: MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”); { mnu1.setIcon(R.drawable.ic_launcher); mnu1.setShowAsAction( MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); } This causes the icon to be displayed together with the text of the menu item (see Figure 3-36). FIGURE 3-36 Besides clicking the action items, users can also click the application icon on the Action Bar. When the application icon is clicked, the onOptionsItemSelected() method is called. To identify the application icon being called, you check the item ID against the android.R.id.home constant: private boolean MenuChoice(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Toast.makeText(this, “You clicked on the Application icon”, Toast.LENGTH_LONG).show(); c03.indd 144 25/01/12 8:31 AM
  • Utilizing the Action Bar ❘ 145 return true; case 0: Toast.makeText(this, “You clicked on Item 1”, Toast.LENGTH_LONG).show(); return true; case 1: //... } return false; } To make the application icon clickable, you need to call the setDisplayHomeAsUpEnabled() method: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); //actionBar.hide(); //actionBar.show(); //---show it again--} Figure 3-37 shows the arrow button displayed next to the application icon. FIGURE 3-37 c03.indd 145 25/01/12 8:31 AM
  • 146 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE The application icon is often used by applications to enable them to return to the main activity of the application. For example, your application may have several activities, and you can use the application icon as a shortcut for users to return directly to the main activity of your application. To do this, it is always a good practice to create an Intent object and set it using the Intent.FLAG_ ACTIVITY_CLEAR_TOP flag: android.R.id.home: Toast.makeText(this, “You clicked on the Application icon”, Toast.LENGTH_LONG).show(); case Intent i = new Intent(this, MyActionBarActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); return true; The Intent.FLAG_ACTIVITY_CLEAR_TOP flag ensures that the series of activities in the back stack is cleared when the user clicks the application icon on the Action Bar. This way, if the user clicks the back button, the other activities in the application do not appear again. CREATING THE USER INTERFACE PROGRAMMATICALLY So far, all the UIs you have seen in this chapter are created using XML. As mentioned earlier, besides using XML you can also create the UI using code. This approach is useful if your UI needs to be dynamically generated during runtime. For example, suppose you are building a cinema ticket reservation system and your application will display the seats of each cinema using buttons. In this case, you would need to dynamically generate the UI based on the cinema selected by the user. The following Try It Out demonstrates the code needed to dynamically build the UI in your activity. TRY IT OUT Creating the UI via Code codefile UICode.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it UICode. In the UICodeActivity.java fi le, add the following statements in bold: package net.learn2develop.UICode; import import import import import import android.app.Activity; android.os.Bundle; android.view.ViewGroup.LayoutParams; android.widget.Button; android.widget.LinearLayout; android.widget.TextView; public class UICodeActivity extends Activity { /** Called when the activity is first created. */ @Override c03.indd 146 25/01/12 8:31 AM
  • Creating the User Interface Programmatically ❘ 147 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); //---param for views--LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //---create a layout--LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); //---create a textview--TextView tv = new TextView(this); tv.setText(“This is a TextView”); tv.setLayoutParams(params); //---create a button--Button btn = new Button(this); btn.setText(“This is a Button”); btn.setLayoutParams(params); //---adds the textview--layout.addView(tv); //---adds the button--layout.addView(btn); //---create a layout param for the layout--LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ); this.addContentView(layout, layoutParam); } } 3. Press F11 to debug the application on the Android emulator. Figure 3-38 shows the activity created. FIGURE 3-38 How It Works In this example, you fi rst commented out the setContentView() statement so that it does not load the UI from the main.xml fi le. You then created a LayoutParams object to specify the layout parameter that can be used by other views (which you will create next): //---param for views--LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); c03.indd 147 25/01/12 8:31 AM
  • 148 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE You also created a LinearLayout object to contain all the views in your activity: //---create a layout--LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); Next, you created a TextView and a Button view: //---create a textview--TextView tv = new TextView(this); tv.setText(“This is a TextView”); tv.setLayoutParams(params); //---create a button--Button btn = new Button(this); btn.setText(“This is a Button”); btn.setLayoutParams(params); You then added them to the LinearLayout object: //---adds the textview--layout.addView(tv); //---adds the button--layout.addView(btn); You also created a LayoutParams object to be used by the LinearLayout object: //---create a layout param for the layout--LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ); Finally, you added the LinearLayout object to the activity: this.addContentView(layout, layoutParam); As you can see, using code to create the UI is quite a laborious affair. Hence, dynamically generate your UI using code only when necessary. LISTENING FOR UI NOTIFICATIONS Users interact with your UI at two levels: the activity level and the view level. At the activity level, the Activity class exposes methods that you can override. Some common methods that you can override in your activities include the following: ➤ onKeyDown — Called when a key was pressed and not handled by any of the views contained within the activity c03.indd 148 25/01/12 8:31 AM
  • Listening for UI Notifications ➤ ➤ ❘ 149 onKeyUp — Called when a key was released and not handled by any of the views contained within the activity onMenuItemSelected — Called when a panel’s menu item has been selected by the user (covered in Chapter 5) ➤ onMenuOpened — Called when a panel’s menu is opened by the user (covered in Chapter 5) Overriding Methods Defined in an Activity To demonstrate how activities interact with the user, the following example overrides some of the methods defi ned in the activity’s base class. TRY IT OUT Overriding Activity Methods codefile UIActivity.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it UIActivity. Add the following statements in bold to main.xml (replacing the TextView): <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”214dp” android:layout_height=”wrap_content” android:text=”Your Name” /> <EditText android:id=”@+id/txt1” android:layout_width=”214dp” android:layout_height=”wrap_content” /> <Button android:id=”@+id/btn1” android:layout_width=”106dp” android:layout_height=”wrap_content” android:text=”OK” /> <Button android:id=”@+id/btn2” android:layout_width=”106dp” android:layout_height=”wrap_content” android:text=”Cancel” /> </LinearLayout> 3. Add the following statements in bold to the UIActivityActivity.java fi le: package net.learn2develop.UIActivity; import android.app.Activity; import android.os.Bundle; c03.indd 149 25/01/12 8:31 AM
  • 150 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE import android.view.KeyEvent; import android.widget.Toast; public class UIActivityActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: Toast.makeText(getBaseContext(), “Center was clicked”, Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_LEFT: Toast.makeText(getBaseContext(), “Left arrow was clicked”, Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_RIGHT: Toast.makeText(getBaseContext(), “Right arrow was clicked”, Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_UP: Toast.makeText(getBaseContext(), “Up arrow was clicked”, Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_DOWN: Toast.makeText(getBaseContext(), “Down arrow was clicked”, Toast.LENGTH_LONG).show(); break; } return false; } 4. 5. c03.indd 150 Press F11 to debug the application on the Android emulator. When the activity is loaded, type some text into the EditText. Next, click the down arrow key on the directional pad. Observe the message shown on the screen, as shown in Figure 3-39. FIGURE 3-39 25/01/12 8:31 AM
  • Listening for UI Notifications ❘ 151 How It Works When the activity is loaded, the cursor will be blinking in the EditText view, as it has the focus. In the MainActivitiy class, you override the onKeyDown() method of the base Activity class: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: //... break; case KeyEvent.KEYCODE_DPAD_LEFT: //... break; case KeyEvent.KEYCODE_DPAD_RIGHT: //... break; case KeyEvent.KEYCODE_DPAD_UP: //... break; case KeyEvent.KEYCODE_DPAD_DOWN: //... break; } return false; } In Android, whenever you press any keys on your device, the view that currently has the focus will try to handle the event generated. In this case, when the EditText has the focus and you press a key, the EditText view handles the event and displays the character you have just pressed in the view. However, if you press the up or down directional arrow key, the EditText view does not handle this, and instead passes the event to the activity. In this case, the onKeyDown() method is called. In this example, you checked the key that was pressed and displayed a message indicating the key pressed. Observe that the focus is now also transferred to the next view, which is the OK button. Interestingly, if the EditText view already has some text in it and the cursor is at the end of the text, then clicking the left arrow key does not fi re the onKeyDown() method; it simply moves the cursor one character to the left. This is because the EditText view has already handled the event. If you press the right arrow key instead (when the cursor is at the end of the text), then the onKeyDown() method will be called (because now the EditText view will not be handling the event). The same applies when the cursor is at the beginning of the EditText view. Clicking the left arrow will fi re the onKeyDown() method, whereas clicking the right arrow will simply move the cursor one character to the right. With the OK button in focus, press the center button in the directional pad. Note that the message “Center was clicked” is not displayed. This is because the Button view itself is handling the click event. Hence, the event is not caught by the onKeyDown() method. c03.indd 151 25/01/12 8:31 AM
  • 152 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE Note also that the onKeyDown() method returns a boolean result. You should return true when you want to tell the system that you are done with the event and that the system should not proceed further with it. For example, consider the case when you return true after each key has been matched: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: Toast.makeText(getBaseContext(), “Center was clicked”, Toast.LENGTH_LONG).show(); //break; return true; case KeyEvent.KEYCODE_DPAD_LEFT: Toast.makeText(getBaseContext(), “Left arrow was clicked”, Toast.LENGTH_LONG).show(); //break; return true; case KeyEvent.KEYCODE_DPAD_RIGHT: Toast.makeText(getBaseContext(), “Right arrow was clicked”, Toast.LENGTH_LONG).show(); //break; return true; case KeyEvent.KEYCODE_DPAD_UP: Toast.makeText(getBaseContext(), “Up arrow was clicked”, Toast.LENGTH_LONG).show(); //break; return true; case KeyEvent.KEYCODE_DPAD_DOWN: Toast.makeText(getBaseContext(), “Down arrow was clicked”, Toast.LENGTH_LONG).show(); //break; return true; } return false; } If you test this, you will see that now you cannot navigate between the views using the arrow keys. Registering Events for Views Views can fi re events when users interact with them. For example, when a user touches a Button view, you need to service the event so that the appropriate action can be performed. To do so, you need to explicitly register events for views. c03.indd 152 25/01/12 8:31 AM
  • Listening for UI Notifications ❘ 153 Using the same example discussed in the previous section, recall that the activity has two Button views; therefore, you can register the button click events using an anonymous class, as shown here: package net.learn2develop.UIActivity; import import import import import import import android.app.Activity; android.os.Bundle; android.view.KeyEvent; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.Toast; public class UIActivityActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---the two buttons are wired to the same event handler--Button btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(btnListener); Button btn2 = (Button)findViewById(R.id.btn2); btn2.setOnClickListener(btnListener); } //---create an anonymous class to act as a button click listener--private OnClickListener btnListener = new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), ((Button) v).getText() + “ was clicked”, Toast.LENGTH_LONG).show(); } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //... } } If you now click either the OK button or the Cancel button, the appropriate message will be displayed (see Figure 3-40), proving that the event is wired up properly. c03.indd 153 25/01/12 8:31 AM
  • 154 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE FIGURE 3-40 Besides defi ning an anonymous class for the event handler, you can also defi ne an anonymous inner class to handle an event. The following example shows how you can handle the onFocusChange() method for the EditText view: import android.widget.EditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---the two buttons are wired to the same event handler--Button btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(btnListener); Button btn2 = (Button)findViewById(R.id.btn2); btn2.setOnClickListener(btnListener); //---create an anonymous inner class to act as an onfocus listener--EditText txt1 = (EditText)findViewById(R.id.txt1); txt1.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Toast.makeText(getBaseContext(), c03.indd 154 25/01/12 8:31 AM
  • Listening for UI Notifications ❘ 155 ((EditText) v).getId() + “ has focus - “ + hasFocus, Toast.LENGTH_LONG).show(); } }); } NOTE For this example, you should ensure that the onKeyDown() method returns a false, instead of true as you have tried in the previous section. As shown in Figure 3-41, when the EditText view receives the focus, a message is printed on the screen. FIGURE 3-41 Using the anonymous inner class, the click event handler for the two Buttons can also be rewritten as follows: //---the two buttons are wired to the same event handler--Button btn1 = (Button)findViewById(R.id.btn1); //btn1.setOnClickListener(btnListener); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //---do something--} }); Button btn2 = (Button)findViewById(R.id.btn2); c03.indd 155 25/01/12 8:31 AM
  • 156 ❘ CHAPTER 3 GETTING TO KNOW THE ANDROID USER INTERFACE //btn2.setOnClickListener(btnListener); btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //---do something--} }); Which method should you use to handle events? An anonymous class is useful if you have multiple views handled by one event handler. The anonymous inner class method (the latter method described) is useful if you have an event handler for a single view. SUMMARY In this chapter, you have learned how user interfaces are created in Android. You have also learned about the different layouts that you can use to position the views in your Android UI. Because Android devices support more than one screen orientation, you need to take special care to ensure that your UI can adapt to changes in screen orientation. EXERCISES 1. What is the difference between the dp unit and the px unit? Which one should you use to specify the dimension of a view? 2. Why is the AbsoluteLayout not recommended for use? 3. What is the difference between the onPause()method and the onSaveInstanceState() method? 4. Name the three methods you can override to save an activity’s state. In what instances should you use the various methods? 5. How do you add action items to the Action Bar? Answers to the exercises can be found in Appendix C. c03.indd 156 25/01/12 8:31 AM
  • ❘ 157 Summary WHAT YOU LEARNED IN THIS CHAPTER TOPIC KEY CONCEPTS LinearLayout Arranges views in a single column or single row AbsoluteLayout Enables you to specify the exact location of its children TableLayout Groups views into rows and columns RelativeLayout Enables you to specify how child views are positioned relative to each other FrameLayout A placeholder on screen that you can use to display a single view ScrollView A special type of FrameLayout in that it enables users to scroll through a list of views that occupy more space than the physical display allows Unit of Measure Use dp for specifying the dimension of views and sp for font size. Two ways to adapt to changes in orientation Anchoring, and resizing and repositioning Using different XML files for different orientations Use the layout folder for portrait UI, and layout-land for landscape UI. Three ways to persist activity state Use the onPause() method. Use the onSaveInstanceState() method. Use the onRetainNonConfigurationInstance() method. Getting the dimension of the current device Constraining the activity’s orientation Use the setRequestOrientation() method, or the android:screenOrientation attribute in the AndroidManifest.xml file. Action Bar Replaces the traditional title bar for older versions of Android Action items Action items are displayed on the right of the Action Bar. They are created just like options menus. Application icon c03.indd 157 Use the WindowManager class’s getDefaultDisplay() method. Usually used to return to the “home” activity of an application. It is advisable to use the Intent object with the Intent.FLAG_ ACTIVITY_CLEAR_TOP flag. 25/01/12 8:31 AM
  • c03.indd 158 25/01/12 8:31 AM
  • 4 Designing Your User Interface With Views WHAT YOU WILL LEARN IN THIS CHAPTER ➤ How to use the basic views in Android to design your user interface ➤ How to use the picker views to display lists of items ➤ How to use the list views to display lists of items ➤ How to use specialized fragments In the previous chapter, you learned about the various layouts that you can use to position your views in an activity. You also learned about the techniques you can use to adapt to different screen resolutions and sizes. In this chapter, you will take a look at the various views that you can use to design the user interface for your applications. In particular, you will learn about the following ViewGroups: ➤ Basic views — Commonly used views such as the TextView, EditText, and Button views ➤ Picker views — Views that enable users to select from a list, such as the TimePicker and DatePicker views ➤ List views — Views that display a long list of items, such as the ListView and the SpinnerView views ➤ Specialized fragments — Special fragments that perform specific functions Subsequent chapters cover the other views not covered in this chapter, such as the analog and digital clock views and other views for displaying graphics, and so on. c04.indd 159 25/01/12 8:33 AM
  • 160 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS USING BASIC VIEWS To get started, this section explores some of the basic views that you can use to design the UI of your Android applications: ➤ TextView ➤ EditText ➤ Button ➤ ImageButton ➤ CheckBox ➤ ToggleButton ➤ RadioButton ➤ RadioGroup These basic views enable you to display text information, as well as perform some basic selection. The following sections explore all these views in more detail. TextView View When you create a new Android project, Eclipse always creates the main.xml fi le (located in the res/layout folder), which contains a <TextView> element: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> </LinearLayout> The TextView view is used to display text to the user. This is the most basic view and one that you will frequently use when you develop Android applications. If you need to allow users to edit the text displayed, you should use the subclass of TextView, EditText, which is discussed in the next section. NOTE In some other platforms, the TextView is commonly known as the label view. Its sole purpose is to display text on the screen. c04.indd 160 25/01/12 8:33 AM
  • Using Basic Views ❘ 161 Button, ImageButton, EditText, CheckBox, ToggleButton, RadioButton, and RadioGroup Views Besides the TextView view, which you will likely use the most often, there are some other basic views that you will fi nd yourself frequently using: ➤ Button — Represents a push-button widget ➤ ImageButton — Similar to the Button view, except that it also displays an image ➤ EditText — A subclass of the TextView view that allows users to edit its text content ➤ CheckBox — A special type of button that has two states: checked or unchecked ➤ RadioGroup and RadioButton — The RadioButton has two states: either checked or unchecked.. A RadioGroup is used to group together one or more RadioButton views, thereby allowing only one RadioButton to be checked within the RadioGroup. ➤ ToggleButton — Displays checked/unchecked states using a light indicator The following Try It Out provides details about how these views work. TRY IT OUT Using the Basic Views codefile BasicViews1.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it BasicViews1. Modify the main.xml fi le located in the res/layout folder by adding the following elements shown in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btnSave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”save” /> <Button android:id=”@+id/btnOpen” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Open” /> <ImageButton android:id=”@+id/btnImg1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:src=”@drawable/ic_launcher” /> <EditText android:id=”@+id/txtName” android:layout_width=”fill_parent” c04.indd 161 25/01/12 8:33 AM
  • 162 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS android:layout_height=”wrap_content” /> <CheckBox android:id=”@+id/chkAutosave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Autosave” /> <CheckBox android:id=”@+id/star” style=”?android:attr/starStyle” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <RadioGroup android:id=”@+id/rdbGp1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” > <RadioButton android:id=”@+id/rdb1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Option 1” /> <RadioButton android:id=”@+id/rdb2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Option 2” /> </RadioGroup> <ToggleButton android:id=”@+id/toggle1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 3. To see the views in action, debug the project in Eclipse by selecting the project name and pressing F11. Figure 4-1 shows the various views displayed in the Android emulator. 4. Click the various views and note how they vary in their look and feel. Figure 4-2 shows the following changes to the view: ➤ ➤ The second CheckBox View (star) is selected. ➤ The second RadioButton (Option 2) is selected. ➤ c04.indd 162 The first CheckBox view (Autosave) is checked. The ToggleButton is turned on. 25/01/12 8:33 AM
  • Using Basic Views FIGURE 4-1 ❘ 163 FIGURE 4-2 How It Works So far, all the views are relatively straightforward — they are listed using the <LinearLayout> element, so they are stacked on top of each other when they are displayed in the activity. For the fi rst Button, the layout_width attribute is set to fill_parent so that its width occupies the entire width of the screen: <Button android:id=”@+id/btnSave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”save” /> For the second Button, the layout_width attribute is set to wrap_content so that its width will be the width of its content — specifically, the text that it is displaying (i.e.,“Open”): <Button android:id=”@+id/btnOpen” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Open” /> The ImageButton displays a button with an image. The image is set through the src attribute. In this case, you simply used the image for the application icon: <ImageButton android:id=”@+id/btnImg1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:src=”@drawable/ic_launcher” /> c04.indd 163 25/01/12 8:33 AM
  • 164 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS The EditText view displays a rectangular region where the user can enter some text. You set the layout_height to wrap_content so that if the user enters a long string of text, its height will automatically be adjusted to fit the content (see Figure 4-3). <EditText android:id=”@+id/txtName” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> FIGURE 4-3 The CheckBox displays a checkbox that users can tap to check or uncheck: <CheckBox android:id=”@+id/chkAutosave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Autosave” /> If you do not like the default look of the CheckBox, you can apply a style attribute to it to display it as another image, such as a star: <CheckBox android:id=”@+id/star” style=”?android:attr/starStyle” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> The format for the value of the style attribute is as follows: ?[package:][type:]name The RadioGroup encloses two RadioButtons. This is important because radio buttons are usually used to present multiple options to the user for selection. When a RadioButton in a RadioGroup is selected, all other RadioButtons are automatically unselected: <RadioGroup android:id=”@+id/rdbGp1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” c04.indd 164 25/01/12 8:33 AM
  • ❘ 165 Using Basic Views android:orientation=”vertical” > <RadioButton android:id=”@+id/rdb1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Option 1” /> <RadioButton android:id=”@+id/rdb2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Option 2” /> </RadioGroup> Notice that the RadioButtons are listed vertically, one on top of another. If you want to list them horizontally, you need to change the orientation attribute to horizontal. You would also need to ensure that the layout_width attribute of the RadioButtons are set to wrap_content: <RadioGroup android:id=”@+id/rdbGp1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”horizontal” > <RadioButton android:id=”@+id/rdb1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Option 1” /> <RadioButton android:id=”@+id/rdb2” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Option 2” /> </RadioGroup> Figure 4-4 shows the RadioButtons displayed horizontally. The ToogleButton displays a rectangular button that users can toggle on and off by clicking: FIGURE 4-4 <ToggleButton android:id=”@+id/toggle1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> One thing that has been consistent throughout this example is that each view has the id attribute set to a particular value, such as in the case of the Button: <Button android:id=”@+id/btnSave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/save” /> The id attribute is an identifier for a view so that it may later be retrieved using the View.findViewById() or Activity.findViewById() methods. c04.indd 165 25/01/12 8:33 AM
  • 166 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS The various views that you have just seen were tested on an Android emulator emulating an Android 4.0 smartphone. What will they look like when run on older versions of Android devices? What about Android tablets? Figure 4-5 shows what your activity will look like if you change the android:minSdkVersion attribute in the AndroidManifest.xml file to 10 and run it on the Google Nexus S running Android 2.3.6: <uses-sdk android:minSdkVersion=”10” /> Figure 4-6 shows what your activity will look like if you change the android:minSdkVersion attribute in the AndroidManifest.xml fi le to 13 and run it on the Asus Eee Pad Transformer running Android 3.2.1. FIGURE 4-5 FIGURE 4-6 If you now run it on the Asus Eee Pad Transformer running Android 3.2.1 with the android:minSdkVersion attribute set to 8 or smaller, you will see the additional button that appears in Figure 4-7. c04.indd 166 25/01/12 8:33 AM
  • Using Basic Views ❘ 167 FIGURE 4-7 Tapping on the button will reveal the option to stretch the activity to fill the entire screen (default) or zoom the activity to fill the screen (see Figure 4-8). FIGURE 4-8 c04.indd 167 25/01/12 8:33 AM
  • 168 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS In short, applications with the minimum SDK version set to 8 or lower can be displayed at the screen ratios with which they were originally designed, or they can automatically stretch to fi ll the screen (default behavior). Now that you have seen what the various views for an activity look like, the following Try It Out demonstrates how you can programmatically control them. TRY IT OUT 1. Handling View Events Using the BasicViews1 project created in the previous Try It Out, modify the BasicViews1Activity .java file by adding the following statements in bold: package net.learn2develop.BasicViews1; import import import import import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.widget.Button; android.widget.CheckBox; android.widget.RadioButton; android.widget.RadioGroup; android.widget.RadioGroup.OnCheckedChangeListener; android.widget.Toast; android.widget.ToggleButton; public class BasicViews1Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---Button view--Button btnOpen = (Button) findViewById(R.id.btnOpen); btnOpen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DisplayToast(“You have clicked the Open button”); } }); //---Button view--Button btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DisplayToast(“You have clicked the Save button”); } }); //---CheckBox--CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave); checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { c04.indd 168 25/01/12 8:33 AM
  • Using Basic Views ❘ 169 if (((CheckBox)v).isChecked()) DisplayToast(“CheckBox is checked”); else DisplayToast(“CheckBox is unchecked”); } }); //---RadioButton--RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1); if (rb1.isChecked()) { DisplayToast(“Option 1 checked!”); } else { DisplayToast(“Option 2 checked!”); } } }); //---ToggleButton--ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle1); toggleButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((ToggleButton)v).isChecked()) DisplayToast(“Toggle button is On”); else DisplayToast(“Toggle button is Off”); } }); } private void DisplayToast(String msg) { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); } } 2. 3. Press F11 to debug the project on the Android emulator. Click on the various views and observe the message displayed in the Toast window. How It Works To handle the events fi red by each view, you fi rst have to programmatically locate the view that you created during the onCreate() event. You do so using the findViewById() method (belonging to the Activity base class), supplying it with the ID of the view: //---Button view--Button btnOpen = (Button) findViewById(R.id.btnOpen); c04.indd 169 25/01/12 8:33 AM
  • 170 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS The setOnClickListener() method registers a callback to be invoked later when the view is clicked: btnOpen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DisplayToast(“You have clicked the Open button”); } }); The onClick() method is called when the view is clicked. To determine the state of the CheckBox, you have to typecast the argument of the onClick() method to a CheckBox and then check its isChecked() method to see if it is checked: CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave); checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((CheckBox)v).isChecked()) DisplayToast(“CheckBox is checked”); else DisplayToast(“CheckBox is unchecked”); } }); For the RadioButton, you need to use the setOnCheckedChangeListener() method on the RadioGroup to register a callback to be invoked when the checked RadioButton changes in this group: //---RadioButton--RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1); if (rb1.isChecked()) { DisplayToast(“Option 1 checked!”); } else { DisplayToast(“Option 2 checked!”); } } }); When a RadioButton is selected, the onCheckedChanged() method is fi red. Within it, you locate individual RadioButtons and then call their isChecked() method to determine which RadioButton is selected. Alternatively, the onCheckedChanged() method contains a second argument that contains a unique identifier of the RadioButton selected. The ToggleButton works just like the CheckBox. c04.indd 170 25/01/12 8:33 AM
  • Using Basic Views ❘ 171 So far, to handle the events on the views, you fi rst had to get a reference to the view and then register a callback to handle the event. There is another way to handle view events. Using the Button as an example, you can add an attribute called onClick to it: <Button android:id=”@+id/btnSave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/save” android:onClick=”btnSaved_clicked”/> The onClick attribute specifies the click event of the button. The value of this attribute is the name of the event handler. Therefore, to handle the click event of the button, you simply need to create a method called btnSaved_clicked, as shown in the following example (note that the method must have a single parameter of type View): public class BasicViews1Activity extends Activity { public void btnSaved_clicked (View view) { DisplayToast(“You have clicked the Save button1”); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //... } private void DisplayToast(String msg) { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); } } If you compare this approach to the earlier ones used, this is much simpler. Which method you use is really up to you, but this book mostly uses the latter approach. ProgressBar View The ProgressBar view provides visual feedback about some ongoing tasks, such as when you are performing a task in the background. For example, you might be downloading some data from the web and need to update the user about the status of the download. In this case, the ProgressBar view is a good choice for this task. The following activity demonstrates how to use this view. c04.indd 171 25/01/12 8:33 AM
  • 172 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS TRY IT OUT Using the ProgressBar View codefile BasicViews2.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it BasicViews2. Modify the main.xml fi le located in the res/layout folder by adding the following code in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <ProgressBar android:id=”@+id/progressbar” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 3. In the BasicViews2Activity.java fi le, add the following statements in bold: package net.learn2develop.BasicViews2; import import import import import android.app.Activity; android.os.Bundle; android.os.Handler; android.view.View; android.widget.ProgressBar; public class BasicViews2Activity extends Activity { static int progress; ProgressBar progressBar; int progressStatus = 0; Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progress = 0; progressBar = (ProgressBar) findViewById(R.id.progressbar); //---do some work in background thread--new Thread(new Runnable() { public void run() { //---do some work here--while (progressStatus < 10) { progressStatus = doSomeWork(); } //---hides the progress bar--- c04.indd 172 25/01/12 8:33 AM
  • Using Basic Views ❘ 173 handler.post(new Runnable() { public void run() { //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE--progressBar.setVisibility(View.GONE); } }); } //---do some long running work here--private int doSomeWork() { try { //---simulate doing some work--Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return ++progress; } }).start(); } } 4. Press F11 to debug the project on the Android emulator. Figure 4-9 shows the ProgressBar animating. After about five seconds, it will disappear. FIGURE 4-9 How It Works The default mode of the ProgressBar view is indeterminate — that is, it shows a cyclic animation. This mode is useful for tasks that do not have specific completion times, such as when you are sending some data to a web service and waiting for the server to respond. If you simply put the <ProgressBar> element in your main.xml fi le, it will display a spinning icon continuously. It is your responsibility to stop it when your background task has completed. The code that you have added in the Java fi le shows how you can spin off a background thread to simulate performing some long-running tasks. To do so, you use the Thread class together with a Runnable object. The run() method starts the execution of the thread, which in this case calls the doSomeWork() method to simulate doing some work. When the simulated work is done (after about five seconds), you use a Handler object to send a message to the thread to dismiss the ProgressBar: //---do some work in background thread--new Thread(new Runnable() { public void run() { //---do some work here--while (progressStatus < 10) { progressStatus = doSomeWork(); c04.indd 173 25/01/12 8:33 AM
  • 174 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS } //---hides the progress bar--handler.post(new Runnable() { public void run() { //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE--progressBar.setVisibility(View.GONE); } }); } //---do some long running work here--private int doSomeWork() { try { //---simulate doing some work--Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return ++progress; } }).start(); When the task is completed, you hide the ProgressBar by setting its Visibility property to View .GONE (value 8). The difference between the INVISIBLE and GONE constants is that the INVISIBLE constant simply hides the ProgressBar (the region occupied by the ProgressBar is still taking up space in the activity); whereas the GONE constant removes the ProgressBar view from the activity and does not take up any space on it. The next Try It Out shows how you can change the look of the ProgressBar. TRY IT OUT 1. Customizing the ProgressBar View Using the BasicViews2 project created in the previous Try It Out, modify the main.xml fi le as shown here: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <ProgressBar android:id=”@+id/progressbar” android:layout_width=”wrap_content” android:layout_height=”wrap_content” style=”@android:style/Widget.ProgressBar.Horizontal” /> </LinearLayout> c04.indd 174 25/01/12 8:33 AM
  • Using Basic Views 2. ❘ 175 Modify the BasicViews2Activity.java fi le by adding the following statements in bold: package net.learn2develop.BasicViews2; import import import import import android.app.Activity; android.os.Bundle; android.os.Handler; android.view.View; android.widget.ProgressBar; public class BasicViews2Activity extends Activity { static int progress; ProgressBar progressBar; int progressStatus = 0; Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progress = 0; progressBar = (ProgressBar) findViewById(R.id.progressbar); progressBar.setMax(200); //---do some work in background thread--new Thread(new Runnable() { public void run() { //---do some work here--while (progressStatus < 100) { progressStatus = doSomeWork(); //---Update the progress bar--handler.post(new Runnable() { public void run() { progressBar.setProgress(progressStatus); } }); } //---hides the progress bar--handler.post(new Runnable() { public void run() { //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE--progressBar.setVisibility(View.GONE); } }); c04.indd 175 25/01/12 8:33 AM
  • 176 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS } //---do some long running work here--private int doSomeWork() { try { //---simulate doing some work--Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return ++progress; } }).start(); } } 3. Press F11 to debug the project on the Android emulator. 4. Figure 4-10 shows the ProgressBar displaying the progress. The ProgressBar disappears when the progress reaches 50%. FIGURE 4-10 How It Works To make the ProgressBar display horizontally, simply set its style attribute to @android:style/ Widget.ProgressBar.Horizontal: <ProgressBar android:id=”@+id/progressbar” android:layout_width=”wrap_content” android:layout_height=”wrap_content” style=”@android:style/Widget.ProgressBar.Horizontal” /> To display the progress, call its setProgress() method, passing in an integer indicating its progress: //---Update the progress bar--handler.post(new Runnable() { public void run() { progressBar.setProgress(progressStatus); } }); In this example, you set the range of the ProgressBar from 0 to 200 (via the setMax() method). Hence, the ProgressBar will stop and then disappear when it is halfway through (since you only continue to call the doSomeWork() method as long as the progressStatus is less than 100). To ensure that the ProgressBar disappears only when the progress reaches 100%, either set the maximum value to 100 or modify the while loop to stop when the progressStatus reaches 200, like this: //---do some work here--while (progressStatus < 200) c04.indd 176 25/01/12 8:33 AM
  • ❘ 177 Using Basic Views Besides the horizontal style for the ProgressBar that you have used for this example, you can also use the following constants: ➤ Widget.ProgressBar.Horizontal ➤ Widget.ProgressBar.Small ➤ Widget.ProgressBar.Large ➤ Widget.ProgressBar.Inverse ➤ Widget.ProgressBar.Small.Inverse ➤ Widget.ProgressBar.Large.Inverse AutoCompleteTextView View The AutoCompleteTextView is a view that is similar to EditText (in fact it is a subclass of EditText), except that it shows a list of completion suggestions automatically while the user is typing. The following Try It Out shows how to use the AutoCompleteTextView to automatically help users complete the text entry. TRY IT OUT Using the AutoCompleteTextView codefile BasicViews3.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it BasicViews3. Modify the main.xml fi le located in the res/layout folder as shown here in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Name of President” /> <AutoCompleteTextView android:id=”@+id/txtCountries” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> </LinearLayout> 3. Add the following statements in bold to the BasicViews3Activity.java fi le: package net.learn2develop.BasicViews3; import android.app.Activity; import android.os.Bundle; c04.indd 177 25/01/12 8:33 AM
  • 178 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class BasicViews3Activity extends Activity { String[] presidents = { “Dwight D. Eisenhower”, “John F. Kennedy”, “Lyndon B. Johnson”, “Richard Nixon”, “Gerald Ford”, “Jimmy Carter”, “Ronald Reagan”, “George H. W. Bush”, “Bill Clinton”, “George W. Bush”, “Barack Obama” }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, presidents); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries); textView.setThreshold(3); textView.setAdapter(adapter); } } 4. Press F11 to debug the application on the Android emulator. As shown in Figure 4-11, a list of matching names appears as you type into the AutoCompleteTextView. How It Works In the BasicViews3Activity class, you first created a String array containing a list of presidents’ names: String[] presidents = { “Dwight D. Eisenhower”, “John F. Kennedy”, “Lyndon B. Johnson”, “Richard Nixon”, “Gerald Ford”, “Jimmy Carter”, “Ronald Reagan”, “George H. W. Bush”, “Bill Clinton”, c04.indd 178 FIGURE 4-11 25/01/12 8:33 AM
  • ❘ 179 Using Picker Views “George W. Bush”, “Barack Obama” }; The ArrayAdapter object manages the array of strings that will be displayed by the AutoCompleteTextView. In the preceding example, you set the AutoCompleteTextView to display in the simple_dropdown_ item_1line mode: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, presidents); The setThreshold() method sets the minimum number of characters the user must type before the suggestions appear as a drop-down menu: textView.setThreshold(3); The list of suggestions to display for the AutoCompleteTextView is obtained from the ArrayAdapter object: textView.setAdapter(adapter); USING PICKER VIEWS Selecting the date and time is one of the common tasks you need to perform in a mobile application. Android supports this functionality through the TimePicker and DatePicker views. The following sections demonstrate how to use these views in your activity. TimePicker View The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode. The following Try It Out shows you how to use it. TRY IT OUT Using the TimePicker View codefile BasicViews4.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it BasicViews4. Modify the main.xml fi le located in the res/layout folder by adding the following lines in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TimePicker android:id=”@+id/timePicker” android:layout_width=”wrap_content” c04.indd 179 25/01/12 8:33 AM
  • 180 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS android:layout_height=”wrap_content” /> <Button android:id=”@+id/btnSet” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”I am all set!” android:onClick=”onClick” /> </LinearLayout> 3. Select the project name in Eclipse and press F11 to debug the application on the Android emulator. Figure 4-12 shows the TimePicker in action. Besides clicking the plus (+) and minus (-) buttons, you can use the numeric keypad on the device to change the hour and minute, and click the AM button to toggle between AM and PM. 4. Back in Eclipse, add the following statements in bold to the BasicViews4Activity.java fi le: package net.learn2develop.BasicViews4; import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.widget.TimePicker; android.widget.Toast; FIGURE 4-12 public class BasicViews4Activity extends Activity { TimePicker timePicker; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setIs24HourView(true); } public void onClick(View view) { Toast.makeText(getBaseContext(), “Time selected:” + timePicker.getCurrentHour() + “:” + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } } c04.indd 180 25/01/12 8:33 AM
  • Using Picker Views 5. ❘ 181 Press F11 to debug the application on the Android emulator. This time, the TimePicker will be displayed in the 24-hour format. Clicking the Button will display the time that you have set in the TimePicker (see Figure 4-13). How It Works The TimePicker displays a standard UI to enable users to set a time. By default, it displays the time in the AM/PM format. If you wish to display the time in the 24-hour format, you can use the setIs24HourView() method. To programmatically get the time set by the user, use the getCurrentHour() and getCurrentMinute() methods: Toast.makeText(getBaseContext(), “Time selected:” + timePicker.getCurrentHour() + “:” + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); FIGURE 4-13 NOTE The getCurrentHour() method always returns the hour in 24-hour format (i.e., a value from 0 to 23). Although you can display the TimePicker in an activity, it’s better to display it in a dialog window; that way, once the time is set, it disappears and doesn’t take up any space in an activity. The following Try It Out demonstrates how to do just that. TRY IT OUT 1. Using a Dialog to Display the TimePicker View Using the BasicViews4 project created in the previous Try It Out, modify the BasicViews4Activity .java file as shown here: package net.learn2develop.BasicViews4; import java.text.SimpleDateFormat; import java.util.Date; import import import import import import c04.indd 181 android.app.Activity; android.app.Dialog; android.app.TimePickerDialog; android.os.Bundle; android.view.View; android.widget.TimePicker; 25/01/12 8:33 AM
  • 182 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS import android.widget.Toast; public class BasicViews4Activity extends Activity { TimePicker timePicker; int hour, minute; static final int TIME_DIALOG_ID = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setIs24HourView(true); showDialog(TIME_DIALOG_ID); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog( this, mTimeSetListener, hour, minute, false); } return null; } private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet( TimePicker view, int hourOfDay, int minuteOfHour) { hour = hourOfDay; minute = minuteOfHour; SimpleDateFormat timeFormat = new SimpleDateFormat(“hh:mm aa”); Date date = new Date(0,0,0, hour, minute); String strDate = timeFormat.format(date); Toast.makeText(getBaseContext(), “You have selected “ + strDate, Toast.LENGTH_SHORT).show(); } }; public void onClick(View view) { Toast.makeText(getBaseContext(), “Time selected:” + c04.indd 182 25/01/12 8:33 AM
  • Using Picker Views ❘ 183 timePicker.getCurrentHour() + “:” + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } } 2. Press F11 to debug the application on the Android emulator. When the activity is loaded, you can see the TimePicker displayed in a dialog window (see Figure 4-14). Set a time and then click the Set button. You will see the Toast window displaying the time that you just set. How It Works To display a dialog window, you use the showDialog() method, passing it an ID to identify the source of the dialog: showDialog(TIME_DIALOG_ID); When the showDialog() method is called, the onCreateDialog() method will be called: @Override protected Dialog onCreateDialog(int id) { switch (id) { FIGURE 4-14 case TIME_DIALOG_ID: return new TimePickerDialog( this, mTimeSetListener, hour, minute, false); } return null; } Here, you create a new instance of the TimePickerDialog class, passing it the current context, the callback, the initial hour and minute, as well as whether the TimePicker should be displayed in 24-hour format. When the user clicks the Set button in the TimePicker dialog window, the onTimeSet() method is called: private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet( TimePicker view, int hourOfDay, int minuteOfHour) { hour = hourOfDay; minute = minuteOfHour; SimpleDateFormat timeFormat = new SimpleDateFormat(“hh:mm aa”); Date date = new Date(0,0,0, hour, minute); c04.indd 183 25/01/12 8:33 AM
  • 184 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS String strDate = timeFormat.format(date); Toast.makeText(getBaseContext(), “You have selected “ + strDate, Toast.LENGTH_SHORT).show(); } }; Here, the onTimeSet() method contains the hour and minute set by the user via the hourOfDay and minuteOfHour arguments, respectively. DatePicker View Another view that is similar to the TimePicker is the DatePicker. Using the DatePicker, you can enable users to select a particular date on the activity. The following Try It Out shows you how to use the DatePicker. TRY IT OUT 1. Using the DatePicker View Using the BasicViews4 project created earlier, modify the main.xml fi le as shown here: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btnSet” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”I am all set!” android:onClick=”onClick” /> <DatePicker android:id=”@+id/datePicker” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <TimePicker android:id=”@+id/timePicker” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 2. c04.indd 184 Press F11 to debug the application on the Android emulator. Figure 4-15 shows the DatePicker view (you have to change the emulator’s orientation to landscape by pressing Ctrl-F11; portrait mode is too narrow to display the DatePicker). 25/01/12 8:33 AM
  • Using Picker Views ❘ 185 FIGURE 4-15 3. Back in Eclipse, add the following statements in bold to the BasicViews4Activity.java fi le: package net.learn2develop.BasicViews4; import java.text.SimpleDateFormat; import java.util.Date; import import import import import import import import android.app.Activity; android.app.Dialog; android.app.TimePickerDialog; android.os.Bundle; android.view.View; android.widget.DatePicker; android.widget.TimePicker; android.widget.Toast; public class BasicViews4Activity extends Activity { TimePicker timePicker; DatePicker datePicker; int hour, minute; static final int TIME_DIALOG_ID = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); c04.indd 185 25/01/12 8:33 AM
  • 186 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS setContentView(R.layout.main); timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setIs24HourView(true); // showDialog(TIME_DIALOG_ID); datePicker = (DatePicker) findViewById(R.id.datePicker); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog( this, mTimeSetListener, hour, minute, false); } return null; } private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet( TimePicker view, int hourOfDay, int minuteOfHour) { hour = hourOfDay; minute = minuteOfHour; SimpleDateFormat timeFormat = new SimpleDateFormat(“hh:mm aa”); Date date = new Date(0,0,0, hour, minute); String strDate = timeFormat.format(date); Toast.makeText(getBaseContext(), “You have selected “ + strDate, Toast.LENGTH_SHORT).show(); } }; public void onClick(View view) { Toast.makeText(getBaseContext(), “Date selected:” + (datePicker.getMonth() + 1) + “/” + datePicker.getDayOfMonth() + “/” + datePicker.getYear() + “n” + “Time selected:” + timePicker.getCurrentHour() + “:” + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } } 4. Press F11 to debug the application on the Android emulator. Once the date is set, clicking the Button will display the date set (see Figure 4-16). c04.indd 186 25/01/12 8:33 AM
  • Using Picker Views ❘ 187 FIGURE 4-16 How It Works Like the TimePicker, you call the getMonth(), getDayOfMonth(), and getYear() methods to get the month, day, and year, respectively: “Date selected:” + (datePicker.getMonth() + 1) + “/” + datePicker.getDayOfMonth() + “/” + datePicker.getYear() + “n” + Note that the getMonth()method returns 0 for January, 1 for February, and so on. Hence, you need to increment the result of this method by one to get the corresponding month number. Like the TimePicker, you can also display the DatePicker in a dialog window. The following Try It Out shows you how. TRY IT OUT 1. Using a Dialog to Display the DatePicker View Using the BasicViews4 project created earlier, add the following statements in bold to the BasicViews4Activity.java fi le: package net.learn2develop.BasicViews4; import java.text.SimpleDateFormat; import java.util.Calendar; c04.indd 187 25/01/12 8:33 AM
  • 188 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS import java.util.Date; import import import import import import import import import android.app.Activity; android.app.DatePickerDialog; android.app.Dialog; android.app.TimePickerDialog; android.os.Bundle; android.view.View; android.widget.DatePicker; android.widget.TimePicker; android.widget.Toast; public class BasicViews4Activity extends Activity { TimePicker timePicker; DatePicker datePicker; int hour, minute; int yr, month, day; static final int TIME_DIALOG_ID = 0; static final int DATE_DIALOG_ID = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setIs24HourView(true); // showDialog(TIME_DIALOG_ID); datePicker = (DatePicker) findViewById(R.id.datePicker); //---get the current date--Calendar today = Calendar.getInstance(); yr = today.get(Calendar.YEAR); month = today.get(Calendar.MONTH); day = today.get(Calendar.DAY_OF_MONTH); showDialog(DATE_DIALOG_ID); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog( this, mTimeSetListener, hour, minute, false); case DATE_DIALOG_ID: return new DatePickerDialog( c04.indd 188 25/01/12 8:33 AM
  • Using Picker Views ❘ 189 this, mDateSetListener, yr, month, day); } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth) { yr = year; month = monthOfYear; day = dayOfMonth; Toast.makeText(getBaseContext(), “You have selected : “ + (month + 1) + “/” + day + “/” + year, Toast.LENGTH_SHORT).show(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet( TimePicker view, int hourOfDay, int minuteOfHour) { hour = hourOfDay; minute = minuteOfHour; SimpleDateFormat timeFormat = new SimpleDateFormat(“hh:mm aa”); Date date = new Date(0,0,0, hour, minute); String strDate = timeFormat.format(date); Toast.makeText(getBaseContext(), “You have selected “ + strDate, Toast.LENGTH_SHORT).show(); } }; public void onClick(View view) { Toast.makeText(getBaseContext(), “Date selected:” + (datePicker.getMonth() + 1) + “/” + datePicker.getDayOfMonth() + “/” + datePicker.getYear() + “n” + “Time selected:” + timePicker.getCurrentHour() + “:” + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } } c04.indd 189 25/01/12 8:33 AM
  • 190 2. ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS Press F11 to debug the application on the Android emulator. When the activity is loaded, you can see the DatePicker displayed in a dialog window (see Figure 4-17). Select a date and then click the Set button. The Toast window will display the date you have just set. FIGURE 4-17 How It Works The DatePicker works exactly like the TimePicker. When a date is set, it fi res the onDateSet() method, where you can obtain the date set by the user: public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth) { yr = year; month = monthOfYear; day = dayOfMonth; Toast.makeText(getBaseContext(), “You have selected : “ + (month + 1) + “/” + day + “/” + year, Toast.LENGTH_SHORT).show(); } Note that you have to initialize the three variables — yr, month, and day — before showing the dialog: //---get the current date--Calendar today = Calendar.getInstance(); yr = today.get(Calendar.YEAR); c04.indd 190 25/01/12 8:33 AM
  • Using List Views to Display Long Lists ❘ 191 month = today.get(Calendar.MONTH); day = today.get(Calendar.DAY_OF_MONTH); showDialog(DATE_DIALOG_ID); If you don’t, you will get an illegal argument exception error (“current should be >= start and <= end”) during runtime when you create an instance of the DatePickerDialog class. USING LIST VIEWS TO DISPLAY LONG LISTS List views are views that enable you to display a long list of items. In Android, there are two types of list views: ListView and SpinnerView. Both are useful for displaying long lists of items. The following Try It Outs show them in action. ListView View The ListView displays a list of items in a vertically scrolling list. The following Try It Out demonstrates how to display a list of items using the ListView. TRY IT OUT 1. Displaying a Long List of Items Using the ListView Using Eclipse, create an Android project and name it BasicViews5. codefile BasicViews5.zip available for download at Wrox.com 2. Modify the BasicViews5Activity.java fi le by inserting the statements shown here in bold: package net.learn2develop.BasicViews5; import import import import import import android.app.ListActivity; android.os.Bundle; android.view.View; android.widget.ArrayAdapter; android.widget.ListView; android.widget.Toast; public class BasicViews5Activity extends ListActivity String[] presidents = { “Dwight D. Eisenhower”, “John F. Kennedy”, “Lyndon B. Johnson”, “Richard Nixon”, “Gerald Ford”, “Jimmy Carter”, “Ronald Reagan”, “George H. W. Bush”, “Bill Clinton”, “George W. Bush”, c04.indd 191 { 25/01/12 8:33 AM
  • 192 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS “Barack Obama” }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---no need to call this--//setContentView(R.layout.main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents)); } public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, “You have selected “ + presidents[position], Toast.LENGTH_SHORT).show(); } } 3. Press F11 to debug the application on the Android emulator. Figure 4-18 shows the activity displaying the list of presidents’ names. 4. Click on an item. A message containing the item selected will be displayed. How It Works The first thing to notice in this example is that the BasicViews5Activity class extends the ListActivity class. The ListActivity class extends the Activity class and it displays a list of items by binding to a data source. Also note that there is no need to modify the main.xml file to include the ListView; the ListActivity class itself contains a ListView. Hence, in the onCreate() method, you don’t need to call the setContentView() method to load the UI from the main.xml file: //---no need to call this--//setContentView(R.layout.main); FIGURE 4-18 In the onCreate() method, you use the setListAdapter() method to programmatically fill the entire screen of the activity with a ListView. The ArrayAdapter object manages the array of strings that will be displayed by the ListView. In the preceding example, you set the ListView to display in the simple_list_item_1 mode: setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents)); c04.indd 192 25/01/12 8:33 AM
  • ❘ 193 Using List Views to Display Long Lists The onListItemClick() method is fi red whenever an item in the ListView has been clicked: public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, “You have selected “ + presidents[position], Toast.LENGTH_SHORT).show(); } Here, you simply display the name of the president selected using the Toast class. Customizing the ListView The ListView is a versatile view that you can further customize. The following Try It Out shows how you can allow multiple items in the ListView to be selected and how you can enable filtering support. TRY IT OUT 1. Enabling Filtering and Multi-Item Support in the ListView Using the BasicViews5 project created in the previous section, add the following statements in bold to the BasicViews5Activity.java fi le: /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---no need to call this--//setContentView(R.layout.main); ListView lstView = getListView(); //lstView.setChoiceMode(ListView.CHOICE_MODE_NONE); //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lstView.setTextFilterEnabled(true); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, presidents)); } 2. c04.indd 193 Press F11 to debug the application on the Android emulator. You can now click on each item to display the check icon next to it (see Figure 4-19). 25/01/12 8:33 AM
  • 194 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS FIGURE 4-19 How It Works To programmatically get a reference to the ListView object, you use the getListView() method, which fetches the ListActivity’s list view. You need to do this so that you can programmatically modify the behavior of the ListView. In this case, you used the setChoiceMode() method to specify how the ListView should handle a user’s click. For this example, you set it to ListView.CHOICE_MODE_ MULTIPLE, which means that the user can select multiple items: ListView lstView = getListView(); //lstView.setChoiceMode(ListView.CHOICE_MODE_NONE); //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); A very cool feature of the ListView is its support for fi ltering. When you enable fi ltering through the setTextFilterEnabled() method, users will be able to type on the keypad and the ListView will automatically fi lter the items to match what was typed: lstView.setTextFilterEnabled(true); Figure 4-20 shows the list fi ltering in action. Here, all items in the list that contain the word “john” will appear in the result list. c04.indd 194 25/01/12 8:33 AM
  • Using List Views to Display Long Lists ❘ 195 FIGURE 4-20 While the previous example shows that the list of presidents’ names is stored in an array, in a reallife application it is recommended that you either retrieve them from a database or at least store them in the strings.xml fi le. The following Try It Out shows you how. TRY IT OUT 1. Storing Items in the strings.xml File Using the BasicViews5 project created earlier, add the following lines in bold to the strings.xml fi le located in the res/values folder: <?xml version=”1.0” encoding=”utf-8”?> <resources> <string name=”hello”>Hello World, BasicViews5Activity!</string> <string name=”app_name”>BasicViews5</string> <string-array name=”presidents_array”> <item>Dwight D. Eisenhower</item> <item>John F. Kennedy</item> <item>Lyndon B. Johnson</item> <item>Richard Nixon</item> <item>Gerald Ford</item> <item>Jimmy Carter</item> <item>Ronald Reagan</item> <item>George H. W. Bush</item> <item>Bill Clinton</item> <item>George W. Bush</item> c04.indd 195 25/01/12 8:33 AM
  • 196 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS <item>Barack Obama</item> </string-array> </resources> 2. Modify the BasicViews5Activity.java fi le as shown in bold: public class BasicViews5Activity extends ListActivity String[] presidents; { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---no need to call this--//setContentView(R.layout.main); ListView lstView = getListView(); //lstView.setChoiceMode(ListView.CHOICE_MODE_NONE); //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lstView.setTextFilterEnabled(true); presidents = getResources().getStringArray(R.array.presidents_array); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, presidents)); } public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, “You have selected “ + presidents[position], Toast.LENGTH_SHORT).show(); } } 3. Press F11 to debug the application on the Android emulator. You should see the same list of names that appeared in the previous Try It Out. How It Works With the names now stored in the strings.xml fi le, you can retrieve it programmatically in the BasicViews5Activity.java fi le using the getResources() method: presidents = getResources().getStringArray(R.array.presidents_array); In general, you can programmatically retrieve resources bundled with your application using the getResources() method. c04.indd 196 25/01/12 8:33 AM
  • Using List Views to Display Long Lists ❘ 197 This example demonstrated how to make items in a ListView selectable. At the end of the selection process, how do you know which item or items are selected? The following Try It Out shows you how. TRY IT OUT 1. Checking Which Items Are Selected Using the BasicViews5 project again, add the following lines in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btn” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Show selected items” android:onClick=”onClick”/> <ListView android:id=”@+id/android:list” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 2. Add the following lines in bold to the BasicViews5Activity.java fi le: package net.learn2develop.BasicViews5; import import import import import import android.app.ListActivity; android.os.Bundle; android.view.View; android.widget.ArrayAdapter; android.widget.ListView; android.widget.Toast; public class BasicViews5Activity extends ListActivity { String[] presidents; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lstView = getListView(); //lstView.setChoiceMode(ListView.CHOICE_MODE_NONE); //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lstView.setTextFilterEnabled(true); presidents = c04.indd 197 25/01/12 8:33 AM
  • 198 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS getResources().getStringArray(R.array.presidents_array); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, presidents)); } public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, “You have selected “ + presidents[position], Toast.LENGTH_SHORT).show(); } public void onClick(View view) { ListView lstView = getListView(); String itemsSelected = “Selected items: n”; for (int i=0; i<lstView.getCount(); i++) { if (lstView.isItemChecked(i)) { itemsSelected += lstView.getItemAtPosition(i) + “n”; } } Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show(); } } 3. Press F11 to debug the application on the Android emulator. Click on a few items and then click the Show selected items button (see Figure 4-21). The list of names selected will be displayed. How It Works In the previous section’s exercise, you saw how to populate a ListView that occupies the entire activity — in that example, there is no need to add a <ListView> element to the main.xml fi le. In this example, you saw how a ListView can partially fi ll up an activity. To do that, you needed to add a <ListView> element with the id attribute set to @+id/android:list: <ListView android:id=”@+id/android:list” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> You then needed to load the content of the activity using the setContentView() method (previously commented out): FIGURE 4-21 setContentView(R.layout.main); c04.indd 198 25/01/12 8:33 AM
  • ❘ 199 Using List Views to Display Long Lists To fi nd out which items in the ListView have been checked, you use the isItemChecked() method: ListView lstView = getListView(); String itemsSelected = “Selected items: n”; for (int i=0; i<lstView.getCount(); i++) { if (lstView.isItemChecked(i)) { itemsSelected += lstView.getItemAtPosition(i) + “n”; } } Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show(); The getItemAtPosition() method returns the name of the item at the specified position. NOTE So far, all the examples show how to use the ListView inside a ListActivity. This is not absolutely necessary — you can also use the ListView inside an Activity. In this case, to programmatically refer to the ListView, you use the findViewByID() method instead of the getListView() method; and the id attribute of the <ListView> element can use the format of @+id/<view_name>. Using the Spinner View The ListView displays a long list of items in an activity, but sometimes you may want your user interface to display other views, and hence you do not have the additional space for a full-screen view like the ListView. In such cases, you should use the SpinnerView. The SpinnerView displays one item at a time from a list and enables users to choose among them. The following Try It Out shows how you can use the SpinnerView in your activity. TRY IT OUT Using the SpinnerView to Display One Item at a Time codefile BasicViews6.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it BasicViews6. Modify the main.xml fi le located in the res/layout folder as shown here: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Spinner android:id=”@+id/spinner1” android:layout_width=”wrap_content” c04.indd 199 25/01/12 8:33 AM
  • 200 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS android:layout_height=”wrap_content” android:drawSelectorOnTop=”true” /> </LinearLayout> 3. Add the following lines in bold to the strings.xml fi le located in the res/values folder: <?xml version=”1.0” encoding=”utf-8”?> <resources> <string name=”hello”>Hello World, BasicViews6Activity!</string> <string name=”app_name”>BasicViews6</string> <string-array name=”presidents_array”> <item>Dwight D. Eisenhower</item> <item>John F. Kennedy</item> <item>Lyndon B. Johnson</item> <item>Richard Nixon</item> <item>Gerald Ford</item> <item>Jimmy Carter</item> <item>Ronald Reagan</item> <item>George H. W. Bush</item> <item>Bill Clinton</item> <item>George W. Bush</item> <item>Barack Obama</item> </string-array> </resources> 4. Add the following statements in bold to the BasicViews6Activity.java fi le: package net.learn2develop.BasicViews6; import import import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.widget.AdapterView; android.widget.AdapterView.OnItemSelectedListener; android.widget.ArrayAdapter; android.widget.Spinner; android.widget.Toast; public class BasicViews6Activity extends Activity { String[] presidents; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); presidents = getResources().getStringArray(R.array.presidents_array); c04.indd 200 25/01/12 8:33 AM
  • Using List Views to Display Long Lists ❘ 201 Spinner s1 = (Spinner) findViewById(R.id.spinner1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, presidents); s1.setAdapter(adapter); s1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { int index = arg0.getSelectedItemPosition(); Toast.makeText(getBaseContext(), “You have selected item : “ + presidents[index], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } } 5. Press F11 to debug the application on the Android emulator. Click on the SpinnerView and you will see a pop-up displaying the list of presidents’ names (see Figure 4-22). Clicking an item will display a message showing you the item selected. How It Works The preceding example works very much like the ListView. One additional method you need to implement is the onNothingSelected() method. This method is fi red when the user presses the back button, which dismisses the list of items displayed. In this case, nothing is selected so you do not need to do anything. Instead of displaying the items in the ArrayAdapter as a simple list, you can also display them using radio buttons. To do so, modify the second parameter in the constructor of the ArrayAdapter class: FIGURE 4-22 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, presidents); c04.indd 201 25/01/12 8:33 AM
  • 202 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS This causes the items to be displayed as a list of radio buttons (see Figure 4-23). FIGURE 4-23 UNDERSTANDING SPECIALIZED FRAGMENTS In Chapter 2, you learned about the fragment feature that is available beginning with Android 3. Using fragments, you can customize the user interface of your Android application by dynamically rearranging fragments to fit within an activity. This enables you to build applications that run on devices with different screen sizes. As you have learned, fragments are really “mini-activities” that have their own life cycles. To create a fragment, you need a class that extends the Fragment base class. Besides the Fragment base class, you can also extend from some other subclasses of the Fragment base class to create more specialized fragments. The following sections discuss the three subclasses of Fragment: ListFragment, DialogFragment, and PreferenceFragment. Using a ListFragment A list fragment is a fragment that contains a ListView, displaying a list of items from a data source such as an array or a Cursor. A list fragment is very useful, as you may often have one fragment that contains a list of items (such as a list of RSS postings), and another fragment that displays c04.indd 202 25/01/12 8:33 AM
  • Understanding Specialized Fragments ❘ 203 details about the selected posting. To create a list fragment, you need to extend the ListFragment base class. The following Try It Out shows you how to get started with a list fragment. TRY IT OUT Creating and Using a List Fragment codefile ListFragmentExample.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it ListFragmentExample. Modify the main.xml fi le as shown in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <fragment android:name=”net.learn2develop.ListFragmentExample.Fragment1” android:id=”@+id/fragment1” android:layout_weight=”0.5” android:layout_width=”0dp” android:layout_height=”200dp” /> <fragment android:name=”net.learn2develop.ListFragmentExample.Fragment1” android:id=”@+id/fragment2” android:layout_weight=”0.5” android:layout_width=”0dp” android:layout_height=”300dp” /> </LinearLayout> 3. 4. Add an XML fi le to the res/layout folder and name it fragment1.xml. Populate the fragment1.xml as follows: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <ListView android:id=”@id/android:list” android:layout_width=”match_parent” android:layout_height=”match_parent” android:layout_weight=”1” android:drawSelectorOnTop=”false”/> </LinearLayout> 5. c04.indd 203 Add a Java Class fi le to the package and name it Fragment1. 25/01/12 8:33 AM
  • 204 6. ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS Populate the Fragment1.java fi le as follows: package net.learn2develop.ListFragmentExample; import import import import import import import import android.app.ListFragment; android.os.Bundle; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; android.widget.ArrayAdapter; android.widget.ListView; android.widget.Toast; public class Fragment1 extends ListFragment { String[] presidents = { “Dwight D. Eisenhower”, “John F. Kennedy”, “Lyndon B. Johnson”, “Richard Nixon”, “Gerald Ford”, “Jimmy Carter”, “Ronald Reagan”, “George H. W. Bush”, “Bill Clinton”, “George W. Bush”, “Barack Obama” }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, presidents)); } public void onListItemClick(ListView parent, View v, int position, long id) { Toast.makeText(getActivity(), “You have selected “ + presidents[position], Toast.LENGTH_SHORT).show(); } } c04.indd 204 25/01/12 8:33 AM
  • Understanding Specialized Fragments ❘ 205 7. Press F11 to debug the application on the Android emulator. Figure 4-24 shows the two list fragments displaying the two lists of presidents’ names. 8. Click on any of the items in the two ListView views, and a message is displayed (see Figure 4-25). FIGURE 4-24 FIGURE 4-25 How It Works First, you created the XML fi le for the fragment by adding a ListView element to it: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <ListView android:id=”@id/android:list” android:layout_width=”match_parent” android:layout_height=”match_parent” android:layout_weight=”1” android:drawSelectorOnTop=”false”/> </LinearLayout> c04.indd 205 25/01/12 8:33 AM
  • 206 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS To create a list fragment, the Java class for the fragment must extend the ListFragment base class: public class Fragment1 extends ListFragment { } You then declared an array to contain the list of presidents’ names in your activity: String[] presidents = { “Dwight D. Eisenhower”, “John F. Kennedy”, “Lyndon B. Johnson”, “Richard Nixon”, “Gerald Ford”, “Jimmy Carter”, “Ronald Reagan”, “George H. W. Bush”, “Bill Clinton”, “George W. Bush”, “Barack Obama” }; In the onCreate() event, you use the setListAdapter() method to programmatically fill the ListView with the content of the array. The ArrayAdapter object manages the array of strings that will be displayed by the ListView. In this example, you set the ListView to display in the simple_list_item_1 mode: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, presidents)); } The onListItemClick() method is fi red whenever an item in the ListView is clicked: public void onListItemClick(ListView parent, View v, int position, long id) { Toast.makeText(getActivity(), “You have selected “ + presidents[position], Toast.LENGTH_SHORT).show(); } Finally, you added two fragments to the activity. Note the height of each fragment: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”horizontal” > <fragment android:name=”net.learn2develop.ListFragmentExample.Fragment1” android:id=”@+id/fragment1” android:layout_weight=”0.5” c04.indd 206 25/01/12 8:33 AM
  • Understanding Specialized Fragments ❘ 207 android:layout_width=”0dp” android:layout_height=”200dp” /> <fragment android:name=”net.learn2develop.ListFragmentExample.Fragment1” android:id=”@+id/fragment2” android:layout_weight=”0.5” android:layout_width=”0dp” android:layout_height=”300dp” /> </LinearLayout> Using a DialogFragment Another type of fragment that you can create is a dialog fragment. A dialog fragment floats on top of an activity and is displayed modally. Dialog fragments are useful for cases in which you need to obtain the user’s response before continuing with execution. To create a dialog fragment, you need to extend the DialogFragment base class. The following Try It Out shows how to create a dialog fragment. TRY IT OUT Creating and Using a Dialog Fragment codefile DialogFragmentExample.zip available for download at Wrox.com 1. 2. 3. Using Eclipse, create an Android project and name it DialogFragmentExample. Add a Java Class fi le under the package and name it Fragment1. Populate the Fragment1.java fi le as follows: package net.learn2develop.DialogFragmentExample; import import import import import android.app.AlertDialog; android.app.Dialog; android.app.DialogFragment; android.content.DialogInterface; android.os.Bundle; public class Fragment1 extends DialogFragment { static Fragment1 newInstance(String title) { Fragment1 fragment = new Fragment1(); Bundle args = new Bundle(); args.putString(“title”, title); fragment.setArguments(args); return fragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { c04.indd 207 25/01/12 8:33 AM
  • 208 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS String title = getArguments().getString(“title”); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_launcher) .setTitle(title) .setPositiveButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doPositiveClick(); } }) .setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doNegativeClick(); } }).create(); } } 4. Populate the DialogFragmentExampleActivity.java fi le as shown here in bold: package net.learn2develop.DialogFragmentExample; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class DialogFragmentExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Fragment1 dialogFragment = Fragment1.newInstance( “Are you sure you want to do this?”); dialogFragment.show(getFragmentManager(), “dialog”); } public void doPositiveClick() { //---perform steps when user clicks on OK--Log.d(“DialogFragmentExample”, “User clicks on OK”); } public void doNegativeClick() { //---perform steps when user clicks on Cancel--Log.d(“DialogFragmentExample”, “User clicks on Cancel”); } } c04.indd 208 25/01/12 8:33 AM
  • Understanding Specialized Fragments 5. ❘ 209 Press F11 to debug the application on the Android emulator. Figure 4-26 shows the fragment displayed as an alert dialog. Click either the OK button or the Cancel button and observe the message displayed. How It Works To create a dialog fragment, fi rst your Java class must extend the DialogFragment base class: public class Fragment1 extends DialogFragment { } In this example, you created an alert dialog, which is a dialog window that displays a message with optional buttons. Within the Fragment1 class, you defi ned the newInstance() method: static Fragment1 newInstance(String title) { Fragment1 fragment = new Fragment1(); Bundle args = new Bundle(); args.putString(“title”, title); fragment.setArguments(args); return fragment; } FIGURE 4-26 The newInstance() method allows a new instance of the fragment to be created, and at the same time it accepts an argument specifying the string (title) to display in the alert dialog. The title is then stored in a Bundle object for use later. Next, you defi ned the onCreateDialog() method, which is called after onCreate() and before onCreateView(): @Override public Dialog onCreateDialog(Bundle savedInstanceState) { String title = getArguments().getString(“title”); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_launcher) .setTitle(title) .setPositiveButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doPositiveClick(); } }) .setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doNegativeClick(); c04.indd 209 25/01/12 8:33 AM
  • 210 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS } }).create(); } Here, you created an alert dialog with two buttons: OK and Cancel. The string to be displayed in it is obtained from the title argument saved in the Bundle object. To display the dialog fragment, you created an instance of it and then called its show() method: Fragment1 dialogFragment = Fragment1.newInstance( “Are you sure you want to do this?”); dialogFragment.show(getFragmentManager(), “dialog”); You also needed to implement two methods, doPositiveClick() and doNegativeClick(), to handle the user clicking the OK or Cancel buttons, respectively: public void doPositiveClick() { //---perform steps when user clicks on OK--Log.d(“DialogFragmentExample”, “User clicks on OK”); } public void doNegativeClick() { //---perform steps when user clicks on Cancel--Log.d(“DialogFragmentExample”, “User clicks on Cancel”); } Using a PreferenceFragment Your Android applications will typically provide preferences that allow users to personalize the application for their own use. For example, you may allow users to save the login credentials that they use to access their web resources, or save information such as how often the feeds must be refreshed (such as in an RSS reader application), and so on. In Android, you can use the PreferenceActivity base class to display an activity for the user to edit the preferences. In Android 3.0 and later, you can use the PreferenceFragment class to do the same thing. The following Try It Out shows you how to create and use a preference fragment in Android 3 and 4. TRY IT OUT Creating and Using a Preference Fragment codefile PreferenceFragmentExample.zip available for download at Wrox.com 1. 2. c04.indd 210 Using Eclipse, create an Android project and name it PreferenceFragmentExample. Create a new xml folder under the res folder and then add a new Android XML file to it. Name the XML fi le preferences.xml (see Figure 4-27). 25/01/12 8:33 AM
  • Understanding Specialized Fragments ❘ 211 FIGURE 4-27 3. Populate the preferences.xml fi le as follows: <?xml version=”1.0” encoding=”utf-8”?> <PreferenceScreen xmlns:android=”http://schemas.android.com/apk/res/android”> <PreferenceCategory android:title=”Category 1”> <CheckBoxPreference android:title=”Checkbox” android:defaultValue=”false” android:summary=”True of False” android:key=”checkboxPref” /> </PreferenceCategory> <PreferenceCategory android:title=”Category 2”> <EditTextPreference android:name=”EditText” android:summary=”Enter a string” android:defaultValue=”[Enter a string here]” android:title=”Edit Text” android:key=”editTextPref” /> <RingtonePreference android:name=”Ringtone Preference” android:summary=”Select a ringtone” android:title=”Ringtones” android:key=”ringtonePref” /> <PreferenceScreen android:title=”Second Preference Screen” android:summary= “Click here to go to the second Preference Screen” android:key=”secondPrefScreenPref”> <EditTextPreference android:name=”EditText” c04.indd 211 25/01/12 8:33 AM
  • 212 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS android:summary=”Enter a string” android:title=”Edit Text (second Screen)” android:key=”secondEditTextPref” /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen> 4. 5. Add a Java Class fi le to the package and name it Fragment1. Populate the Fragment1.java fi le as follows: package net.learn2develop.PreferenceFragmentExample; import android.os.Bundle; import android.preference.PreferenceFragment; public class Fragment1 extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---load the preferences from an XML file--addPreferencesFromResource(R.xml.preferences); } } 6. Modify the PreferenceFragmentExampleActivity.java fi le as shown in bold: package net.learn2develop.PreferenceFragmentExample; import import import import android.app.Activity; android.app.FragmentManager; android.app.FragmentTransaction; android.os.Bundle; public class PreferenceFragmentExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment1 fragment1 = new Fragment1(); fragmentTransaction.replace(android.R.id.content, fragment1); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } } 7. c04.indd 212 Press F11 to debug the application on the Android emulator. Figure 4-28 shows the preference fragment displaying the list of preferences that the user can modify. 25/01/12 8:33 AM
  • Understanding Specialized Fragments 8. 9. When the Edit Text preference is clicked, a pop-up will be displayed (see Figure 4-29). Clicking the Second Preference Screen item will cause a second preference screen to be displayed (see Figure 4-30). FIGURE 4-28 10. 11. ❘ 213 FIGURE 4-29 FIGURE 4-30 To cause the preference fragment to go away, click the back button on the emulator. If you look at the File Explorer (available in the DDMS perspective), you will be able to locate the preferences fi le located in the /data/data/net.learn2develop.PreferenceFragmentExample/ shared_prefs/ folder (see Figure 4-31). All changes made by the user are persisted in this fi le. FIGURE 4-31 c04.indd 213 25/01/12 8:33 AM
  • 214 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS NOTE Chapter 6 describes how to retrieve the values saved in a preference file. How It Works To create a list of preferences in your Android application, you fi rst needed to create the preferences .xml fi le and populate it with the various XML elements. This XML fi le defi nes the various items that you want to persist in your application. To create the preference fragment, you needed to extend the PreferenceFragment base class: public class Fragment1 extends PreferenceFragment { } To load the preferences file in the preference fragment, you use the addPreferencesFromResource() method: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---load the preferences from an XML file--addPreferencesFromResource(R.xml.preferences); } To display the preference fragment in your activity, you can make use of the FragmentManager and the FragmentTransaction classes: FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment1 fragment1 = new Fragment1(); fragmentTransaction.replace(android.R.id.content, fragment1); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); You needed to add the preference fragment to the back stack using the addToBackStack() method so that the user can dismiss the fragment by clicking the back button. SUMMARY This chapter provided a brief look at some of the commonly used views in an Android application. While it is not possible to exhaustively examine each view in detail, the views you learned about here should provide a good foundation for designing your Android application’s user interface, regardless of its requirements. c04.indd 214 25/01/12 8:33 AM
  • Summary ❘ 215 EXERCISES 1. How do you programmatically determine whether a RadioButton is checked? 2. How do you access the string resource stored in the strings.xml file? 3. Write the code snippet to obtain the current date. 4. Name the three specialized fragments you can use in your Android application and describe their uses. Answers to the exercises can be found in Appendix C. c04.indd 215 25/01/12 8:33 AM
  • 216 ❘ CHAPTER 4 DESIGNING YOUR USER INTERFACE WITH VIEWS WHAT YOU LEARNED IN THIS CHAPTER TOPIC KEY CONCEPTS TextView <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> Button <Button android:id=”@+id/btnSave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Save” /> ImageButton <ImageButton android:id=”@+id/btnImg1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:src=”@drawable/icon” /> EditText <EditText android:id=”@+id/txtName” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> CheckBox <CheckBox android:id=”@+id/chkAutosave” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Autosave” /> RadioGroup and RadioButton <RadioGroup android:id=”@+id/rdbGp1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” > <RadioButton android:id=”@+id/rdb1” android:layout_width=”fill_parent” c04.indd 216 25/01/12 8:33 AM
  • ❘ 217 Summary TOPIC KEY CONCEPTS android:layout_height=”wrap_content” android:text=”Option 1” /> <RadioButton android:id=”@+id/rdb2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Option 2” /> </RadioGroup> ToggleButton <ToggleButton android:id=”@+id/toggle1” android:layout_width=”wrap_content” ProgressBar <ProgressBar android:id=”@+id/progressbar” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> AutoCompleteTextBox <AutoCompleteTextView android:id=”@+id/txtCountries” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> TimePicker <TimePicker android:id=”@+id/timePicker” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> DatePicker <DatePicker android:id=”@+id/datePicker” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> Spinner <Spinner android:id=”@+id/spinner1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:drawSelectorOnTop=”true” /> Specialized fragment types c04.indd 217 ListFragment, DialogFragment, and PreferenceFragment 25/01/12 8:33 AM
  • c04.indd 218 25/01/12 8:33 AM
  • 5 Displaying Pictures and Menus with Views WHAT YOU WILL LEARN IN THIS CHAPTER ➤ How to use the Gallery, ImageSwitcher, GridView, and ImageView views to display images ➤ How to display options menus and context menus ➤ How to display time using the AnalogClock and DigitalClock views ➤ How to display web content using the WebView view In the previous chapter, you learned about the various views that you can use to build the user interface of your Android application. In this chapter, you continue your exploration of the other views that you can use to create robust and compelling applications. In particular, you will learn how to work with views that enable you to display images. In addition, you will learn how to create option and context menus in your Android application. This chapter ends with a discussion of some helpful views that enable users to display the current time and web content. USING IMAGE VIEWS TO DISPLAY PICTURES So far, all the views you have seen until this point are used to display text information. For displaying images, you can use the ImageView, Gallery, ImageSwitcher, and GridView views. The following sections discuss each view in detail. c05.indd 219 25/01/12 9:02 AM
  • 220 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Gallery and ImageView Views The Gallery is a view that shows items (such as images) in a center-locked, horizontal scrolling list. Figure 5-1 shows how the Gallery view looks when it is displaying some images. The following Try It Out shows you how to use the Gallery view to display a set of images. TRY IT OUT FIGURE 5-1 Using the Gallery View codefile Gallery.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Gallery. Modify the main.xml fi le as shown in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Images of San Francisco” /> <Gallery android:id=”@+id/gallery1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <ImageView android:id=”@+id/image1” android:layout_width=”320dp” android:layout_height=”250dp” android:scaleType=”fitXY” /> </LinearLayout> 3. 4. Right-click on the res/values folder and select New ➪ File. Name the fi le attrs.xml. Populate the attrs.xml fi le as follows: <?xml version=”1.0” encoding=”utf-8”?> <resources> <declare-styleable name=”Gallery1”> <attr name=”android:galleryItemBackground” /> </declare-styleable> </resources> 5. c05.indd 220 Prepare a series of images and name them pic1.png, pic2.png, and so on for each subsequent image (see Figure 5-2). 25/01/12 9:02 AM
  • Using Image Views to Display Pictures ❘ 221 FIGURE 5-2 NOTE You can download the series of images from this book’s support website at www.wrox.com. 6. Drag and drop all the images into the res/drawable-mdpi folder (see Figure 5-3). When a dialog is displayed, check the Copy files option and click OK. FIGURE 5-3 NOTE This example assumes that this project will be tested on an AVD with medium DPI screen resolution. For a real-life project, you need to ensure that each drawable folder has a set of images (of different resolutions). c05.indd 221 25/01/12 9:02 AM
  • 222 7. ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Add the following statements in bold to the GalleryActivity.java fi le: package net.learn2develop.Gallery; import import import import import import import import import import import import android.app.Activity; android.content.Context; android.content.res.TypedArray; android.os.Bundle; android.view.View; android.view.ViewGroup; android.widget.AdapterView; android.widget.AdapterView.OnItemClickListener; android.widget.BaseAdapter; android.widget.Gallery; android.widget.ImageView; android.widget.Toast; public class GalleryActivity extends Activity { //---the images to display--Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getBaseContext(), “pic” + (position + 1) + “ selected”, Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { Context context; c05.indd 222 25/01/12 9:02 AM
  • Using Image Views to Display Pictures ❘ 223 int itemBackground; public ImageAdapter(Context c) { context = c; //---setting the style--TypedArray a = obtainStyledAttributes( R.styleable.Gallery1); itemBackground = a.getResourceId( R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle(); } //---returns the number of images--public int getCount() { return imageIDs.length; } //---returns the item--public Object getItem(int position) { return position; } //---returns the ID of an item--public long getItemId(int position) { return position; } //---returns an ImageView view--public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setImageResource(imageIDs[position]); imageView.setScaleType( ImageView.ScaleType.FIT_XY); imageView.setLayoutParams( new Gallery.LayoutParams(150, 120)); } else { imageView = (ImageView) convertView; } imageView.setBackgroundResource(itemBackground); return imageView; } } } 8. c05.indd 223 Press F11 to debug the application on the Android emulator. Figure 5-4 shows the Gallery view displaying the series of images. You can swipe the images to view the entire series. Observe that as you click on an image, the Toast class displays its name. 25/01/12 9:02 AM
  • 224 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS FIGURE 5-4 9. To display the selected image in the ImageView, add the following statements in bold to the GalleryActivity.java fi le: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getBaseContext(), “pic” + (position + 1) + “ selected”, Toast.LENGTH_SHORT).show(); //---display the images selected--ImageView imageView = (ImageView) findViewById(R.id.image1); imageView.setImageResource(imageIDs[position]); } }); } 10. c05.indd 224 Press F11 to debug the application again. This time, the image selected will be displayed in the ImageView (see Figure 5-5). 25/01/12 9:02 AM
  • Using Image Views to Display Pictures ❘ 225 How It Works You fi rst added the Gallery and ImageView views to main.xml: <Gallery android:id=”@+id/gallery1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <ImageView android:id=”@+id/image1” android:layout_width=”320dp” android:layout_height=”250dp” android:scaleType=”fitXY” /> As mentioned earlier, the Gallery view is used to display a series of images in a horizontal scrolling list. The ImageView is used to display the image selected by the user. The list of images to be displayed is stored in the imageIDs array: //---the images to display--Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 }; FIGURE 5-5 You create the ImageAdapter class (which extends the BaseAdapter class) so that it can bind to the Gallery view with a series of ImageView views. The BaseAdapter class acts as a bridge between an AdapterView and the data source that feeds data into it. Examples of AdapterViews are as follows: ➤ ListView ➤ GridView ➤ Spinner ➤ Gallery There are several subclasses of the BaseAdapter class in Android: ➤ ➤ ArrayAdapter ➤ CursorAdapter ➤ c05.indd 225 ListAdapter SpinnerAdapter 25/01/12 9:02 AM
  • 226 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS For the ImageAdapter class, you implemented the following methods in bold: public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { ... } //---returns the number of images--public int getCount() { ... } //---returns the item--public Object getItem(int position) { ... } //---returns the ID of an item--public long getItemId(int position) { ... } //---returns an ImageView view--public View getView(int position, View convertView, ViewGroup parent) { ... } } In particular, the getView() method returns a View at the specified position. In this case, you returned an ImageView object. When an image in the Gallery view is selected (i.e., clicked), the selected image’s position (0 for the fi rst image, 1 for the second image, and so on) is displayed and the image is displayed in the ImageView: Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getBaseContext(), “pic” + (position + 1) + “ selected”, Toast.LENGTH_SHORT).show(); //---display the images selected--ImageView imageView = (ImageView) findViewById(R.id.image1); imageView.setImageResource(imageIDs[position]); } }); ImageSwitcher The previous section demonstrated how to use the Gallery view together with an ImageView to display a series of thumbnail images so that when one is selected, it is displayed in the ImageView. However, sometimes you don’t want an image to appear abruptly when the user selects it in the Gallery view — you might, for example, want to apply some animation to the image when it c05.indd 226 25/01/12 9:02 AM
  • ❘ 227 Using Image Views to Display Pictures transitions from one image to another. In this case, you need to use the ImageSwitcher together with the Gallery view. The following Try It Out shows you how. TRY IT OUT Using the ImageSwitcher View codefile ImageSwitcher.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it ImageSwitcher. Modify the main.xml fi le by adding the following statements in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Images of San Francisco” /> <Gallery android:id=”@+id/gallery1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <ImageSwitcher android:id=”@+id/switcher1” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:layout_alignParentLeft=”true” android:layout_alignParentRight=”true” android:layout_alignParentBottom=”true” /> </LinearLayout> 3. 4. Right-click on the res/values folder and select New ➪ File. Name the fi le attrs.xml. Populate the attrs.xml fi le as follows: <?xml version=”1.0” encoding=”utf-8”?> <resources> <declare-styleable name=”Gallery1”> <attr name=”android:galleryItemBackground” /> </declare-styleable> </resources> 5. c05.indd 227 Drag and drop a series of images into the res/drawable-mdpi folder (refer to the previous example for the images). When a dialog is displayed, check the Copy files option and click OK. 25/01/12 9:02 AM
  • 228 6. ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Add the following bold statements to the ImageSwitcherActivity.java fi le: package net.learn2develop.ImageSwitcher; import import import import import import import import import import import import import import import android.app.Activity; android.content.Context; android.content.res.TypedArray; android.os.Bundle; android.view.View; android.view.ViewGroup; android.view.ViewGroup.LayoutParams; android.view.animation.AnimationUtils; android.widget.AdapterView; android.widget.AdapterView.OnItemClickListener; android.widget.BaseAdapter; android.widget.Gallery; android.widget.ImageSwitcher; android.widget.ImageView; android.widget.ViewSwitcher.ViewFactory; public class ImageSwitcherActivity extends Activity implements ViewFactory { //---the images to display--Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 }; private ImageSwitcher imageSwitcher; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { imageSwitcher.setImageResource(imageIDs[position]); c05.indd 228 25/01/12 9:02 AM
  • Using Image Views to Display Pictures ❘ 229 } }); } public View makeView() { ImageView imageView = new ImageView(this); imageView.setBackgroundColor(0xFF000000); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView; } public class ImageAdapter extends BaseAdapter { private Context context; private int itemBackground; public ImageAdapter(Context c) { context = c; //---setting the style--TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); itemBackground = a.getResourceId( R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle(); } //---returns the number of images--public int getCount() { return imageIDs.length; } //---returns the item--public Object getItem(int position) { return position; } //---returns the ID of an item--public long getItemId(int position) { return position; } //---returns an ImageView view--public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(imageIDs[position]); c05.indd 229 25/01/12 9:02 AM
  • 230 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); imageView.setBackgroundResource(itemBackground); return imageView; } } } 7. Press F11 to debug the application on the Android emulator. Figure 5-6 shows the Gallery and ImageSwitcher views, with both the collection of images as well as the image selected. How It Works The fi rst thing to note in this example is that the ImageSwitcherActivity not only extends Activity, but also implements ViewFactory. To use the ImageSwitcher view, you need to implement the ViewFactory interface, which creates the views for use with the ImageSwitcher view. For this, you need to implement the makeView() method: public View makeView() FIGURE 5-6 { ImageView imageView = new ImageView(this); imageView.setBackgroundColor(0xFF000000); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView; } This method creates a new View to be added in the ImageSwitcher view, which in this case is an ImageView. Like the Gallery example in the previous section, you also implemented an ImageAdapter class so that it can bind to the Gallery view with a series of ImageView views. In the onCreate() method, you get a reference to the ImageSwitcher view and set the animation, specifying how images should “fade” in and out of the view. Finally, when an image is selected from the Gallery view, the image is displayed in the ImageSwitcher view: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, c05.indd 230 25/01/12 9:02 AM
  • Using Image Views to Display Pictures ❘ 231 android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { imageSwitcher.setImageResource(imageIDs[position]); } }); } In this example, when an image is selected in the Gallery view, it appears by “fading” in. When the next image is selected, the current image fades out. If you want the image to slide in from the left and slide out to the right when another image is selected, try the following animation: imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)); GridView The GridView shows items in a two-dimensional scrolling grid. You can use the GridView together with an ImageView to display a series of images. The following Try It Out demonstrates how. TRY IT OUT Using the GridView View codefile Grid.zip available for download at Wrox.com 1. 2. 3. Using Eclipse, create a new Android project and name it Grid. Drag and drop a series of images into the res/drawable-mdpi folder (see the previous example for the images). When a dialog is displayed, check the Copy fi les option and click OK. Populate the main.xml fi le with the following content: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <GridView android:id=”@+id/gridview” android:layout_width=”fill_parent” android:layout_height=”fill_parent” c05.indd 231 25/01/12 9:02 AM
  • 232 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS android:numColumns=”auto_fit” android:verticalSpacing=”10dp” android:horizontalSpacing=”10dp” android:columnWidth=”90dp” android:stretchMode=”columnWidth” android:gravity=”center” /> </LinearLayout> 4. Add the following statements in bold to the GridActivity.java fi le: package net.learn2develop.Grid; import import import import import import import import import import import android.app.Activity; android.content.Context; android.os.Bundle; android.view.View; android.view.ViewGroup; android.widget.AdapterView; android.widget.AdapterView.OnItemClickListener; android.widget.BaseAdapter; android.widget.GridView; android.widget.ImageView; android.widget.Toast; public class GridActivity extends Activity { //---the images to display--Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridView = (GridView) findViewById(R.id.gridview); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getBaseContext(), “pic” + (position + 1) + “ selected”, Toast.LENGTH_SHORT).show(); } c05.indd 232 25/01/12 9:02 AM
  • Using Image Views to Display Pictures ❘ 233 }); } public class ImageAdapter extends BaseAdapter { private Context context; public ImageAdapter(Context c) { context = c; } //---returns the number of images--public int getCount() { return imageIDs.length; } //---returns the item--public Object getItem(int position) { return position; } //---returns the ID of an item--public long getItemId(int position) { return position; } //---returns an ImageView view--public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType( ImageView.ScaleType.CENTER_CROP); imageView.setPadding(5, 5, 5, 5); } else { imageView = (ImageView) convertView; } imageView.setImageResource(imageIDs[position]); return imageView; } } } 5. c05.indd 233 Press F11 to debug the application on the Android emulator. Figure 5-7 shows the GridView displaying all the images. FIGURE 5-7 25/01/12 9:02 AM
  • 234 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS How It Works Like the Gallery and ImageSwitcher example, you implemented the ImageAdapter class and then bound it to the GridView: GridView gridView = (GridView) findViewById(R.id.gridview); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getBaseContext(), “pic” + (position + 1) + “ selected”, Toast.LENGTH_SHORT).show(); } }); When an image is selected, you display a Toast message indicating the selected image. Within the getView() method you can specify the size of the images and how images are spaced in the GridView by setting the padding for each image: //---returns an ImageView view--public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType( ImageView.ScaleType.CENTER_CROP); imageView.setPadding(5, 5, 5, 5); } else { imageView = (ImageView) convertView; } imageView.setImageResource(imageIDs[position]); return imageView; } } USING MENUS WITH VIEWS Menus are useful for displaying additional options that are not directly visible on the main UI of an application. There are two main types of menus in Android: ➤ c05.indd 234 Options menu — Displays information related to the current activity. In Android, you activate the options menu by pressing the MENU button. 25/01/12 9:02 AM
  • Using Menus with Views ➤ ❘ 235 Context menu — Displays information related to a particular view on an activity. In Android, to activate a context menu you tap and hold on to it. Figure 5-8 shows an example of an options menu in the Browser application. The options menu is displayed whenever the user presses the MENU button. The menu items displayed vary according to the current activity that is running. Figure 5-9 shows a context menu that is displayed when the user taps and holds on an image displayed on the page. The menu items displayed vary according to the component or view currently selected. In general, to activate the context menu, the user selects an item on the screen and taps and holds it. FIGURE 5-8 FIGURE 5-9 Creating the Helper Methods Before you go ahead and create your options and context menus, you need to create two helper methods. One creates a list of items to show inside a menu, while the other handles the event that is fi red when the user selects an item inside the menu. TRY IT OUT Creating the Menu Helper Methods codefile Menus.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Menus. In the MenusActivity.java fi le, add the following statements in bold: package net.learn2develop.Menus; import android.app.Activity; c05.indd 235 25/01/12 9:02 AM
  • 236 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS import import import import android.os.Bundle; android.view.Menu; android.view.MenuItem; android.widget.Toast; public class MenusActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } private void CreateMenu(Menu menu) { MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”); { mnu1.setAlphabeticShortcut(‘a’); mnu1.setIcon(R.drawable.ic_launcher); } MenuItem mnu2 = menu.add(0, 1, 1, “Item 2”); { mnu2.setAlphabeticShortcut(‘b’); mnu2.setIcon(R.drawable.ic_launcher); } MenuItem mnu3 = menu.add(0, 2, 2, “Item 3”); { mnu3.setAlphabeticShortcut(‘c’); mnu3.setIcon(R.drawable.ic_launcher); } MenuItem mnu4 = menu.add(0, 3, 3, “Item 4”); { mnu4.setAlphabeticShortcut(‘d’); } menu.add(0, 4, 4, “Item 5”); menu.add(0, 5, 5, “Item 6”); menu.add(0, 6, 6, “Item 7”); } private boolean MenuChoice(MenuItem item) { switch (item.getItemId()) { case 0: Toast.makeText(this, “You clicked on Item 1”, Toast.LENGTH_LONG).show(); return true; case 1: Toast.makeText(this, “You clicked on Item 2”, Toast.LENGTH_LONG).show(); return true; case 2: Toast.makeText(this, “You clicked on Item 3”, Toast.LENGTH_LONG).show(); return true; case 3: c05.indd 236 25/01/12 9:02 AM
  • Using Menus with Views Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 4: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 5: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; case 6: Toast.makeText(this, “You clicked Toast.LENGTH_LONG).show(); return true; } return false; ❘ 237 on Item 4”, on Item 5”, on Item 6”, on Item 7”, } } How It Works The preceding example creates two methods: CreateMenu() and MenuChoice(). The CreateMenu() method takes a Menu argument and adds a series of menu items to it. To add a menu item to the menu, you create an instance of the MenuItem class and use the add() method of the Menu object: MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”); { mnu1.setAlphabeticShortcut(‘a’); mnu1.setIcon(R.drawable.ic_launcher); } The four arguments of the add() method are as follows: ➤ groupId — The group identifier that the menu item should be part of. Use 0 if an item is not in a group. ➤ itemId — A unique item ID ➤ order — The order in which the item should be displayed ➤ title — The text to display for the menu item You can use the setAlphabeticShortcut() method to assign a shortcut key to the menu item so that users can select an item by pressing a key on the keyboard. The setIcon() method sets an image to be displayed on the menu item. The MenuChoice() method takes a MenuItem argument and checks its ID to determine the menu item that is selected. It then displays a Toast message to let the user know which menu item was selected. c05.indd 237 25/01/12 9:02 AM
  • 238 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Options Menu You are now ready to modify the application to display the options menu when the user presses the MENU key on the Android device. TRY IT OUT 1. Displaying an Options Menu Using the same project created in the previous section, add the following statements in bold to the MenusActivity.java fi le: package net.learn2develop.Menus; import import import import import android.app.Activity; android.os.Bundle; android.view.Menu; android.view.MenuItem; android.widget.Toast; public class MenusActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); CreateMenu(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return MenuChoice(item); } private void CreateMenu(Menu menu) { //... } private boolean MenuChoice(MenuItem item) { //... } } 2. c05.indd 238 Press F11 to debug the application on the Android emulator. Figure 5-10 shows the options menu that pops up when you click the MENU button. To select a menu item, either click on an 25/01/12 9:02 AM
  • Using Menus with Views ❘ 239 individual item or use its shortcut key (A to D; applicable only to the fi rst four items). Note that menu items 1 to 3 did not display the icons even though the code explicitly did so. 3. If you now change the minimum SDK attribute of the AndroidManifest.xml fi le to a value of 10 or less and then rerun the application on the emulator, the icons will be displayed as shown in Figure 5-11. Note that any menu items after the fi fth item are encapsulated in the item named More. Clicking on More will reveal the rest of the menu items. <uses-sdk android:minSdkVersion=”10” /> FIGURE 5-10 FIGURE 5-11 How It Works To display the options menu for your activity, you need to implement two methods in your activity: onCreateOptionsMenu() and onOptionsItemSelected(). The onCreateOptionsMenu() method is called when the MENU button is pressed. In this case, you call the CreateMenu() helper method to display the options menu. When a menu item is selected, the onOptionsItemSelected() method is called. In this case, you call the MenuChoice() method to display the menu item selected (and perform whatever action is appropriate). Take note of the look and feel of the options menu in different versions of Android. Starting with Honeycomb, the options menu items do not have icons and display all menu items in a scrollable list. For versions of Android before Honeycomb, no more than five menu items are displayed; any additional menu items are part of a “More” menu item that represents the rest of the menu items. c05.indd 239 25/01/12 9:02 AM
  • 240 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Context Menu The previous section showed how the options menu is displayed when the user presses the MENU button. Besides the options menu, you can also display a context menu. A context menu is usually associated with a view on an activity, and it is displayed when the user taps and holds an item. For example, if the user taps on a Button view and holds it for a few seconds, a context menu can be displayed. If you want to associate a context menu with a view on an activity, you need to call the setOnCreateContextMenuListener() method of that particular view. The following Try It Out shows how you can associate a context menu with a Button view. TRY IT OUT Displaying a Context Menu codefile Menus.zip available for download at Wrox.com 1. Using the same project from the previous example, add the following statements to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”@string/hello” /> <Button android:id=”@+id/button1” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”Click and hold on it” /> </LinearLayout> 2. Add the following statements in bold to the MenusActivity.java fi le: package net.learn2develop.Menus; import import import import import import import import c05.indd 240 android.app.Activity; android.os.Bundle; android.view.ContextMenu; android.view.ContextMenu.ContextMenuInfo; android.view.Menu; android.view.MenuItem; android.view.View; android.widget.Button; 25/01/12 9:02 AM
  • Using Menus with Views ❘ 241 import android.widget.Toast; public class MenusActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.button1); btn.setOnCreateContextMenuListener(this); } @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); CreateMenu(menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { //... } @Override public boolean onOptionsItemSelected(MenuItem item) { return MenuChoice(item); } private void CreateMenu(Menu menu) { //... } private boolean MenuChoice(MenuItem item) { //... } } NOTE If you changed the minimum SDK attribute of the AndroidManifest.xml file to a value of 10 earlier, be sure to change it back to 14 before you debug your application in the next step. c05.indd 241 25/01/12 9:02 AM
  • 242 3. ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Press F11 to debug the application on the Android emulator. Figure 5-12 shows the context menu that is displayed when you click and hold the Button view. How It Works In the preceding example, you call the setOnCreateContextMenuListener() method of the Button view to associate it with a context menu. When the user taps and holds the Button view, the onCreateContextMenu() method is called. In this method, you call the CreateMenu() method to display the context menu. Similarly, when an item inside the context menu is selected, the onContextItemSelected() method is called, where you call the MenuChoice() method to display a message to the user. Notice that the shortcut keys for the menu items do not work. To enable the shortcuts keys, you need to call the setQuertyMode() method of the Menu object, like this: FIGURE 5-12 private void CreateMenu(Menu menu) { menu.setQwertyMode(true); MenuItem mnu1 = menu.add(0, 0, 0, “Item 1”); { mnu1.setAlphabeticShortcut(‘a’); mnu1.setIcon(R.drawable.ic_launcher); } //... } SOME ADDITIONAL VIEWS Besides the standard views that you have seen up to this point, the Android SDK provides some additional views that make your applications much more interesting. In this section, you will learn more about the following views: AnalogClock, DigitalClock, and WebView. AnalogClock and DigitalClock Views The AnalogClock view displays an analog clock with two hands — one for minutes and one for hours. Its counterpart, the DigitalClock view, displays the time digitally. Both views display the system time only, and do not allow you to display a particular time (such as the current time in another time zone). Hence, if you want to display the time for a particular region, you have to build your own custom views. c05.indd 242 25/01/12 9:02 AM
  • Some Additional Views ❘ 243 NOTE Creating your own custom views in Android is beyond the scope of this book. However, if you are interested in this area, take a look at Google’s Android documentation on this topic at http://developer.android.com/guide/ topics/ui/custom-components.html. Using the AnalogClock and DigitalClock views are straightforward; simply declare them in your XML fi le (such as main.xml), like this: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <AnalogClock android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <DigitalClock android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> Figure 5-13 shows the AnalogClock and DigitalClock views in action. WebView The WebView enables you to embed a web browser in your activity. This is very useful if your application needs to embed some web content, such as maps from some other providers, and so on. The following Try It Out shows how you can programmatically load the content of a web page and display it in your activity. TRY IT OUT FIGURE 5-13 Using the WebView View codefile WebView.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it WebView. Add the following statements to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” c05.indd 243 25/01/12 9:02 AM
  • 244 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS android:layout_height=”fill_parent” android:orientation=”vertical” > <WebView android:id=”@+id/webview1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 3. In the WebViewActivity.java fi le, add the following statements in bold: package net.learn2develop.WebView; import import import import android.app.Activity; android.os.Bundle; android.webkit.WebSettings; android.webkit.WebView; public class WebViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView wv = (WebView) findViewById(R.id.webview1); WebSettings webSettings = wv.getSettings(); webSettings.setBuiltInZoomControls(true); wv.loadUrl( “http://chart.apis.google.com/chart” + “?chs=300x225” + “&cht=v” + “&chco=FF6342,ADDE63,63C6DE” + “&chd=t:100,80,60,30,30,30,10” + “&chdl=A|B|C”); } } 4. In the AndroidManifest.xml fi le, add the following permission: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.WebView” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <uses-permission android:name=”android.permission.INTERNET”/> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity c05.indd 244 25/01/12 9:02 AM
  • Some Additional Views ❘ 245 android:label=”@string/app_name” android:name=”.WebViewActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> 5. Press F11 to debug the application on the Android emulator. Figure 5-14 shows the content of the WebView. How It Works To use the WebView to load a web page, you use the loadUrl() method and pass it a URL, like this: wv.loadUrl( “http://chart.apis.google.com/chart” + “?chs=300x225” + “&cht=v” + “&chco=FF6342,ADDE63,63C6DE” + “&chd=t:100,80,60,30,30,30,10” + “&chdl=A|B|C”); To display the built-in zoom controls, you need to first get the WebSettings property from the WebView and then call its setBuiltInZoomControls() method: FIGURE 5-14 WebSettings webSettings = wv.getSettings(); webSettings.setBuiltInZoomControls(true); Figure 5-15 shows the built-in zoom controls that appear when you use the mouse to click and drag the content of the WebView on the Android emulator. NOTE While most Android devices support multi-touch screens, the built-in zoom controls are useful for zooming your web content when testing your application on the Android emulator. Sometimes when you load a page that redirects you (for example, loading www.wrox.com redirects you to www.wrox.com/wileyCDA), WebView will cause your application to launch the device’s Browser application to load the desired page. In Figure 5-16, note the URL bar at the top of the screen. c05.indd 245 25/01/12 9:02 AM
  • 246 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS FIGURE 5-15 FIGURE 5-16 To prevent this from happening, you need to implement the WebViewClient class and override the shouldOverrideUrlLoading() method, as shown in the following example: package net.learn2develop.WebView; import import import import import android.app.Activity; android.os.Bundle; android.webkit.WebSettings; android.webkit.WebView; android.webkit.WebViewClient; public class WebViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView wv = (WebView) findViewById(R.id.webview1); WebSettings webSettings = wv.getSettings(); webSettings.setBuiltInZoomControls(true); wv.setWebViewClient(new Callback()); wv.loadUrl(“http://www.wrox.com”); c05.indd 246 25/01/12 9:02 AM
  • Some Additional Views ❘ 247 } private class Callback extends WebViewClient { @Override public boolean shouldOverrideUrlLoading( WebView view, String url) { return(false); } } } Figure 5-17 shows the Wrox.com home page now loading correctly in the WebView. FIGURE 5-17 You can also dynamically formulate an HTML string and load it into the WebView, using the loadDataWithBaseURL() method: WebView wv = (WebView) findViewById(R.id.webview1); final String mimeType = “text/html”; final String encoding = “UTF-8”; String html = “<H1>A simple HTML page</H1><body>” + “<p>The quick brown fox jumps over the lazy dog</p>” + “</body>”; wv.loadDataWithBaseURL(“”, html, mimeType, encoding, “”); c05.indd 247 25/01/12 9:02 AM
  • 248 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS Figure 5-18 shows the content displayed by the WebView. FIGURE 5-18 Alternatively, if you have an HTML fi le located in the assets folder of the project (see Figure 5-19), you can load it into the WebView using the loadUrl() method: WebView wv = (WebView) findViewById(R.id.webview1); wv.loadUrl(“file:///android_asset/Index.html”); FIGURE 5-19 Figure 5-20 shows the content of the WebView. c05.indd 248 25/01/12 9:02 AM
  • Summary ❘ 249 FIGURE 5-20 SUMMARY In this chapter, you have taken a look at the various views that enable you to display images: Gallery, ImageView, ImageSwitcher, and GridView. In addition, you learned about the difference between options menus and context menus, and how to display them in your application. Finally, you learned about the AnalogClock and DigitalClock views, which display the current time graphically, as well as the WebView, which displays the content of a web page. EXERCISES 1. What is the purpose of the ImageSwitcher? 2. Name the two methods you need to override when implementing an options menu in your activity. 3. Name the two methods you need to override when implementing a context menu in your activity. 4. How do you prevent the WebView from invoking the device’s web browser when a redirection occurs in the WebView? Answers to the exercises can be found in Appendix C. c05.indd 249 25/01/12 9:02 AM
  • 250 ❘ CHAPTER 5 DISPLAYING PICTURES AND MENUS WITH VIEWS WHAT YOU LEARNED IN THIS CHAPTER TOPIC Using the Gallery view Displays a series of images in a horizontal scrolling list Gallery <Gallery android:id=”@+id/gallery1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> ImageView <ImageView android:id=”@+id/image1” android:layout_width=”320px” android:layout_height=”250px” android:scaleType=”fitXY” /> Using the ImageSwitcher view Performs animation when switching between images ImageSwitcher <ImageSwitcher android:id=”@+id/switcher1” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:layout_alignParentLeft=”true” android:layout_alignParentRight=”true” android:layout_alignParentBottom=”true” /> Using the GridView Shows items in a two-dimensional scrolling grid GridView <GridView android:id=”@+id/gridview” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:numColumns=”auto_fit” android:verticalSpacing=”10dp” android:horizontalSpacing=”10dp” android:columnWidth=”90dp” android:stretchMode=”columnWidth” android:gravity=”center” /> AnalogClock <AnalogClock android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> DigitalClock <DigitalClock android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> WebView c05.indd 250 KEY CONCEPTS <WebView android:id=”@+id/webview1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> 25/01/12 9:02 AM
  • 6 Data Persistence WHAT YOU WILL LEARN IN THIS CHAPTER ➤ How to save simple data using the SharedPreferences object ➤ Enabling users to modify preferences using a PreferenceActivity class ➤ How to write and read files in internal and external storage ➤ Creating and using a SQLite database In this chapter, you will learn how to persist data in your Android applications. Persisting data is an important topic in application development, as users typically expect to reuse data in the future. For Android, there are primarily three basic ways of persisting data: ➤ A lightweight mechanism known as shared preferences to save small chunks of data ➤ Traditional file systems ➤ A relational database management system through the support of SQLite databases The techniques discussed in this chapter enable applications to create and access their own private data. In the next chapter you’ll learn how you can share data across applications. SAVING AND LOADING USER PREFERENCES Android provides the SharedPreferences object to help you save simple application data. For example, your application may have an option that enables users to specify the font size of the text displayed in your application. In this case, your application needs to remember the size set by the user so that the next time he or she uses the application again, it can set the size appropriately. In order to do so, you have several options. You can save the data to a fi le, but you have to perform some fi le management routines, such as writing the data to c06.indd 251 25/01/12 9:03 AM
  • 252 ❘ CHAPTER 6 DATA PERSISTENCE the fi le, indicating how many characters to read from it, and so on. Also, if you have several pieces of information to save, such as text size, font name, preferred background color, and so on, then the task of writing to a fi le becomes more onerous. An alternative to writing to a text file is to use a database, but saving simple data to a database is overkill, both from a developer’s point of view and in terms of the application’s run-time performance. Using the SharedPreferences object, however, you save the data you want through the use of name/value pairs — specify a name for the data you want to save, and then both it and its value will be saved automatically to an XML file for you. Accessing Preferences Using an Activity In the following Try It Out, you learn how to use the SharedPreferences object to store application data. You will also learn how the stored application data can be modified directly by the user through a special type of activity provided by the Android OS. TRY IT OUT Saving Data Using the SharedPreferences Object codefile SharedPreferences.zip available for download at Wrox.com 1. 2. 3. Using Eclipse, create an Android project and name it UsingPreferences. Create a new subfolder in the res folder and name it xml. In this newly created folder, add a file and name it myapppreferences.xml (see Figure 6-1). Populate the myapppreferences.xml fi le as follows: <?xml version=”1.0” encoding=”utf-8”?> <PreferenceScreen xmlns:android=”http://schemas.android.com/apk/res/android”> <PreferenceCategory android:title=”Category 1”> <CheckBoxPreference android:title=”Checkbox” android:defaultValue=”false” android:summary=”True or False” android:key=”checkboxPref” /> </PreferenceCategory> <PreferenceCategory android:title=”Category 2”> <EditTextPreference android:summary=”Enter a string” android:defaultValue=”[Enter a string here]” android:title=”Edit Text” android:key=”editTextPref” /> <RingtonePreference android:summary=”Select a ringtone” android:title=”Ringtones” android:key=”ringtonePref” /> FIGURE 6-1 <PreferenceScreen android:title=”Second Preference Screen” c06.indd 252 25/01/12 9:03 AM
  • Saving and Loading User Preferences ❘ 253 android:summary= “Click here to go to the second Preference Screen” android:key=”secondPrefScreenPref” > <EditTextPreference android:summary=”Enter a string” android:title=”Edit Text (second Screen)” android:key=”secondEditTextPref” /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen> 4. 5. Under the package name, add a new Class fi le and name it AppPreferenceActivity. Populate the AppPreferenceActivity.java fi le as follows: package net.learn2develop.UsingPreferences; import android.os.Bundle; import android.preference.PreferenceActivity; public class AppPreferenceActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---load the preferences from an XML file--addPreferencesFromResource(R.xml.myapppreferences); } } 6. In the AndroidManifest.xml fi le, add the new entry for the AppPreferenceActivity class: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.UsingPreferences” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.UsingPreferencesActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <activity android:name=”.AppPreferenceActivity” android:label=”@string/app_name”> <intent-filter> c06.indd 253 25/01/12 9:03 AM
  • 254 ❘ CHAPTER 6 DATA PERSISTENCE <action android:name=”net.learn2develop.AppPreferenceActivity” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </activity> </application> </manifest> 7. In the main.xml fi le, add the following code in bold (replacing the existing TextView): <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btnPreferences” android:text=”Load Preferences Screen” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickLoad”/> <Button android:id=”@+id/btnDisplayValues” android:text=”Display Preferences Values” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickDisplay”/> <EditText android:id=”@+id/txtString” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <Button android:id=”@+id/btnModifyValues” android:text=”Modify Preferences Values” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickModify”/> </LinearLayout> 8. Add the following lines in bold to the UsingPreferencesActivity.java fi le: package net.learn2develop.UsingPreferences; import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; public class UsingPreferencesActivity extends Activity { c06.indd 254 25/01/12 9:03 AM
  • Saving and Loading User Preferences ❘ 255 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClickLoad(View view) { Intent i = new Intent(“net.learn2develop.AppPreferenceActivity”); startActivity(i); } } 9. Press F11 to debug the application on the Android emulator. Click the Load Preferences Screen button to see the preferences screen, as shown in Figure 6-2. FIGURE 6-2 10. c06.indd 255 Clicking the Checkbox item toggles the checkbox’s value between checked and unchecked. Note the two categories: Category 1 and Category 2. Click the Edit Text item and enter some values as shown in Figure 6-3. Click OK to dismiss the dialog. 25/01/12 9:03 AM
  • 256 ❘ CHAPTER 6 DATA PERSISTENCE FIGURE 6-3 11. Click the Ringtones item to select either the default ringtone or silent mode (see Figure 6-4). If you test the application on a real Android device, you can select from a more comprehensive list of ringtones. 12. Clicking the Second Preference Screen item will navigate to the next screen (see Figure 6-5). FIGURE 6-4 c06.indd 256 FIGURE 6-5 25/01/12 9:03 AM
  • Saving and Loading User Preferences ❘ 257 13. To go back to the previous screen, click the Back button. To dismiss the preferences screen, you also click the Back button. 14. Once you have modified the value of at least one of the preferences, a fi le is created in the /data/ data/net.learn2develop.UsingPreferences/shared_prefs folder of the Android emulator. To verify this, switch to the DDMS perspective in Eclipse and look at the File Explorer tab (see Figure 6-6); you will see an XML fi le named net.learn2develop.UsingPreferences_ preferences.xml. FIGURE 6-6 15. If you extract this fi le and examine its content, you will see something like the following: <?xml version=’1.0’ encoding=’utf-8’ standalone=’yes’ ?> <map> <string name=”editTextPref”>[Enter a string here]</string> <string name=”ringtonePref”></string> </map> How It Works You fi rst created an XML fi le named myapppreferences.xml to store the types of preferences you want to save for your application: <?xml version=”1.0” encoding=”utf-8”?> <PreferenceScreen xmlns:android=”http://schemas.android.com/apk/res/android”> <PreferenceCategory android:title=”Category 1”> c06.indd 257 25/01/12 9:03 AM
  • 258 ❘ CHAPTER 6 DATA PERSISTENCE <CheckBoxPreference android:title=”Checkbox” android:defaultValue=”false” android:summary=”True or False” android:key=”checkboxPref” /> </PreferenceCategory> <PreferenceCategory android:title=”Category 2”> <EditTextPreference android:summary=”Enter a string” android:defaultValue=”[Enter a string here]” android:title=”Edit Text” android:key=”editTextPref” /> <RingtonePreference android:summary=”Select a ringtone” android:title=”Ringtones” android:key=”ringtonePref” /> <PreferenceScreen android:title=”Second Preference Screen” android:summary= “Click here to go to the second Preference Screen” android:key=”secondPrefScreenPref” > <EditTextPreference android:summary=”Enter a string” android:title=”Edit Text (second Screen)” android:key=”secondEditTextPref” /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen> In the preceding snippet, you created the following: ➤ Two preference categories for grouping different types of preferences ➤ Two checkbox preferences with keys named checkboxPref and secondEditTextPref ➤ A ringtone preference with a key named ringtonePref ➤ A preference screen to contain additional preferences The android:key attribute specifies the key that you can programmatically reference in your code to set or retrieve the value of that particular preference. To get the OS to display all these preferences for users to edit, you create an activity that extends the PreferenceActivity base class, and then call the addPreferencesFromResource() method to load the XML fi le containing the preferences: public class AppPreferenceActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //---load the preferences from an XML file--addPreferencesFromResource(R.xml.myapppreferences); } } c06.indd 258 25/01/12 9:03 AM
  • ❘ 259 Saving and Loading User Preferences The PreferenceActivity class is a specialized type of activity that displays a hierarchy of preferences to the user. To display the activity for the preferences, you invoke it using an Intent object: Intent i = new Intent(“net.learn2develop.AppPreferenceActivity”); startActivity(i); All the changes made to the preferences are automatically persisted to an XML fi le in the shared_ prefs folder of the application. Programmatically Retrieving and Modifying the Preferences Values In the previous section, you saw how the PreferenceActivity class both enables developers to easily create preferences and enables users to modify them during runtime. To make use of these preferences in your application, you use the SharedPreferences class. The following Try It Out shows you how. TRY IT OUT 1. Retrieving and Modifying Preferences Using the same project created in the previous section, add the following lines in bold to the UsingPreferencesActivity.java fi le: package net.learn2develop.UsingPreferences; import import import import import import import android.app.Activity; android.content.Intent; android.content.SharedPreferences; android.os.Bundle; android.view.View; android.widget.EditText; android.widget.Toast; public class UsingPreferencesActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClickLoad(View view) { Intent i = new Intent(“net.learn2develop.AppPreferenceActivity”); startActivity(i); } public void onClickDisplay(View view) { c06.indd 259 25/01/12 9:03 AM
  • 260 ❘ CHAPTER 6 DATA PERSISTENCE SharedPreferences appPrefs = getSharedPreferences(“net.learn2develop.UsingPreferences_preferences”, MODE_PRIVATE); DisplayText(appPrefs.getString(“editTextPref”, “”)); } public void onClickModify(View view) { SharedPreferences appPrefs = getSharedPreferences(“net.learn2develop.UsingPreferences_preferences”, MODE_PRIVATE); SharedPreferences.Editor prefsEditor = appPrefs.edit(); prefsEditor.putString(“editTextPref”, ((EditText) findViewById(R.id.txtString)).getText().toString()); prefsEditor.commit(); } private void DisplayText(String str) { Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show(); } } 2. Press F11 to rerun the application on the Android emulator again. This time, clicking the Display Preferences Values button will display the value shown in Figure 6-7. 3. Enter a string in the EditText view and click the Modify Preferences Values button (see Figure 6-8). FIGURE 6-7 c06.indd 260 FIGURE 6-8 25/01/12 9:03 AM
  • Saving and Loading User Preferences 4. ❘ 261 Now click the Display Preferences Values button again. Note that the new value is saved. How It Works In the onClickDisplay() method, you fi rst used the getSharedPreferences() method to obtain an instance of the SharedPreferences class. You do so by specifying the name of the XML fi le (in this case it is “net.learn2develop.UsingPreferences_preferences,” using the format: <PackageName>_preferences). To retrieve a string preference, you used the getString() method, passing it the key to the preference that you want to retrieve: public void onClickDisplay(View view) { SharedPreferences appPrefs = getSharedPreferences(“net.learn2develop.UsingPreferences_preferences”, MODE_PRIVATE); DisplayText(appPrefs.getString(“editTextPref”, “”)); } The MODE_PRIVATE constant indicates that the preference fi le can only be opened by the application that created it. In the onClickModify() method, you created a SharedPreferences.Editor object through the edit() method of the SharedPreferences object. To change the value of a string preference, use the putString() method. To save the changes to the preferences fi le, use the commit() method: public void onClickModify(View view) { SharedPreferences appPrefs = getSharedPreferences(“net.learn2develop.UsingPreferences_preferences”, MODE_PRIVATE); SharedPreferences.Editor prefsEditor = appPrefs.edit(); prefsEditor.putString(“editTextPref”, ((EditText) findViewById(R.id.txtString)).getText().toString()); prefsEditor.commit(); } Changing the Default Name of the Preferences File Notice that by default the name of the preferences fi le saved on the device is net.learn2develop .UsingPreferences_preferences.xml, with the package name used as the prefi x. However, sometimes it is useful to give the preferences fi le a specific name. In this case, you can do the following. Add the following code in bold to the AppPreferenceActivity.java fi le: package net.learn2develop.UsingPreferences; import android.os.Bundle; import android.preference.PreferenceActivity; c06.indd 261 25/01/12 9:03 AM
  • 262 ❘ CHAPTER 6 DATA PERSISTENCE import android.preference.PreferenceManager; public class AppPreferenceActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceManager prefMgr = getPreferenceManager(); prefMgr.setSharedPreferencesName(“appPreferences”); //---load the preferences from an XML file--addPreferencesFromResource(R.xml.myapppreferences); } } Here, you make use of the PreferenceManager class to set the shared preferences fi le name to appPreferences.xml. Modify the UsingPreferencesActivity.java fi le as follows: public void onClickDisplay(View view) { /* SharedPreferences appPrefs = getSharedPreferences(“net.learn2develop.UsingPreferences_preferences”, MODE_PRIVATE); */ SharedPreferences appPrefs = getSharedPreferences(“appPreferences”, MODE_PRIVATE); DisplayText(appPrefs.getString(“editTextPref”, “”)); } public void onClickModify(View view) { /* SharedPreferences appPrefs = getSharedPreferences(“net.learn2develop.UsingPreferences_preferences”, MODE_PRIVATE); */ SharedPreferences appPrefs = getSharedPreferences(“appPreferences”, MODE_PRIVATE); SharedPreferences.Editor prefsEditor = appPrefs.edit(); prefsEditor.putString(“editTextPref”, ((EditText) findViewById(R.id.txtString)).getText().toString()); prefsEditor.commit(); } When you rerun the application and make changes to the preferences, you will notice that the appPreferences.xml fi le is now created (see Figure 6-9). c06.indd 262 25/01/12 9:03 AM
  • Persisting Data to Files ❘ 263 FIGURE 6-9 PERSISTING DATA TO FILES The SharedPreferences object enables you to store data that is best stored as name/value pairs — for example, user ID, birth date, gender, driving license number, and so on. However, sometimes you might prefer to use the traditional fi le system to store your data. For example, you might want to store the text of poems you want to display in your applications. In Android, you can use the classes in the java.io package to do so. Saving to Internal Storage The first way to save files in your Android application is to write to the device’s internal storage. The following Try It Out demonstrates how to save a string entered by the user to the device’s internal storage. TRY IT OUT Saving Data to Internal Storage codefile Files.zip available for download at Wrox.com 1. 2. Using Eclipse, create an Android project and name it Files. In the main.xml fi le, add the following statements in bold: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” c06.indd 263 25/01/12 9:03 AM
  • 264 ❘ CHAPTER 6 DATA PERSISTENCE android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Please enter some text” /> <EditText android:id=”@+id/txtText1” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> <Button android:id=”@+id/btnSave” android:text=”Save” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickSave” /> <Button android:id=”@+id/btnLoad” android:text=”Load” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickLoad” /> </LinearLayout> 3. In the FilesActivity.java fi le, add the following statements in bold: package net.learn2develop.Files; import import import import import java.io.FileInputStream; java.io.FileOutputStream; java.io.IOException; java.io.InputStreamReader; java.io.OutputStreamWriter; import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.widget.EditText; android.widget.Toast; public class FilesActivity extends Activity { EditText textBox; static final int READ_BLOCK_SIZE = 100; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textBox = (EditText) findViewById(R.id.txtText1); } public void onClickSave(View view) { c06.indd 264 25/01/12 9:03 AM
  • Persisting Data to Files ❘ 265 String str = textBox.getText().toString(); try { FileOutputStream fOut = openFileOutput(“textfile.txt”, MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); //---write the string to the file--osw.write(str); osw.flush(); osw.close(); //---display file saved message--Toast.makeText(getBaseContext(), “File saved successfully!”, Toast.LENGTH_SHORT).show(); //---clears the EditText--textBox.setText(“”); } catch (IOException ioe) { ioe.printStackTrace(); } } public void onClickLoad(View view) { try { FileInputStream fIn = openFileInput(“textfile.txt”); InputStreamReader isr = new InputStreamReader(fIn); char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = “”; int charRead; while ((charRead = isr.read(inputBuffer))>0) { //---convert the chars to a String--String readString = String.copyValueOf(inputBuffer, 0, charRead); s += readString; inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the text that has been // read--textBox.setText(s); Toast.makeText(getBaseContext(), “File loaded successfully!”, Toast.LENGTH_SHORT).show(); c06.indd 265 25/01/12 9:03 AM
  • 266 ❘ CHAPTER 6 DATA PERSISTENCE } catch (IOException ioe) { ioe.printStackTrace(); } } } 4. 5. Press F11 to debug the application on the Android emulator. Type some text into the EditText view (see Figure 6-10) and then click the Save button. 6. If the fi le is saved successfully, you will see the Toast class displaying the “File saved successfully!” message. The text in the EditText view should disappear. 7. Click the Load button and you should see the string appearing in the EditText view again. This confirms that the text is saved correctly. FIGURE 6-10 How It Works To save text into a fi le, you use the FileOutputStream class. The openFileOutput() method opens a named fi le for writing, with the mode specified. In this example, you used the MODE_WORLD_READABLE constant to indicate that the fi le is readable by all other applications: FileOutputStream fOut = openFileOutput(“textfile.txt”, MODE_WORLD_READABLE); Apart from the MODE_WORLD_READABLE constant, you can select from the following: MODE_PRIVATE (the fi le can only be accessed by the application that created it), MODE_APPEND (for appending to an existing fi le), and MODE_WORLD_WRITEABLE (all other applications have write access to the fi le). To convert a character stream into a byte stream, you use an instance of the OutputStreamWriter class, by passing it an instance of the FileOutputStream object: OutputStreamWriter osw = new OutputStreamWriter(fOut); You then use its write() method to write the string to the fi le. To ensure that all the bytes are written to the fi le, use the flush() method. Finally, use the close() method to close the file: //---write the string to the file--osw.write(str); osw.flush(); osw.close(); To read the content of a fi le, you use the FileInputStream class, together with the InputStreamReader class: FileInputStream fIn = openFileInput(“textfile.txt”); c06.indd 266 25/01/12 9:03 AM
  • Persisting Data to Files ❘ 267 InputStreamReader isr = new InputStreamReader(fIn); Because you do not know the size of the fi le to read, the content is read in blocks of 100 characters into a buffer (character array). The characters read are then copied into a String object: char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = “”; int charRead; while ((charRead = isr.read(inputBuffer))>0) { //---convert the chars to a String--String readString = String.copyValueOf(inputBuffer, 0, charRead); s += readString; inputBuffer = new char[READ_BLOCK_SIZE]; } The read() method of the InputStreamReader object checks the number of characters read and returns -1 if the end of the fi le is reached. When testing this application on the Android emulator, you can use the DDMS perspective to verify that the application did indeed save the fi le into the application’s fi les directory (see Figure 6-11; the entire path is /data/data/net.learn2develop.Files/files) FIGURE 6-11 c06.indd 267 25/01/12 9:03 AM
  • 268 ❘ CHAPTER 6 DATA PERSISTENCE Saving to External Storage (SD Card) The previous section showed how you can save your files to the internal storage of your Android device. Sometimes, it would be useful to save them to external storage (such as an SD card) because of its larger capacity, as well as the capability to share the fi les easily with other users (by removing the SD card and passing it to somebody else). Using the project created in the previous section as the example, to save the text entered by the user in the SD card, modify the onClick() method of the Save button as shown in bold here: import import import import import import java.io.File; java.io.FileInputStream; java.io.FileOutputStream; java.io.IOException; java.io.InputStreamReader; java.io.OutputStreamWriter; import import import import import import android.app.Activity; android.os.Bundle; android.os.Environment; android.view.View; android.widget.EditText; android.widget.Toast; public void onClickSave(View view) { String str = textBox.getText().toString(); try { //---SD Card Storage--File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard.getAbsolutePath() + “/MyFiles”); directory.mkdirs(); File file = new File(directory, “textfile.txt”); FileOutputStream fOut = new FileOutputStream(file); /* FileOutputStream fOut = openFileOutput(“textfile.txt”, MODE_WORLD_READABLE); */ OutputStreamWriter osw = new OutputStreamWriter(fOut); //---write the string to the file--osw.write(str); osw.flush(); osw.close(); //---display file saved message--- c06.indd 268 25/01/12 9:03 AM
  • Persisting Data to Files ❘ 269 Toast.makeText(getBaseContext(), “File saved successfully!”, Toast.LENGTH_SHORT).show(); //---clears the EditText--textBox.setText(“”); } catch (IOException ioe) { ioe.printStackTrace(); } } The preceding code uses the getExternalStorageDirectory() method to return the full path to the external storage. Typically, it should return the “/sdcard” path for a real device, and “/mnt/ sdcard” for an Android emulator. However, you should never try to hardcode the path to the SD card, as manufacturers may choose to assign a different path name to the SD card. Hence, be sure to use the getExternalStorageDirectory() method to return the full path to the SD card. You then create a directory called MyFiles in the SD card. Finally, you save the file into this directory. To load the fi le from the external storage, modify the onClickLoad() method for the Load button: public void onClickLoad(View view) { try { //---SD Storage--File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard.getAbsolutePath() + “/MyFiles”); File file = new File(directory, “textfile.txt”); FileInputStream fIn = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fIn); /* FileInputStream fIn = openFileInput(“textfile.txt”); InputStreamReader isr = new InputStreamReader(fIn); */ char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = “”; int charRead; while ((charRead = isr.read(inputBuffer))>0) { //---convert the chars to a String--String readString = c06.indd 269 25/01/12 9:03 AM
  • 270 ❘ CHAPTER 6 DATA PERSISTENCE String.copyValueOf(inputBuffer, 0, charRead); s += readString; inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the text that has been // read--textBox.setText(s); Toast.makeText(getBaseContext(), “File loaded successfully!”, Toast.LENGTH_SHORT).show(); } catch (IOException ioe) { ioe.printStackTrace(); } } Note that in order to write to the external storage, you need to add the WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml fi le: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.Files” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.FilesActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> If you run the preceding modified code, you will see the text fi le created in the /mnt/sdcard/ MyFiles/ folder (see Figure 6-12). c06.indd 270 25/01/12 9:03 AM
  • ❘ 271 Persisting Data to Files FIGURE 6-12 Choosing the Best Storage Option The previous sections described three main ways to save data in your Android applications: the SharedPreferences object, internal storage, and external storage. Which one should you use in your applications? Here are some guidelines: ➤ ➤ If you need to store ad-hoc data, then using the internal storage is a good option. For example, your application (such as an RSS reader) may need to download images from the web for display. In this scenario, saving the images to internal storage is a good solution. You may also need to persist data created by the user, such as when you have a note-taking application that enables users to take notes and save them for later use. In both of these scenarios, using the internal storage is a good choice. ➤ c06.indd 271 If you have data that can be represented using name/value pairs, then use the SharedPreferences object. For example, if you want to store user preference data such as user name, background color, date of birth, or last login date, then the SharedPreferences object is the ideal way to store this data. Moreover, you don’t really have to do much to store data this way; just use the SharedPreferences object to store and retrieve it. There are times when you need to share your application data with other users. For example, you may create an Android application that logs the coordinates of the locations that a user has been to, and you want to share all this data with other users. In this scenario, you can 25/01/12 9:03 AM
  • 272 ❘ CHAPTER 6 DATA PERSISTENCE store your files on the SD card of the device so that users can easily transfer the data to other devices (and computers) for use later. Using Static Resources Besides creating and using fi les dynamically during runtime, it is also possible to add fi les to your package during design time so that you can use it during runtime. For example, you may want to bundle some help fi les with your package so that you can display some help messages when users need them. In this case, you can add the fi les to your package’s res/raw folder (you need to create this folder yourself). Figure 6-13 shows the res/raw folder containing a fi le named textfile.txt. FIGURE 6-13 To make use of the fi le in code, use the getResources() method (of the Activity class) to return a Resources object, and then use its openRawResource() method to open the file contained in the res/raw folder: import java.io.BufferedReader; import java.io.InputStream; public class FilesActivity extends Activity { EditText textBox; static final int READ_BLOCK_SIZE = 100; /** Called when the activity is first created. */ @Override c06.indd 272 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 273 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textBox = (EditText) findViewById(R.id.txtText1); InputStream is = this.getResources().openRawResource(R.raw.textfile); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = null; try { while ((str = br.readLine()) != null) { Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show(); } is.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } The resource ID of the resource stored in the res/raw folder is named after its filename without its extension. For example, if the text file is textfile.txt, then its resource ID is R.raw.textfile. CREATING AND USING DATABASES So far, all the techniques you have seen are useful for saving simple sets of data. For saving relational data, using a database is much more efficient. For example, if you want to store the test results of all the students in a school, it is much more efficient to use a database to represent them because you can use database querying to retrieve the results of specific students. Moreover, using databases enables you to enforce data integrity by specifying the relationships between different sets of data. Android uses the SQLite database system. The database that you create for an application is only accessible to itself; other applications will not be able to access it. In this section, you will learn how to programmatically create a SQLite database in your Android application. For Android, the SQLite database that you create programmatically in an application is always stored in the /data/data/<package_name>/databases folder. Creating the DBAdapter Helper Class A good practice for dealing with databases is to create a helper class to encapsulate all the complexities of accessing the data so that it is transparent to the calling code. Hence, for this section, you will create a helper class called DBAdapter that creates, opens, closes, and uses a SQLite database. In this example, you are going to create a database named MyDB containing one table named contacts. This table will have three columns: _id, name, and email (see Figure 6-14). c06.indd 273 FIGURE 6-14 25/01/12 9:03 AM
  • 274 ❘ CHAPTER 6 DATA PERSISTENCE TRY IT OUT Creating the Database Helper Class codefile Databases.zip available for download at Wrox.com 1. 2. 3. Using Eclipse, create an Android project and name it Databases. Add a new Java Class fi le to the package and name it DBAdapter (see Figure 6-15). Add the following statements in bold to the DBAdapter.java fi le: package net.learn2develop.Databases; import import import import import import import android.content.ContentValues; android.content.Context; android.database.Cursor; android.database.SQLException; android.database.sqlite.SQLiteDatabase; android.database.sqlite.SQLiteOpenHelper; android.util.Log; FIGURE 6-15 public class DBAdapter { static final String KEY_ROWID = “_id”; static final String KEY_NAME = “name”; static final String KEY_EMAIL = “email”; static final String TAG = “DBAdapter”; static final String DATABASE_NAME = “MyDB”; static final String DATABASE_TABLE = “contacts”; static final int DATABASE_VERSION = 1; static final String DATABASE_CREATE = “create table contacts (_id integer primary key autoincrement, “ + “name text not null, email text not null);”; final Context context; DatabaseHelper DBHelper; SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) c06.indd 274 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 275 { try { db.execSQL(DATABASE_CREATE); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, “Upgrading database from version “ + oldVersion + “ to “ + newVersion + “, which will destroy all old data”); db.execSQL(“DROP TABLE IF EXISTS contacts”); onCreate(db); } } //---opens the database--public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } //---closes the database--public void close() { DBHelper.close(); } //---insert a contact into the database--public long insertContact(String name, String email) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_NAME, name); initialValues.put(KEY_EMAIL, email); return db.insert(DATABASE_TABLE, null, initialValues); } //---deletes a particular contact--public boolean deleteContact(long rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + “=” + rowId, null) > 0; } //---retrieves all the contacts--public Cursor getAllContacts() { return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_EMAIL}, null, null, null, null, null); } //---retrieves a particular contact--public Cursor getContact(long rowId) throws SQLException { c06.indd 275 25/01/12 9:03 AM
  • 276 ❘ CHAPTER 6 DATA PERSISTENCE Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_EMAIL}, KEY_ROWID + “=” + rowId, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } //---updates a contact--public boolean updateContact(long rowId, String name, String email) { ContentValues args = new ContentValues(); args.put(KEY_NAME, name); args.put(KEY_EMAIL, email); return db.update(DATABASE_TABLE, args, KEY_ROWID + “=” + rowId, null) > 0; } } How It Works You fi rst defi ned several constants to contain the various fields for the table that you are going to create in your database: static static static static final final final final String String String String KEY_ROWID = “_id”; KEY_NAME = “name”; KEY_EMAIL = “email”; TAG = “DBAdapter”; static final String DATABASE_NAME = “MyDB”; static final String DATABASE_TABLE = “contacts”; static final int DATABASE_VERSION = 1; static final String DATABASE_CREATE = “create table contacts (_id integer primary key autoincrement, “ + “name text not null, email text not null);”; In particular, the DATABASE_CREATE constant contains the SQL statement for creating the contacts table within the MyDB database. Within the DBAdapter class, you also added a private class that extended the SQLiteOpenHelper class, which is a helper class in Android to manage database creation and version management. In particular, you overrode the onCreate() and onUpgrade() methods: private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) c06.indd 276 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 277 { try { db.execSQL(DATABASE_CREATE); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, “Upgrading database from version “ + oldVersion + “ to “ + newVersion + “, which will destroy all old data”); db.execSQL(“DROP TABLE IF EXISTS contacts”); onCreate(db); } } The onCreate() method creates a new database if the required database is not present. The onUpgrade() method is called when the database needs to be upgraded. This is achieved by checking the value defi ned in the DATABASE_VERSION constant. For this implementation of the onUpgrade() method, you simply drop the table and create it again. You can then defi ne the various methods for opening and closing the database, as well as the methods for adding/editing/deleting rows in the table: //---opens the database--public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } //---closes the database--public void close() { DBHelper.close(); } //---insert a contact into the database--public long insertContact(String name, String email) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_NAME, name); initialValues.put(KEY_EMAIL, email); return db.insert(DATABASE_TABLE, null, initialValues); } //---deletes a particular contact--public boolean deleteContact(long rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + “=” + rowId, null) > 0; } //---retrieves all the contacts--- c06.indd 277 25/01/12 9:03 AM
  • 278 ❘ CHAPTER 6 DATA PERSISTENCE public Cursor getAllContacts() { return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_EMAIL}, null, null, null, null, null); } //---retrieves a particular contact--public Cursor getContact(long rowId) throws SQLException { Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_EMAIL}, KEY_ROWID + “=” + rowId, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } //---updates a contact--public boolean updateContact(long rowId, String name, String email) { ContentValues args = new ContentValues(); args.put(KEY_NAME, name); args.put(KEY_EMAIL, email); return db.update(DATABASE_TABLE, args, KEY_ROWID + “=” + rowId, null) > 0; } Notice that Android uses the Cursor class as a return value for queries. Think of the Cursor as a pointer to the result set from a database query. Using Cursor enables Android to more efficiently manage rows and columns as needed. You use a ContentValues object to store name/value pairs. Its put() method enables you to insert keys with values of different data types. To create a database in your application using the DBAdapter class, you create an instance of the DBAdapter class: public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } The constructor of the DBAdapter class will then create an instance of the DatabaseHelper class to create a new database: DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } c06.indd 278 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 279 Using the Database Programmatically With the DBAdapter helper class created, you are now ready to use the database. In the following sections, you will learn how to perform the regular CRUD (create, read, update and delete) operations commonly associated with databases. Adding Contacts The following Try It Out demonstrates how you can add a contact to the table. Adding Contacts to a Table TRY IT OUT codefile Databases.zip available for download at Wrox.com 1. Using the same project created earlier, add the following statements in bold to the DatabasesActivity.java fi le: package net.learn2develop.Databases; import android.app.Activity; import android.os.Bundle; public class DatabasesActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); //---add a contact--db.open(); long id = db.insertContact(“Wei-Meng Lee”, “weimenglee@learn2develop.net”); id = db.insertContact(“Mary Jackson”, “mary@jackson.com”); db.close(); } } 2. Press F11 to debug the application on the Android emulator. How It Works In this example, you fi rst created an instance of the DBAdapter class: DBAdapter db = new DBAdapter(this); The insertContact() method returns the ID of the inserted row. If an error occurs during the operation, it returns -1. If you examine the fi le system of the Android device/emulator using DDMS, you can see that the MyDB database is created under the databases folder (see Figure 6-16). c06.indd 279 25/01/12 9:03 AM
  • 280 ❘ CHAPTER 6 DATA PERSISTENCE FIGURE 6-16 Retrieving All the Contacts To retrieve all the contacts in the contacts table, use the getAllContacts() method of the DBAdapter class, as the following Try It Out shows. TRY IT OUY Retrieving All Contacts from a Table codefile Databases.zip available for download at Wrox.com 1. Using the same project created earlier, add the following statements in bold to the DatabasesActivity.java fi le: package net.learn2develop.Databases; import import import import android.app.Activity; android.database.Cursor; android.os.Bundle; android.widget.Toast; public class DatabasesActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); /* //---add a contact--- c06.indd 280 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 281 db.open(); ... db.close(); */ //---get all contacts--db.open(); Cursor c = db.getAllContacts(); if (c.moveToFirst()) { do { DisplayContact(c); } while (c.moveToNext()); } db.close(); } public void DisplayContact(Cursor c) { Toast.makeText(this, “id: “ + c.getString(0) + “n” + “Name: “ + c.getString(1) + “n” + “Email: “ + c.getString(2), Toast.LENGTH_LONG).show(); } } 2. Press F11 to debug the application on the Android emulator. Figure 6-17 shows the Toast class displaying the contacts retrieved from the database. FIGURE 6-17 How It Works The getAllContacts() method of the DBAdapter class retrieves all the contacts stored in the database. The result is returned as a Cursor object. To display all the contacts, you fi rst need to call the moveToFirst() method of the Cursor object. If it succeeds (which means at least one row is available), then you display the details of the contact using the DisplayContact() method. To move to the next contact, call the moveToNext() method of the Cursor object. Retrieving a Single Contact To retrieve a single contact using its ID, call the getContact() method of the DBAdapter class, as the following Try It Out shows. TRY IT OUT Retrieving a Contact from a Table codefile Databases.zip available for download at Wrox.com 1. Using the same project created earlier, add the following statements in bold to the DatabasesActivity.java file: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); c06.indd 281 25/01/12 9:03 AM
  • 282 ❘ CHAPTER 6 DATA PERSISTENCE setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); /* //---add a contact--... //--get all contacts--... db.close(); */ //---get a contact--db.open(); Cursor c = db.getContact(2); if (c.moveToFirst()) DisplayContact(c); else Toast.makeText(this, “No contact found”, Toast.LENGTH_LONG).show(); db.close(); } 2. Press F11 to debug the application on the Android emulator. The details of the second contact will be displayed using the Toast class. How It Works The getContact() method of the DBAdapter class retrieves a single contact using its ID. You passed in the ID of the contact; in this case, you passed in an ID of 2 to indicate that you want to retrieve the second contact: Cursor c = db.getContact(2); The result is returned as a Cursor object. If a row is returned, you display the details of the contact using the DisplayContact() method; otherwise, you display a message using the Toast class. Updating a Contact To update a particular contact, call the updateContact() method in the DBAdapter class by passing the ID of the contact you want to update, as the following Try It Out shows. TRY IT OUT Updating a Contact in a Table codefile Databases.zip available for download at Wrox.com 1. Using the same project created earlier, add the following statements in bold to the DatabasesActivity.java fi le: @Override public void onCreate(Bundle savedInstanceState) { c06.indd 282 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 283 super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); /* //---add a contact--... //--get all contacts--... //---get a contact--... db.close(); */ //---update contact--db.open(); if (db.updateContact(1, “Wei-Meng Lee”, “weimenglee@gmail.com”)) Toast.makeText(this, “Update successful.”, Toast.LENGTH_LONG).show(); else Toast.makeText(this, “Update failed.”, Toast.LENGTH_LONG).show(); db.close(); } 2. Press F11 to debug the application on the Android emulator. A message will be displayed if the update is successful. How It Works The updateContact() method in the DBAdapter class updates a contact’s details by using the ID of the contact you want to update. It returns a Boolean value, indicating whether the update was successful. Deleting a Contact To delete a contact, use the deleteContact() method in the DBAdapter class by passing the ID of the contact you want to update, as the following Try It Out shows. TRY IT OUT Deleting a Contact from a Table codefile Databases.zip available for download at Wrox.com 1. Using the same project created earlier, add the following statements in bold to the DatabasesActivity.java fi le: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); c06.indd 283 25/01/12 9:03 AM
  • 284 ❘ CHAPTER 6 DATA PERSISTENCE setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); /* //---add a contact--... //--get all contacts--... //---get a contact--... //---update contact--... db.close(); */ //---delete a contact--db.open(); if (db.deleteContact(1)) Toast.makeText(this, “Delete successful.”, Toast.LENGTH_LONG).show(); else Toast.makeText(this, “Delete failed.”, Toast.LENGTH_LONG).show(); db.close(); } 2. Press F11 to debug the application on the Android emulator. A message is displayed if the deletion was successful. How It Works The deleteContact() method in the DBAdapter class deletes a contact using the ID of the contact you want to delete. It returns a Boolean value, indicating whether the deletion was successful. Upgrading the Database Sometimes, after creating and using the database, you may need to add additional tables, change the schema of the database, or add columns to your tables. In this case, you need to migrate your existing data from the old database to a newer one. To upgrade the database, change the DATABASE_VERSION constant to a value higher than the previous one. For example, if its previous value was 1, change it to 2: public class DBAdapter { static final String KEY_ROWID = “_id”; static final String KEY_NAME = “name”; static final String KEY_EMAIL = “email”; static final String TAG = “DBAdapter”; static final String DATABASE_NAME = “MyDB”; static final String DATABASE_TABLE = “contacts”; static final int DATABASE_VERSION = 2; c06.indd 284 25/01/12 9:03 AM
  • Creating and Using Databases ❘ 285 NOTE Before you run this example, be sure to comment out the block of delete statements described in the previous section. If not, the deletion will fail as the table in the database will be dropped (deleted). When you run the application one more time, you will see the following message in the LogCat window of Eclipse: DBAdapter(8705): Upgrading database from version 1 to 2, which will destroy all old data In this example, for simplicity you simply drop the existing table and create a new one. In real life, you usually back up your existing table and then copy it over to the new table. Pre-Creating the Database In real-life applications, sometimes it would be more efficient to pre-create the database at design time rather than runtime. For example, you want to create an application to log the coordinates of all the places that you have been to. In this case, it is much easier to pre-create the database during the design time and simply use it during runtime. To pre-create a SQLite database, you can use many of the free tools available on the Internet. One such tool is the SQLite Database Browser, which is available free for different platforms (http:// sourceforge.net/projects/sqlitebrowser/). Once you have installed the SQLite Database Browser, you can create a database visually. Figure 6-18 shows that I have created a contacts table with the fields indicated. FIGURE 6-18 Populating the table with rows is also straightforward. Figure 6-19 shows how you can fi ll the table with data using the Browse Data tab. c06.indd 285 25/01/12 9:03 AM
  • 286 ❘ CHAPTER 6 DATA PERSISTENCE FIGURE 6-19 With the database created at design time, the next thing you should do is bundle it together with your application so that you can use it in your application. The following Try It Out shows you how. TRY IT OUT 1. Bundling a Database Using the same project created earlier, drag and drop the SQLite database file that you have created in the previous section into the assets folder in your Android project in Eclipse (see Figure 6-20). FIGURE 6-20 NOTE Note that a filename for files added to the assets folder must be in lowercase letters. As such, a filename such as MyDB is invalid, whereas mydb is fine. c06.indd 286 25/01/12 9:03 AM
  • Creating and Using Databases 2. ❘ 287 Add the following statements in bold to the DatabasesActivity.java fi le: package net.learn2develop.Databases; import import import import import import java.io.File; java.io.FileNotFoundException; java.io.FileOutputStream; java.io.IOException; java.io.InputStream; java.io.OutputStream; import import import import android.app.Activity; android.database.Cursor; android.os.Bundle; android.widget.Toast; public class DatabasesActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); try { String destPath = “/data/data/” + getPackageName() + “/databases”; File f = new File(destPath); if (!f.exists()) { f.mkdirs(); f.createNewFile(); //---copy the db from the assets folder into // the databases folder--CopyDB(getBaseContext().getAssets().open(“mydb”), new FileOutputStream(destPath + “/MyDB”)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //---get all contacts--db.open(); Cursor c = db.getAllContacts(); if (c.moveToFirst()) { do { DisplayContact(c); } while (c.moveToNext()); } db.close(); c06.indd 287 25/01/12 9:03 AM
  • 288 ❘ CHAPTER 6 DATA PERSISTENCE } public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException { //---copy 1K bytes at a time--byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } public void DisplayContact(Cursor c) { Toast.makeText(this, “id: “ + c.getString(0) + “n” + “Name: “ + c.getString(1) + “n” + “Email: “ + c.getString(2), Toast.LENGTH_LONG).show(); } } 3. Press F11 to debug the application on the Android emulator. When the application runs, it will copy the mydb database fi le into the /data/data/net.learn2develop.Databases/databases/ folder with the name MyDB. How It Works You fi rst defi ned the CopyDB() method to copy the database file from one location to another: public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException { //---copy 1K bytes at a time--byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } Note that in this case you used the InputStream object to read from the source fi le, and then wrote it to the destination fi le using the OutputStream object. When the activity is created, you copy the database fi le located in the assets folder into the /data/ data/net.learn2develop.Databases/databases/ folder on the Android device (or emulator): try { String destPath = “/data/data/” + getPackageName() + “/databases”; File f = new File(destPath); c06.indd 288 25/01/12 9:03 AM
  • Summary ❘ 289 if (!f.exists()) { f.mkdirs(); f.createNewFile(); //---copy the db from the assets folder into // the databases folder--CopyDB(getBaseContext().getAssets().open(“mydb”), new FileOutputStream(destPath + “/MyDB”)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } You copy the database fi le only if it does not exist in the destination folder. If you don’t perform this check, every time the activity is created you will overwrite the database fi le with the one in the assets folder. This may not be desirable, as your application may make changes to the database file during runtime, and this will overwrite all the changes you have made so far. To ensure that the database file is indeed copied, be sure to delete the database file in your emulator (if it already existed) prior to testing the application. You can delete the database using DDMS (see Figure 6-21). FIGURE 6-21 SUMMARY In this chapter, you learned the different ways to save persistent data to your Android device. For simple unstructured data, using the SharedPreferences object is the ideal solution. If you need to store bulk data, then consider using the traditional fi le system. Finally, for structured data, it is c06.indd 289 25/01/12 9:03 AM
  • 290 ❘ CHAPTER 6 DATA PERSISTENCE more efficient to store it in a relational database management system. For this, Android provides the SQLite database, which you can access easily using the APIs exposed. Note that for the SharedPreferences object and the SQLite database, the data is accessible only by the application that creates it. In other words, it is not shareable. If you need to share data among different applications, you need to create a content provider. Content providers are discussed in more detail in the next chapter. EXERCISES 1. How do you display the preferences of your application using an activity? 2. Name the method that enables you to obtain the path of the external storage of an Android device. 3. What is the permission you need to declare when writing files to external storage? Answers to the exercises can be found in Appendix C. c06.indd 290 25/01/12 9:03 AM
  • ❘ 291 Summary WHAT YOU LEARNED IN THIS CHAPTER TOPIC Saving simple user data Use the SharedPreferences object. Sharing data among activities in the same application Use the getSharedPreferences() method. Saving to a file Use the FileOutputStream and OutputStreamReader classes. Reading from a file Use the FileInputStream and InputStreamReader classes. Saving to external storage Use the getExternalStorageDirectory() method to return the path to the external storage. Accessing files in the res/raw folder Use the openRawResource() method in the Resources object (obtained via the getResources() method). Creating a database helper class c06.indd 291 KEY CONCEPTS Extend the SQLiteOpenHelper class. 25/01/12 9:03 AM
  • c06.indd 292 25/01/12 9:03 AM
  • 7 Content Providers WHAT YOU WILL LEARN IN THIS CHAPTER ➤ What are content providers? ➤ How to use a content provider in Android ➤ How to create and use your own content provider In the previous chapter, you learned about the various ways to persist data — using shared preferences, fi les, as well as SQLite databases. While using the database approach is the recommended way to save structured and complex data, sharing data is a challenge because the database is accessible to only the package that created it. In this chapter, you will learn Android’s way of sharing data through the use of content providers. You will learn how to use the built-in content providers, as well as implement your own content providers to share data across packages. SHARING DATA IN ANDROID In Android, using a content provider is the recommended way to share data across packages. Think of a content provider as a data store. How it stores its data is not relevant to the application using it; what is important is how packages can access the data stored in it using a consistent programming interface. A content provider behaves very much like a database — you can query it, edit its content, as well as add or delete content. However, unlike a database, a content provider can use different ways to store its data. The data can be stored in a database, in fi les, or even over a network. Android ships with many useful content providers, including the following: ➤ ➤ c07.indd 293 Browser — Stores data such as browser bookmarks, browser history, and so on CallLog — Stores data such as missed calls, call details, and so on 25/01/12 9:03 AM
  • 294 ❘ CHAPTER 7 CONTENT PROVIDERS ➤ Contacts — Stores contact details ➤ MediaStore — Stores media files such as audio, video, and images ➤ Settings — Stores the device’s settings and preferences Besides the many built-in content providers, you can also create your own content providers. To query a content provider, you specify the query string in the form of a URI, with an optional specifier for a particular row. The format of the query URI is as follows: <standard_prefix>://<authority>/<data_path>/<id> The various parts of the URI are as follows: ➤ The standard prefix for content providers is always content://. ➤ The authority specifies the name of the content provider. An example would be contacts for the built-in Contacts content provider. For third-party content providers, this could be the fully qualified name, such as com.wrox.provider or net.learn2develop.provider. ➤ The data path specifies the kind of data requested. For example, if you are getting all the contacts from the Contacts content provider, then the data path would be people, and the URI would look like this: content://contacts/people. ➤ The id specifies the specific record requested. For example, if you are looking for contact number 2 in the Contacts content provider, the URI would look like this: content:// contacts/people/2. Table 7-1 shows some examples of query strings. TABLE 7-1: Example Query Strings QUERY STRING DESCRIPTION content://media/internal/images Returns a list of all the internal images on the device content://media/external/images Returns a list of all the images stored on the external storage (e.g., SD card) on the device content://call_log/calls Returns a list of all calls registered in the Call Log content://browser/bookmarks Returns a list of bookmarks stored in the browser USING A CONTENT PROVIDER The best way to understand content providers is to actually use one. The following Try It Out shows how you can use a content provider from within your Android application. c07.indd 294 25/01/12 9:03 AM
  • Using a Content Provider TRY IT OUT ❘ 295 Using the Contacts Content Provider codefile Provider.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Provider. Add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <ListView android:id=”@+id/android:list” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:layout_weight=”1” android:stackFromBottom=”false” android:transcriptMode=”normal” /> <TextView android:id=”@+id/contactName” android:textStyle=”bold” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> <TextView android:id=”@+id/contactID” android:layout_width=”fill_parent” android:layout_height=”wrap_content” /> </LinearLayout> 3. In the ProviderActivity.java class, code the following: package net.learn2develop.Provider; import import import import import import import import android.app.ListActivity; android.content.CursorLoader; android.database.Cursor; android.net.Uri; android.os.Bundle; android.provider.ContactsContract; android.widget.CursorAdapter; android.widget.SimpleCursorAdapter; public class ProviderActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); c07.indd 295 25/01/12 9:03 AM
  • 296 ❘ CHAPTER 7 CONTENT PROVIDERS setContentView(R.layout.main); Uri allContacts = Uri.parse(“content://contacts/people”); Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, null, null, null, null); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allContacts, null, null, null , null); c = cursorLoader.loadInBackground(); } String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID}; int[] views = new int[] {R.id.contactName, R.id.contactID}; SimpleCursorAdapter adapter; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views); } else { //---Honeycomb and later--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); } this.setListAdapter(adapter); } } 4. Add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.Provider” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <uses-permission android:name=”android.permission.READ_CONTACTS”/> <application c07.indd 296 25/01/12 9:03 AM
  • Using a Content Provider ❘ 297 android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.ProviderActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> 5. Launch an AVD and create a few contacts in the Android Emulator. To add a contact, go to the Phone application and click the Star icon at the top (see Figure 7-1). Click the MENU button on the emulator and click the New contact menu item. You will be warned about backing up your contacts. Click the Keep Local button and enter the name, phone number, and e-mail address of a few people. 6. Press F11 to debug the application on the Android emulator. Figure 7-2 shows the activity displaying the list of contacts you just created. FIGURE 7-1 c07.indd 297 FIGURE 7-2 25/01/12 9:03 AM
  • 298 ❘ CHAPTER 7 CONTENT PROVIDERS How It Works In this example, you retrieved all the contacts stored in the Contacts application and displayed them in the ListView. First, you specified the URI for accessing the Contacts application: Uri allContacts = Uri.parse(“content://contacts/people”); Next, observe that you have a conditional check to detect the version of the device on which the application is currently running: Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, null, null, null, null); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allContacts, null, null, null , null); c = cursorLoader.loadInBackground(); } If the application is running on pre-Honeycomb devices (the value of the android.os.Build.VERSION .SDK_INT variable is lower than 11), you can use the managedQuery() method of the Activity class to retrieve a managed cursor. A managed cursor handles all the work of unloading itself when the application pauses and requerying itself when the application restarts. The statement Cursor c = managedQuery(allContacts, null, null, null, null); is equivalent to Cursor c = getContentResolver().query(allContacts, null, null, null, null); //---allows the activity to manage the Cursor’s // lifecyle based on the activity’s lifecycle--startManagingCursor(c); The getContentResolver() method returns a ContentResolver object, which helps to resolve a content URI with the appropriate content provider. However, beginning with Android API level 11 (Honeycomb and later), the managedQuery() method is deprecated (still available but not recommended for use). For Honeycomb or later devices, use the CursorLoader class: CursorLoader cursorLoader = new CursorLoader( this, allContacts, c07.indd 298 25/01/12 9:03 AM
  • Using a Content Provider ❘ 299 null, null, null , null); c = cursorLoader.loadInBackground(); The CursorLoader class (only available beginning with Android API level 11 and later) performs the cursor query on a background thread and hence does not block the application UI. The SimpleCursorAdapter object maps a cursor to TextViews (or ImageViews) defi ned in your XML fi le (main.xml). It maps the data (as represented by columns) to views (as represented by views): String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID}; int[] views = new int[] {R.id.contactName, R.id.contactID}; SimpleCursorAdapter adapter; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views); } else { //---Honeycomb and later--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); } this.setListAdapter(adapter); Like the managedQuery() method, one of the constructors for the SimpleCursorAdapter class has been deprecated. For Honeycomb or later devices, you need to use the new constructor for the SimpleCursorAdapter class with one additional argument: //---Honeycomb and later--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); The flag registers the adapter to be informed when there is a change in the content provider. Note that in order for your application to access the Contacts application, you need to have the READ_ CONTACTS permission in your AndroidManifest.xml fi le. c07.indd 299 25/01/12 9:03 AM
  • 300 ❘ CHAPTER 7 CONTENT PROVIDERS Predefined Query String Constants Besides using the query URI, you can use a list of predefi ned query string constants in Android to specify the URI for the different data types. For example, besides using the query content:// contacts/people, you can rewrite the statement Uri allContacts = Uri.parse(“content://contacts/people”); using one of the predefi ned constants in Android, as follows: Uri allContacts = ContactsContract.Contacts.CONTENT_URI; NOTE For Android 2.0 and later, to query the base Contacts records you need to use the ContactsContract.Contacts.CONTENT_URI URI. Some examples of predefi ned query string constants are as follows: ➤ Browser.BOOKMARKS_URI ➤ Browser.SEARCHES_URI ➤ CallLog.CONTENT_URI ➤ MediaStore.Images.Media.INTERNAL_CONTENT_URI ➤ MediaStore.Images.Media.EXTERNAL_CONTENT_URI ➤ Settings.CONTENT_URI If you want to retrieve the fi rst contact, specify the ID of that contact, like this: Uri allContacts = Uri.parse(“content://contacts/people/1”); Alternatively, use the predefi ned constant together with the withAppendedId() method of the ContentUris class: import android.content.ContentUris; ... Uri allContacts = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, 1); Besides binding to a ListView, you can also print out the results using the Cursor object, as shown here: package net.learn2develop.Provider; import android.app.ListActivity; import android.content.CursorLoader; import android.database.Cursor; c07.indd 300 25/01/12 9:03 AM
  • Using a Content Provider ❘ 301 import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.widget.CursorAdapter; import android.widget.SimpleCursorAdapter; import android.util.Log; public class ProviderActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Uri allContacts = ContactsContract.Contacts.CONTENT_URI; ... ... if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views); } else { //---Honeycomb and later--adapter = new SimpleCursorAdapter( this, R.layout.main, c, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); } this.setListAdapter(adapter); PrintContacts(c); } private void PrintContacts(Cursor c) { if (c.moveToFirst()) { do{ String contactID = c.getString(c.getColumnIndex( ContactsContract.Contacts._ID)); String contactDisplayName = c.getString(c.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); Log.v(“Content Providers”, contactID + “, “ + contactDisplayName); } while (c.moveToNext()); } } } NOTE If you don’t know how to view the LogCat window, refer to Appendix A for a quick tour of the Eclipse IDE. c07.indd 301 25/01/12 9:03 AM
  • 302 ❘ CHAPTER 7 CONTENT PROVIDERS The PrintContacts() method will print out the following in the LogCat window: 12-13 08:32:50.471: V/Content Providers(12346): 1, Wei-Meng Lee 12-13 08:32:50.471: V/Content Providers(12346): 2, Linda Chen 12-13 08:32:50.471: V/Content Providers(12346): 3, Joanna Yip It prints out the ID and name of each contact stored in the Contacts application. In this case, you access the ContactsContract.Contacts._ID field to obtain the ID of a contact, and ContactsContract.Contacts.DISPLAY_NAME for the name of a contact. If you want to display the phone number of a contact, you need to query the content provider again, as the information is stored in another table: private void PrintContacts(Cursor c) { if (c.moveToFirst()) { do{ String contactID = c.getString(c.getColumnIndex( ContactsContract.Contacts._ID)); String contactDisplayName = c.getString(c.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); Log.v(“Content Providers”, contactID + “, “ + contactDisplayName); //---get phone number--int hasPhone = c.getInt(c.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (hasPhone == 1) { Cursor phoneCursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + “ = “ + contactID, null, null); while (phoneCursor.moveToNext()) { Log.v(“Content Providers”, phoneCursor.getString( phoneCursor.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER))); } phoneCursor.close(); } } while (c.moveToNext()); } } NOTE To access the phone number of a contact, you need to query against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. c07.indd 302 25/01/12 9:03 AM
  • Using a Content Provider ❘ 303 In the preceding code snippet, you fi rst check whether a contact has a phone number using the ContactsContract.Contacts.HAS_PHONE_NUMBER field. If the contact has at least a phone number, you then query the content provider again based on the ID of the contact. Once the phone number(s) are retrieved, you then iterate through them and print out the numbers. You should see something like this: 12-13 12-13 12-13 12-13 12-13 12-13 08:59:31.881: 08:59:32.311: 08:59:32.321: 08:59:32.511: 08:59:32.545: 08:59:32.641: V/Content V/Content V/Content V/Content V/Content V/Content Providers(13351): Providers(13351): Providers(13351): Providers(13351): Providers(13351): Providers(13351): 1, Wei-Meng Lee +651234567 2, Linda Chen +1 876-543-21 3, Joanna Yip +239 846 5522 Projections The second parameter of the managedQuery() method (third parameter for the CursorLoader class) controls how many columns are returned by the query; this parameter is known as the projection. Earlier, you specified null: Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, null, null, null, null); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allContacts, null, null, null , null); c = cursorLoader.loadInBackground(); } You can specify the exact columns to return by creating an array containing the name of the column to return, like this: String[] projection = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}; Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, projection, null, null, null); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, c07.indd 303 25/01/12 9:03 AM
  • 304 ❘ CHAPTER 7 CONTENT PROVIDERS allContacts, projection, null, null , null); c = cursorLoader.loadInBackground(); } In the above case, the _ID, DISPLAY_NAME, and HAS_PHONE_NUMBER fields will be retrieved. Filtering The third and fourth parameters of the managedQuery() method (fourth and fi fth parameters for the CursorLoader class) enable you to specify a SQL WHERE clause to filter the result of the query. For example, the following statement retrieves only the people whose name ends with “Lee”: Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + “ LIKE ‘%Lee’”, null, null); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + “ LIKE ‘%Lee’”, null , null); c = cursorLoader.loadInBackground(); } Here, the third parameter (for the managedQuery() method, and the fourth parameter for the CursorLoader constructor) contains a SQL statement containing the name to search for (“Lee”). You can also put the search string into the next argument of the method/constructor, like this: Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + “ LIKE ?”, new String[] {“%Lee”}, null); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + “ LIKE ?”, c07.indd 304 25/01/12 9:03 AM
  • Creating Your Own Content Providers ❘ 305 new String[] {“%Lee”}, null); c = cursorLoader.loadInBackground(); } Sorting The last parameter of the managedQuery() method (and constructor for the CursorLoader class) enables you to specify a SQL ORDER BY clause to sort the result of the query. For example, the following statement sorts the contact names in ascending order: Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME new String[] {“%Lee”}, ContactsContract.Contacts.DISPLAY_NAME } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME new String[] {“%Lee”}, ContactsContract.Contacts.DISPLAY_NAME c = cursorLoader.loadInBackground(); } + “ LIKE ?”, + “ ASC”); + “ LIKE ?”, + “ ASC”); CREATING YOUR OWN CONTENT PROVIDERS Creating your own content provider in Android is relatively simple. All you need to do is extend the abstract ContentProvider class and override the various methods defi ned within it. In this section, you will learn how to create a simple content provider that stores a list of books. For ease of illustration, the content provider stores the books in a database table containing three fields, as shown in Figure 7-3. FIGURE 7-3 The following Try It Out shows you the steps. TRY IT OUT Creating Your Own Content Provider codefile ContentProviders.zip available for download at Wrox.com 1. 2. c07.indd 305 Using Eclipse, create a new Android project and name it ContentProviders. In the src folder of the project, add a new Java class file and name it BooksProvider. 25/01/12 9:03 AM
  • 306 3. ❘ CHAPTER 7 CONTENT PROVIDERS Populate the BooksProvider.java fi le as follows: package net.learn2develop.ContentProviders; import import import import import import import import import import import import import android.content.ContentProvider; android.content.ContentUris; android.content.ContentValues; android.content.Context; android.content.UriMatcher; android.database.Cursor; android.database.SQLException; android.database.sqlite.SQLiteDatabase; android.database.sqlite.SQLiteOpenHelper; android.database.sqlite.SQLiteQueryBuilder; android.net.Uri; android.text.TextUtils; android.util.Log; public class BooksProvider extends ContentProvider { static final String PROVIDER_NAME = “net.learn2develop.provider.Books”; static final Uri CONTENT_URI = Uri.parse(“content://”+ PROVIDER_NAME + “/books”); static final String _ID = “_id”; static final String TITLE = “title”; static final String ISBN = “isbn”; static final int BOOKS = 1; static final int BOOK_ID = 2; private static final UriMatcher uriMatcher; static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(PROVIDER_NAME, “books”, BOOKS); uriMatcher.addURI(PROVIDER_NAME, “books/#”, BOOK_ID); } //---for database use--SQLiteDatabase booksDB; static final String DATABASE_NAME = “Books”; static final String DATABASE_TABLE = “titles”; static final int DATABASE_VERSION = 1; static final String DATABASE_CREATE = “create table “ + DATABASE_TABLE + “ (_id integer primary key autoincrement, “ + “title text not null, isbn text not null);”; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { c07.indd 306 25/01/12 9:03 AM
  • Creating Your Own Content Providers ❘ 307 super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(“Content provider database”, “Upgrading database from version “ + oldVersion + “ to “ + newVersion + “, which will destroy all old data”); db.execSQL(“DROP TABLE IF EXISTS titles”); onCreate(db); } } @Override public int delete(Uri arg0, String arg1, String[] arg2) { // arg0 = uri // arg1 = selection // arg2 = selectionArgs int count=0; switch (uriMatcher.match(arg0)){ case BOOKS: count = booksDB.delete( DATABASE_TABLE, arg1, arg2); break; case BOOK_ID: String id = arg0.getPathSegments().get(1); count = booksDB.delete( DATABASE_TABLE, _ID + “ = “ + id + (!TextUtils.isEmpty(arg1) ? “ AND (“ + arg1 + ‘)’ : “”), arg2); break; default: throw new IllegalArgumentException(“Unknown URI “ + arg0); } getContext().getContentResolver().notifyChange(arg0, null); return count; } @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)){ //---get all books--case BOOKS: c07.indd 307 25/01/12 9:03 AM
  • 308 ❘ CHAPTER 7 CONTENT PROVIDERS return “vnd.android.cursor.dir/vnd.learn2develop.books “; //---get a particular book--case BOOK_ID: return “vnd.android.cursor.item/vnd.learn2develop.books “; default: throw new IllegalArgumentException(“Unsupported URI: “ + uri); } } @Override public Uri insert(Uri uri, ContentValues values) { //---add a new book--long rowID = booksDB.insert( DATABASE_TABLE, “”, values); //---if added successfully--if (rowID>0) { Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); getContext().getContentResolver().notifyChange(_uri, null); return _uri; } throw new SQLException(“Failed to insert row into “ + uri); } @Override public boolean onCreate() { Context context = getContext(); DatabaseHelper dbHelper = new DatabaseHelper(context); booksDB = dbHelper.getWritableDatabase(); return (booksDB == null)? false:true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); sqlBuilder.setTables(DATABASE_TABLE); if (uriMatcher.match(uri) == BOOK_ID) //---if getting a particular book--sqlBuilder.appendWhere( _ID + “ = “ + uri.getPathSegments().get(1)); if (sortOrder==null || sortOrder==””) sortOrder = TITLE; Cursor c = sqlBuilder.query( booksDB, projection, c07.indd 308 25/01/12 9:03 AM
  • Creating Your Own Content Providers ❘ 309 selection, selectionArgs, null, null, sortOrder); //---register to watch a content URI for changes--c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0; switch (uriMatcher.match(uri)){ case BOOKS: count = booksDB.update( DATABASE_TABLE, values, selection, selectionArgs); break; case BOOK_ID: count = booksDB.update( DATABASE_TABLE, values, _ID + “ = “ + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? “ AND (“ + selection + ‘)’ : “”), selectionArgs); break; default: throw new IllegalArgumentException(“Unknown URI “ + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } } 4. Add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.ContentProviders” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.ContentProvidersActivity” > c07.indd 309 25/01/12 9:03 AM
  • 310 ❘ CHAPTER 7 CONTENT PROVIDERS <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <provider android:name=”BooksProvider” android:authorities=”net.learn2develop.provider.Books”> </provider> </application> </manifest> How It Works In this example, you fi rst created a class named BooksProvider that extends the ContentProvider base class. The various methods to override in this class are as follows: ➤ getType() — Returns the MIME type of the data at the given URI ➤ onCreate() — Called when the provider is started ➤ query() — Receives a request from a client. The result is returned as a Cursor object. ➤ insert() — Inserts a new record into the content provider ➤ delete() — Deletes an existing record from the content provider ➤ update() — Updates an existing record from the content provider Within your content provider, you are free to choose how you want to store your data — a traditional fi le system, XML, a database, or even through web services. For this example, you used the SQLite database approach discussed in the previous chapter. You then defi ned the following constants within the BooksProvider class: static final String PROVIDER_NAME = “net.learn2develop.provider.Books”; static final Uri CONTENT_URI = Uri.parse(“content://”+ PROVIDER_NAME + “/books”); static final String _ID = “_id”; static final String TITLE = “title”; static final String ISBN = “isbn”; static final int BOOKS = 1; static final int BOOK_ID = 2; private static final UriMatcher uriMatcher; static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(PROVIDER_NAME, “books”, BOOKS); uriMatcher.addURI(PROVIDER_NAME, “books/#”, BOOK_ID); } //---for database use--SQLiteDatabase booksDB; c07.indd 310 25/01/12 9:03 AM
  • Creating Your Own Content Providers ❘ 311 static final String DATABASE_NAME = “Books”; static final String DATABASE_TABLE = “titles”; static final int DATABASE_VERSION = 1; static final String DATABASE_CREATE = “create table “ + DATABASE_TABLE + “ (_id integer primary key autoincrement, “ + “title text not null, isbn text not null);”; Observe in the preceding code that you used an UriMatcher object to parse the content URI that is passed to the content provider through a ContentResolver. For example, the following content URI represents a request for all books in the content provider: content://net.learn2develop.provider.Books/books The following represents a request for a particular book with _id 5: content://net.learn2develop.provider.Books/books/5 Your content provider uses a SQLite database to store the books. Note that you used the SQLiteOpenHelper helper class to help manage your database: private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(“Content provider database”, “Upgrading database from version “ + oldVersion + “ to “ + newVersion + “, which will destroy all old data”); db.execSQL(“DROP TABLE IF EXISTS titles”); onCreate(db); } } Next, you override the getType() method to uniquely describe the data type for your content provider. Using the UriMatcher object, you returned vnd.android.cursor.item/vnd.learn2develop.books for a single book, and vnd.android.cursor.dir/vnd.learn2develop.books for multiple books: @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)){ //---get all books--case BOOKS: c07.indd 311 25/01/12 9:03 AM
  • 312 ❘ CHAPTER 7 CONTENT PROVIDERS return “vnd.android.cursor.dir/vnd.learn2develop.books “; //---get a particular book--case BOOK_ID: return “vnd.android.cursor.item/vnd.learn2develop.books “; default: throw new IllegalArgumentException(“Unsupported URI: “ + uri); } } Next, you overrode the onCreate() method to open a connection to the database when the content provider is started: @Override public boolean onCreate() { Context context = getContext(); DatabaseHelper dbHelper = new DatabaseHelper(context); booksDB = dbHelper.getWritableDatabase(); return (booksDB == null)? false:true; } You overrode the query() method to allow clients to query for books: @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); sqlBuilder.setTables(DATABASE_TABLE); if (uriMatcher.match(uri) == BOOK_ID) //---if getting a particular book--sqlBuilder.appendWhere( _ID + “ = “ + uri.getPathSegments().get(1)); if (sortOrder==null || sortOrder==””) sortOrder = TITLE; Cursor c = sqlBuilder.query( booksDB, projection, selection, selectionArgs, null, null, sortOrder); //---register to watch a content URI for changes--c.setNotificationUri(getContext().getContentResolver(), uri); return c; } By default, the result of the query is sorted using the title field. The resulting query is returned as a Cursor object. c07.indd 312 25/01/12 9:03 AM
  • Creating Your Own Content Providers ❘ 313 To allow a new book to be inserted into the content provider, you override the insert() method: @Override public Uri insert(Uri uri, ContentValues values) { //---add a new book--long rowID = booksDB.insert( DATABASE_TABLE, “”, values); //---if added successfully--if (rowID>0) { Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); getContext().getContentResolver().notifyChange(_uri, null); return _uri; } throw new SQLException(“Failed to insert row into “ + uri); } Once the record is inserted successfully, you call the notifyChange() method of the ContentResolver. This notifies registered observers that a row was updated. To delete a book, you override the delete() method: @Override public int delete(Uri arg0, String arg1, String[] arg2) { // arg0 = uri // arg1 = selection // arg2 = selectionArgs int count=0; switch (uriMatcher.match(arg0)){ case BOOKS: count = booksDB.delete( DATABASE_TABLE, arg1, arg2); break; case BOOK_ID: String id = arg0.getPathSegments().get(1); count = booksDB.delete( DATABASE_TABLE, _ID + “ = “ + id + (!TextUtils.isEmpty(arg1) ? “ AND (“ + arg1 + ‘)’ : “”), arg2); break; default: throw new IllegalArgumentException(“Unknown URI “ + arg0); } getContext().getContentResolver().notifyChange(arg0, null); return count; } c07.indd 313 25/01/12 9:03 AM
  • 314 ❘ CHAPTER 7 CONTENT PROVIDERS Likewise, call the notifyChange() method of the ContentResolver after the deletion. This will notify registered observers that a row was deleted. Finally, to update a book, you override the update() method: @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0; switch (uriMatcher.match(uri)){ case BOOKS: count = booksDB.update( DATABASE_TABLE, values, selection, selectionArgs); break; case BOOK_ID: count = booksDB.update( DATABASE_TABLE, values, _ID + “ = “ + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? “ AND (“ + selection + ‘)’ : “”), selectionArgs); break; default: throw new IllegalArgumentException(“Unknown URI “ + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } As with the insert() and delete() methods, you called the notifyChange() method of the ContentResolver after the update. This notifies registered observers that a row was updated. Finally, to register your content provider with Android, modify the AndroidManifest.xml fi le by adding the <provider> element. USING THE CONTENT PROVIDER Now that you have built your new content provider, you can test it from within your Android application. The following Try It Out demonstrates how to do that. TRY IT OUT 1. Using the Newly Created Content Provider Using the same project created in the previous section, add the following statements in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” c07.indd 314 25/01/12 9:03 AM
  • Using the Content Provider ❘ 315 android:layout_height=”fill_parent” android:orientation=”vertical” > <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”ISBN” /> <EditText android:id=”@+id/txtISBN” android:layout_height=”wrap_content” android:layout_width=”fill_parent” /> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Title” /> <EditText android:id=”@+id/txtTitle” android:layout_height=”wrap_content” android:layout_width=”fill_parent” /> <Button android:text=”Add title” android:id=”@+id/btnAdd” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickAddTitle” /> <Button android:text=”Retrieve titles” android:id=”@+id/btnRetrieve” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”onClickRetrieveTitles” /> </LinearLayout> 2. In the ContentProvidersActivity.java fi le, add the following statements in bold: package net.learn2develop.ContentProviders; import import import import import import import import import android.app.Activity; android.content.ContentValues; android.content.CursorLoader; android.database.Cursor; android.net.Uri; android.os.Bundle; android.view.View; android.widget.EditText; android.widget.Toast; public class ContentProvidersActivity extends Activity { /** Called when the activity is first created. */ c07.indd 315 25/01/12 9:03 AM
  • 316 ❘ CHAPTER 7 CONTENT PROVIDERS @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClickAddTitle(View view) { //---add a book--ContentValues values = new ContentValues(); values.put(BooksProvider.TITLE, ((EditText) findViewById(R.id.txtTitle)).getText().toString()); values.put(BooksProvider.ISBN, ((EditText) findViewById(R.id.txtISBN)).getText().toString()); Uri uri = getContentResolver().insert( BooksProvider.CONTENT_URI, values); Toast.makeText(getBaseContext(),uri.toString(), Toast.LENGTH_LONG).show(); } public void onClickRetrieveTitles(View view) { //---retrieve the titles--Uri allTitles = Uri.parse( “content://net.learn2develop.provider.Books/books”); Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allTitles, null, null, null, “title desc”); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allTitles, null, null, null, “title desc”); c = cursorLoader.loadInBackground(); } if (c.moveToFirst()) { do{ Toast.makeText(this, c.getString(c.getColumnIndex( BooksProvider._ID)) + “, “ + c.getString(c.getColumnIndex( BooksProvider.TITLE)) + “, “ + c.getString(c.getColumnIndex( BooksProvider.ISBN)), Toast.LENGTH_SHORT).show(); } while (c.moveToNext()); } } } c07.indd 316 25/01/12 9:03 AM
  • Using the Content Provider 3. Press F11 to debug the application on the Android emulator. 4. ❘ 317 Enter an ISBN and title for a book and click the Add title button. Figure 7-4 shows the Toast class displaying the URI of the book added to the content provider. To retrieve all the titles stored in the content provider, click the Retrieve titles button and observe the values displayed using the Toast class. How It Works First, you modified the activity so that users can enter a book’s ISBN and title to add to the content provider that you have just created. To add a book to the content provider, you create a new ContentValues object and then populate it with the various information about a book: //---add a book--FIGURE 7-4 ContentValues values = new ContentValues(); values.put(BooksProvider.TITLE, ((EditText) findViewById(R.id.txtTitle)).getText().toString()); values.put(BooksProvider.ISBN, ((EditText) findViewById(R.id.txtISBN)).getText().toString()); Uri uri = getContentResolver().insert( BooksProvider.CONTENT_URI, values); Notice that because your content provider is in the same package, you can use the BooksProvider .TITLE and the BooksProvider.ISBN constants to refer to the “title” and “isbn” fields, respectively. If you were accessing this content provider from another package, then you would not be able to use these constants. In that case, you need to specify the field name directly, like this: ContentValues values = new ContentValues(); values.put(“title”, ((EditText) findViewById(R.id.txtTitle)).getText().toString()); values.put(“isbn”, ((EditText) findViewById(R.id.txtISBN)).getText().toString()); Uri uri = getContentResolver().insert( Uri.parse( “content://net.learn2develop.provider.Books/books”), values); Also note that for external packages, you need to refer to the content URI using the fully qualified content URI: Uri.parse( “content://net.learn2develop.provider.Books/books”), c07.indd 317 25/01/12 9:03 AM
  • 318 ❘ CHAPTER 7 CONTENT PROVIDERS To retrieve all the titles in the content provider, you used the following code snippets: //---retrieve the titles--Uri allTitles = Uri.parse( “content://net.learn2develop.provider.Books/books”); Cursor c; if (android.os.Build.VERSION.SDK_INT <11) { //---before Honeycomb--c = managedQuery(allTitles, null, null, null, “title desc”); } else { //---Honeycomb and later--CursorLoader cursorLoader = new CursorLoader( this, allTitles, null, null, null, “title desc”); c = cursorLoader.loadInBackground(); } if (c.moveToFirst()) { do{ Toast.makeText(this, c.getString(c.getColumnIndex( BooksProvider._ID)) + “, “ + c.getString(c.getColumnIndex( BooksProvider.TITLE)) + “, “ + c.getString(c.getColumnIndex( BooksProvider.ISBN)), Toast.LENGTH_SHORT).show(); } while (c.moveToNext()); } The preceding query will return the result sorted in descending order based on the title field. If you want to update a book’s detail, call the update() method with the content URI, indicating the book’s ID: ContentValues editedValues = new ContentValues(); editedValues.put(BooksProvider.TITLE, “Android Tips and Tricks”); getContentResolver().update( Uri.parse( “content://net.learn2develop.provider.Books/books/2”), editedValues, null, null); To delete a book, use the delete() method with the content URI, indicating the book’s ID: //---delete a title--getContentResolver().delete( Uri.parse(“content://net.learn2develop.provider.Books/books/2”), null, null); c07.indd 318 25/01/12 9:03 AM
  • Summary ❘ 319 To delete all books, simply omit the book’s ID in your content URI: //---delete all titles--getContentResolver().delete( Uri.parse(“content://net.learn2develop.provider.Books/books”), null, null); SUMMARY In this chapter, you learned what content providers are and how to use some of the built-in content providers in Android. In particular, you have seen how to use the Contacts content provider. Google’s decision to provide content providers enables applications to share data through a standard set of programming interfaces. In addition to the built-in content providers, you can also create your own custom content provider to share data with other packages. EXERCISES 1. Write the query to retrieve all contacts from the Contacts application that contain the word “jack.” 2. Name the methods that you need to override in your own implementation of a content provider. 3. How do you register a content provider in your AndroidManifest.xml file? Answers to the exercises can be found in Appendix C. c07.indd 319 25/01/12 9:03 AM
  • 320 ❘ CHAPTER 7 CONTENT PROVIDERS WHAT YOU LEARNED IN THIS CHAPTER TOPIC Retrieving a managed cursor Use the managedQuery() method (for pre-Honeycomb devices) or use the CursorLoader class (for Honeycomb or later devices). Two ways to specify a query for a content provider Use either a query URI or a predefined query string constant. Retrieving the value of a column in a content provider Use the getColumnIndex() method. Query URI for accessing a contact’s name ContactsContract.Contacts.CONTENT_URI Query URI for accessing a contact’s phone number ContactsContract.CommonDataKinds.Phone .CONTENT_URI Creating your own content provider c07.indd 320 KEY CONCEPTS Create a class and extend the ContentProvider class. 25/01/12 9:03 AM
  • 8 Messaging WHAT YOU WILL LEARN IN THIS CHAPTER ➤ How to send SMS messages programmatically from within your application ➤ How to send SMS messages using the built-in Messaging application ➤ How to receive incoming SMS messages ➤ How to send e-mail messages from your application Once your basic Android application is up and running, the next interesting thing you can add to it is the capability to communicate with the outside world. You may want your application to send an SMS message to another phone when an event happens (such as when a particular geographical location is reached), or you may wish to access a web service that provides certain services (such as currency exchange, weather, etc.). In this chapter, you learn how to send and receive SMS messages programmatically from within your Android application. You will also learn how to invoke the Mail application from within your Android application to send e-mail messages to other users. SMS MESSAGING SMS messaging is one of the main killer applications on a mobile phone today — for some users as necessary as the phone itself. Any mobile phone you buy today should have at least SMS messaging capabilities, and nearly all users of any age know how to send and receive such messages. Android comes with a built-in SMS application that enables you to send and receive SMS messages. However, in some cases you might want to integrate SMS capabilities into your c08.indd 321 25/01/12 9:04 AM
  • 322 ❘ CHAPTER 8 MESSAGING own Android application. For example, you might want to write an application that automatically sends an SMS message at regular time intervals. For example, this would be useful if you wanted to track the location of your kids — simply give them an Android device that sends out an SMS message containing its geographical location every 30 minutes. Now you know if they really went to the library after school! (Of course, such a capability also means you would have to pay the fees incurred from sending all those SMS messages…) This section describes how you can programmatically send and receive SMS messages in your Android applications. The good news for Android developers is that you don’t need a real device to test SMS messaging: The free Android emulator provides that capability. Sending SMS Messages Programmatically You will first learn how to send SMS messages programmatically from within your application. Using this approach, your application can automatically send an SMS message to a recipient without user intervention. The following Try It Out shows you how. TRY IT OUT Sending SMS Messages codefile SMS.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it SMS. Replace the TextView with the following statements in bold in the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btnSendSMS” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Send SMS” android:onClick=”onClick” /> </LinearLayout> 3. In the AndroidManifest.xml fi le, add the following statements in bold: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.SMS” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> c08.indd 322 25/01/12 9:04 AM
  • SMS Messaging ❘ 323 <uses-permission android:name=”android.permission.SEND_SMS”/> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.SMSActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> 4. Add the following statements in bold to the SMSActivity.java fi le: package net.learn2develop.SMS; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; public class SMSActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v) { sendSMS(“5556”, “Hello my friends!”); } //---sends an SMS message to another device--private void sendSMS(String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null); } } 5. c08.indd 323 Press F11 to debug the application on the Android emulator. Using the Android SDK and AVD Manager, launch another AVD. 25/01/12 9:04 AM
  • 324 6. ❘ CHAPTER 8 MESSAGING On the fi rst Android emulator (5554), click the Send SMS button to send an SMS message to the second emulator (5556). Figure 8-1 shows the SMS message received by the second emulator (note the notification bar at the top). How It Works Android uses a permissions-based policy whereby all the permissions needed by an application must be specified in the AndroidManifest.xml fi le. This ensures that when the application is installed, the user knows exactly which access permissions it requires. Because sending SMS messages incurs additional costs on the user’s end, indicating the SMS permissions in the AndroidManifest .xml file enables users to decide whether to allow the application to install or not. To send an SMS message programmatically, you use the SmsManager class. Unlike other classes, you do not directly instantiate this class; instead, you call the getDefault() static method to obtain an SmsManager object. You then send the SMS message using the sendTextMessage() method: FIGURE 8-1 //---sends an SMS message to another device--private void sendSMS(String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null); } Following are the five arguments to the sendTextMessage() method: ➤ destinationAddress — Phone number of the recipient ➤ scAddress — Service center address; use null for default SMSC ➤ text — Content of the SMS message ➤ sentIntent — Pending intent to invoke when the message is sent (discussed in more detail in the next section) ➤ deliveryIntent — Pending intent to invoke when the message has been delivered (discussed in more detail in the next section) NOTE If you send an SMS message programmatically using the SmsManager class, the message sent will not appear in the built-in Messaging application of the sender. c08.indd 324 25/01/12 9:04 AM
  • SMS Messaging ❘ 325 Getting Feedback after Sending a Message In the previous section, you learned how to programmatically send SMS messages using the SmsManager class; but how do you know that the message has been sent correctly? To do so, you can create two PendingIntent objects to monitor the status of the SMS message-sending process. These two PendingIntent objects are passed to the last two arguments of the sendTextMessage() method. The following code snippets show how you can monitor the status of the SMS message being sent: package net.learn2develop.SMS; import import import import import import import android.app.Activity; android.app.PendingIntent; android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.content.IntentFilter; android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Toast; public class SMSActivity extends Activity { String SENT = “SMS_SENT”; String DELIVERED = “SMS_DELIVERED”; PendingIntent sentPI, deliveredPI; BroadcastReceiver smsSentReceiver, smsDeliveredReceiver; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); } @Override public void onResume() { super.onResume(); //---create the BroadcastReceiver when the SMS is sent--smsSentReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) c08.indd 325 25/01/12 9:04 AM
  • 326 ❘ CHAPTER 8 MESSAGING { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS sent”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), “Generic failure”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), “No service”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), “Null PDU”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), “Radio off”, Toast.LENGTH_SHORT).show(); break; } } }; //---create the BroadcastReceiver when the SMS is delivered--smsDeliveredReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS delivered”, Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(getBaseContext(), “SMS not delivered”, Toast.LENGTH_SHORT).show(); break; } } }; //---register the two BroadcastReceivers--registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED)); registerReceiver(smsSentReceiver, new IntentFilter(SENT)); } @Override public void onPause() { super.onPause(); //---unregister the two BroadcastReceivers--unregisterReceiver(smsSentReceiver); c08.indd 326 25/01/12 9:04 AM
  • SMS Messaging ❘ 327 unregisterReceiver(smsDeliveredReceiver); } public void onClick(View v) { sendSMS(“5556”, “Hello my friends!”); } //---sends an SMS message to another device--private void sendSMS(String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } } The preceding example created two PendingIntent objects in the onCreate() method: sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); These two PendingIntent objects will be used to send broadcasts later when an SMS message has been sent (“SMS_SENT”) and delivered (“SMS_DELIVERED”). In the onResume() method, you then created and registered two BroadcastReceivers. These two BroadcastReceivers listen for intents that match “SMS_SENT” and “SMS_DELIVERED” (which are fi red by the SmsManager when the message has been sent and delivered, respectively): //---register the two BroadcastReceivers--registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED)); registerReceiver(smsSentReceiver, new IntentFilter(SENT)); Within each BroadcastReceiver you override the onReceive() method and get the current result code. The two PendingIntent objects are passed into the last two arguments of the sendTextMessage() method: SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); In this case, whether a message has been sent correctly or failed to be delivered, you will be notified of its status via the two PendingIntent objects. Finally, in the onPause() method, you unregister the two BroadcastReceivers objects. NOTE If you test the application on the Android emulator, only the sentPI PendingIntent object will be fired, but not the deliveredPI PendingIntent object. On a real device, both PendingIntent objects will fire. c08.indd 327 25/01/12 9:04 AM
  • 328 ❘ CHAPTER 8 MESSAGING Sending SMS Messages Using Intent Using the SmsManager class, you can send SMS messages from within your application without the need to involve the built-in Messaging application. However, sometimes it would be easier if you could simply invoke the built-in Messaging application and let it do all the work of sending the message. To activate the built-in Messaging application from within your application, you can use an Intent object together with the MIME type “vnd.android-dir/mms-sms”, as shown in the following code snippet: Intent i = new Intent(android.content.Intent.ACTION_VIEW); i.putExtra(“address”, “5556; 5558; 5560”); i.putExtra(“sms_body”, “Hello my friends!”); i.setType(“vnd.android-dir/mms-sms”); startActivity(i); This will invoke the Messaging application, as shown in Figure 8-2. Note that you can send your SMS to multiple recipients by simply separating each phone number with a semi-colon (in the putExtra() method). The numbers will be separated using commas in the Messaging application. FIGURE 8-2 c08.indd 328 25/01/12 9:04 AM
  • SMS Messaging ❘ 329 NOTE If you use this method to invoke the Messaging application, there is no need to ask for the SEND_SMS permission in AndroidManifest.xml because your application is ultimately not the one sending the message. Receiving SMS Messages Besides sending SMS messages from your Android applications, you can also receive incoming SMS messages from within your applications by using a BroadcastReceiver object. This is useful when you want your application to perform an action when a certain SMS message is received. For example, you might want to track the location of your phone in case it is lost or stolen. In this case, you can write an application that automatically listens for SMS messages containing some secret code. Once that message is received, you can then send an SMS message containing the location’s coordinates back to the sender. The following Try It Out shows how to programmatically listen for incoming SMS messages. TRY IT OUT 1. Receiving SMS Messages Using the same project created in the previous section, add the following statements in bold to the AndroidManifest.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”net.learn2develop.SMS” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”10” /> <uses-permission android:name=”android.permission.SEND_SMS”/> <uses-permission android:name=”android.permission.RECEIVE_SMS”/> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.SMSActivity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> <receiver android:name=”.SMSReceiver”> <intent-filter> <action android:name= “android.provider.Telephony.SMS_RECEIVED” /> </intent-filter> </receiver> </application> </manifest> c08.indd 329 25/01/12 9:04 AM
  • 330 2. ❘ CHAPTER 8 MESSAGING In the src folder of the project, add a new Class file to the package name and call it SMSReceiver (see Figure 8-3). FIGURE 8-3 3. Code the SMSReceiver.java fi le as follows: package net.learn2develop.SMS; import import import import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.os.Bundle; android.telephony.SmsMessage; android.util.Log; android.widget.Toast; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = “SMS from “; if (bundle != null) c08.indd 330 25/01/12 9:04 AM
  • SMS Messaging ❘ 331 { //---retrieve the SMS message received--Object[] pdus = (Object[]) bundle.get(“pdus”); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); if (i==0) { //---get the sender address/phone number--str += msgs[i].getOriginatingAddress(); str += “: “; } //---get the message body--str += msgs[i].getMessageBody().toString(); } //---display the new SMS message--Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); Log.d(“SMSReceiver”, str); } } } 4. 5. Press F11 to debug the application on the Android emulator. Using the DDMS, send a message to the emulator. Your application should be able to receive the message and display it using the Toast class (see Figure 8-4). FIGURE 8-4 c08.indd 331 25/01/12 9:04 AM
  • 332 ❘ CHAPTER 8 MESSAGING How It Works To listen for incoming SMS messages, you create a BroadcastReceiver class. The BroadcastReceiver class enables your application to receive intents sent by other applications using the sendBroadcast() method. Essentially, it enables your application to handle events raised by other applications. When an intent is received, the onReceive() method is called; hence, you need to override this. When an incoming SMS message is received, the onReceive() method is fi red. The SMS message is contained in the Intent object (intent; the second parameter in the onReceive() method) via a Bundle object. Note that each SMS message received will invoke the onReceive() method. If your device receives five SMS messages, then the onReceive() method will be called five times. Each SMS message is stored in an Object array in the PDU format. If the SMS message is fewer than 160 characters, then the array will have one element. If an SMS message contains more than 160 characters, then the message will be split into multiple smaller messages and stored as multiple elements in the array. To extract the content of each message, you use the static createFromPdu() method from the SmsMessage class. The phone number of the sender is obtained via the getOriginatingAddress() method; therefore, if you need to send an autoreply to the sender, this is the method to obtain the sender’s phone number. To extract the body of the message, you use the getMessageBody() method. One interesting characteristic of the BroadcastReceiver is that your application will continue to listen for incoming SMS messages even if it is not running; as long as the application is installed on the device, any incoming SMS messages will be received by the application. Preventing the Messaging Application from Receiving a Message In the previous section, you may have noticed that every time you send an SMS message to the emulator (or device), both your application and the built-in application receive it. This is because when an SMS message is received, all applications (including the Messaging application) on the Android device take turns handling the incoming message. Sometimes, however, this is not the behavior you want — for example, you might want your application to receive the message and prevent it from being sent to other applications. This is very useful, especially if you are building some kind of tracking application. The solution is very simple. To prevent an incoming message from being handled by the built-in Messaging application, your application just needs to handle the message before the Messaging app has the chance to do so. To do this, add the android:priority attribute to the <intent-filter> element, like this: <receiver android:name=”.SMSReceiver”> <intent-filter android:priority=”100”> <action android:name= “android.provider.Telephony.SMS_RECEIVED” /> </intent-filter> </receiver> Set this attribute to a high number, such as 100. The higher the number, the earlier Android executes your application. When an incoming message is received, your application will execute c08.indd 332 25/01/12 9:04 AM
  • SMS Messaging ❘ 333 fi rst, and you can decide what to do with the message. To prevent other applications from seeing the message, simply call the abortBroadcast() method in your BroadcastReceiver class: @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = “SMS from “; if (bundle != null) { //---retrieve the SMS message received--Object[] pdus = (Object[]) bundle.get(“pdus”); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); if (i==0) { //---get the sender address/phone number--str += msgs[i].getOriginatingAddress(); str += “: “; } //---get the message body--str += msgs[i].getMessageBody().toString(); } //---display the new SMS message--Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); Log.d(“SMSReceiver”, str); //---stop the SMS message from being broadcasted--this.abortBroadcast(); } } Once you do this, no other applications will be able to receive your SMS messages. NOTE Be aware that after the preceding application is installed on your device, all incoming SMS messages will be intercepted by your application and will not appear in your Messaging application ever again. Updating an Activity from a BroadcastReceiver The previous section demonstrated how you can use a BroadcastReceiver class to listen for incoming SMS messages and then use the Toast class to display the received SMS message. Often, you’ll want to send the SMS message back to the main activity of your application. For example, you might wish to display the message in a TextView. The following Try It Out demonstrates how you can do this. c08.indd 333 25/01/12 9:04 AM
  • 334 ❘ CHAPTER 8 MESSAGING TRY IT OUT 1. Creating a View-Based Application Project Using the same project from the previous section, add the following lines in bold to the main.xml fi le: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btnSendSMS” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Send SMS” android:onClick=”onClick” /> <TextView android:id=”@+id/textView1” android:layout_width=”wrap_content” android:layout_height=”wrap_content” /> </LinearLayout> 2. Add the following statements in bold to the SMSReceiver.java fi le: package net.learn2develop.SMS; import import import import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.os.Bundle; android.telephony.SmsMessage; android.util.Log; android.widget.Toast; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = “SMS from “; if (bundle != null) { //---retrieve the SMS message received--Object[] pdus = (Object[]) bundle.get(“pdus”); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); if (i==0) { c08.indd 334 25/01/12 9:04 AM
  • SMS Messaging ❘ 335 //---get the sender address/phone number--str += msgs[i].getOriginatingAddress(); str += “: “; } //---get the message body--str += msgs[i].getMessageBody().toString(); } //---display the new SMS message--Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); Log.d(“SMSReceiver”, str); //---send a broadcast intent to update the SMS received in the activity--Intent broadcastIntent = new Intent(); broadcastIntent.setAction(“SMS_RECEIVED_ACTION”); broadcastIntent.putExtra(“sms”, str); context.sendBroadcast(broadcastIntent); } } } 3. Add the following statements in bold to the SMSActivity.java fi le: package net.learn2develop.SMS; import import import import import import import android.app.Activity; android.app.PendingIntent; android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.content.IntentFilter; android.os.Bundle; import import import import android.telephony.SmsManager; android.view.View; android.widget.TextView; android.widget.Toast; public class SMSActivity extends Activity { String SENT = “SMS_SENT”; String DELIVERED = “SMS_DELIVERED”; PendingIntent sentPI, deliveredPI; BroadcastReceiver smsSentReceiver, smsDeliveredReceiver; IntentFilter intentFilter; private BroadcastReceiver intentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //---display the SMS received in the TextView--TextView SMSes = (TextView) findViewById(R.id.textView1); SMSes.setText(intent.getExtras().getString(“sms”)); } }; /** Called when the activity is first created. */ c08.indd 335 25/01/12 9:04 AM
  • 336 ❘ CHAPTER 8 MESSAGING @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); //---intent to filter for SMS messages received--intentFilter = new IntentFilter(); intentFilter.addAction(“SMS_RECEIVED_ACTION”); } @Override public void onResume() { super.onResume(); //---register the receiver--registerReceiver(intentReceiver, intentFilter); //---create the BroadcastReceiver when the SMS is sent--smsSentReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS sent”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), “Generic failure”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), “No service”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), “Null PDU”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), “Radio off”, Toast.LENGTH_SHORT).show(); break; } } }; //---create the BroadcastReceiver when the SMS is delivered--- c08.indd 336 25/01/12 9:04 AM
  • SMS Messaging ❘ 337 smsDeliveredReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS delivered”, Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(getBaseContext(), “SMS not delivered”, Toast.LENGTH_SHORT).show(); break; } } }; //---register the two BroadcastReceivers--registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED)); registerReceiver(smsSentReceiver, new IntentFilter(SENT)); } @Override public void onPause() { super.onPause(); //---unregister the receiver--unregisterReceiver(intentReceiver); //---unregister the two BroadcastReceivers--unregisterReceiver(smsSentReceiver); unregisterReceiver(smsDeliveredReceiver); } public void onClick(View v) { sendSMS(“5556”, “Hello my friends!”); } public void onSMSIntentClick (View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW); i.putExtra(“address”, “5556; 5558; 5560”); i.putExtra(“sms_body”, “Hello my friends!”); i.setType(“vnd.android-dir/mms-sms”); startActivity(i); } //—-sends an SMS message to another device—private void sendSMS(String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } } c08.indd 337 25/01/12 9:04 AM
  • 338 4. ❘ CHAPTER 8 MESSAGING Press F11 to debug the application on the Android emulator. Using the DDMS, send an SMS message to the emulator. Figure 8-5 shows the Toast class displaying the message received, and the TextView showing the message received. FIGURE 8-5 How It Works You fi rst added a TextView to your activity so that it can be used to display the received SMS message. Next, you modified the SMSReceiver class so that when it receives an SMS message, it broadcasts another Intent object so that any applications listening for this intent can be notified (which you will implement in the activity next). The SMS received is also sent out via this intent: //---send a broadcast intent to update the SMS received in the activity--Intent broadcastIntent = new Intent(); broadcastIntent.setAction(“SMS_RECEIVED_ACTION”); broadcastIntent.putExtra(“sms”, str); context.sendBroadcast(broadcastIntent); c08.indd 338 25/01/12 9:04 AM
  • SMS Messaging ❘ 339 Next, in your activity you created a BroadcastReceiver object to listen for broadcast intents: private BroadcastReceiver intentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //---display the SMS received in the TextView--TextView SMSes = (TextView) findViewById(R.id.textView1); SMSes.setText(intent.getExtras().getString(“sms”)); } }; When a broadcast intent is received, you update the SMS message in the TextView. You need to create an IntentFilter object so that you can listen for a particular intent. In this case, the intent is “SMS_RECEIVED_ACTION”: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); //---intent to filter for SMS messages received--intentFilter = new IntentFilter(); intentFilter.addAction(“SMS_RECEIVED_ACTION”); } Finally, you register the BroadcastReceiver in the activity’s onResume() event and unregister it in the onPause() event: @Override protected void onResume() { //—-register the receiver—registerReceiver(intentReceiver, intentFilter); super.onResume(); } @Override protected void onPause() { //—-unregister the receiver—unregisterReceiver(intentReceiver); super.onPause(); } @Override public void onResume() { super.onResume(); //---register the receiver--- c08.indd 339 25/01/12 9:04 AM
  • 340 ❘ CHAPTER 8 MESSAGING registerReceiver(intentReceiver, intentFilter); //---create the BroadcastReceiver when the SMS is sent--//... } @Override public void onPause() { super.onPause(); //---unregister the receiver--unregisterReceiver(intentReceiver); //---unregister the two BroadcastReceivers--//... } This means that the TextView will display the SMS message only when the message is received while the activity is visible on the screen. If the SMS message is received when the activity is not in the foreground, the TextView will not be updated. Invoking an Activity from a BroadcastReceiver The previous example shows how you can pass the SMS message received to be displayed in the activity. However, in many situations your activity may be in the background when the SMS message is received. In this case, it would be useful to be able to bring the activity to the foreground when a message is received. The following Try It Out shows you how. TRY IT OUT 1. Invoking an Activity Using the same project used in the previous section, add the following lines in bold to the SMSActivity.java fi le: /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); //---intent to filter for SMS messages received--intentFilter = new IntentFilter(); intentFilter.addAction(“SMS_RECEIVED_ACTION”); //---register the receiver--- c08.indd 340 25/01/12 9:04 AM
  • SMS Messaging ❘ 341 registerReceiver(intentReceiver, intentFilter); } @Override public void onResume() { super.onResume(); //---register the receiver--//registerReceiver(intentReceiver, intentFilter); //---create the BroadcastReceiver when the SMS is sent--smsSentReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS sent”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), “Generic failure”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), “No service”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), “Null PDU”, Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), “Radio off”, Toast.LENGTH_SHORT).show(); break; } } }; //---create the BroadcastReceiver when the SMS is delivered--smsDeliveredReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS delivered”, Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(getBaseContext(), “SMS not delivered”, Toast.LENGTH_SHORT).show(); break; c08.indd 341 25/01/12 9:04 AM
  • 342 ❘ CHAPTER 8 MESSAGING } } }; //---register the two BroadcastReceivers--registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED)); registerReceiver(smsSentReceiver, new IntentFilter(SENT)); } @Override public void onPause() { super.onPause(); //---unregister the receiver--//unregisterReceiver(intentReceiver); //---unregister the two BroadcastReceivers--unregisterReceiver(smsSentReceiver); unregisterReceiver(smsDeliveredReceiver); } @Override protected void onDestroy() { super.onDestroy(); //---unregister the receiver--unregisterReceiver(intentReceiver); } 2. Add the following statements in bold to the SMSReceiver.java fi le: @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = “SMS from “; if (bundle != null) { //---retrieve the SMS message received--Object[] pdus = (Object[]) bundle.get(“pdus”); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); if (i==0) { //---get the sender address/phone number--str += msgs[i].getOriginatingAddress(); str += “: “; } //---get the message body--str += msgs[i].getMessageBody().toString(); } //---display the new SMS message--- c08.indd 342 25/01/12 9:04 AM
  • ❘ 343 SMS Messaging Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); Log.d(“SMSReceiver”, str); //---launch the SMSActivity--Intent mainActivityIntent = new Intent(context, SMSActivity.class); mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mainActivityIntent); //---send a broadcast intent to update the SMS received in the activity--Intent broadcastIntent = new Intent(); broadcastIntent.setAction(“SMS_RECEIVED_ACTION”); broadcastIntent.putExtra(“sms”, str); context.sendBroadcast(broadcastIntent); } } 3. Modify the AndroidManifest.xml fi le as follows: <activity android:label=”@string/app_name” android:name=”.SMSActivity” android:launchMode=”singleTask” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> 4. Press F11 to debug the application on the Android emulator. When the SMSActivity is shown, click the Home button to send the activity to the background. 5. Use the DDMS to send an SMS message to the emulator again. This time, note that the activity will be brought to the foreground, displaying the SMS message received. How It Works In the SMSActivity class, you fi rst registered the BroadcastReceiver in the activity’s onCreate() event, instead of the onResume() event; and instead of unregistering it in the onPause() event, you unregister it in the onDestroy() event. This ensures that even if the activity is in the background, it will still be able to listen for the broadcast intent. Next, you modified the onReceive() event in the SMSReceiver class by using an intent to bring the activity to the foreground before broadcasting another intent: //---launch the SMSActivity--Intent mainActivityIntent = new Intent(context, SMSActivity.class); mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mainActivityIntent); //---send a broadcast intent to update the SMS received in the activity--Intent broadcastIntent = new Intent(); broadcastIntent.setAction(“SMS_RECEIVED_ACTION”); broadcastIntent.putExtra(“sms”, str); context.sendBroadcast(broadcastIntent); c08.indd 343 25/01/12 9:04 AM
  • 344 ❘ CHAPTER 8 MESSAGING The startActivity() method launches the activity and brings it to the foreground. Note that you needed to set the Intent.FLAG_ACTIVITY_NEW_TASK flag because calling startActivity() from outside of an activity context requires the FLAG_ACTIVITY_NEW_TASK flag. You also needed to set the launchMode attribute of the <activity> element in the AndroidManifest .xml fi le to singleTask: <activity android:label=”@string/app_name” android:name=”.SMSActivity” android:launchMode=”singleTask” > If you don’t set this, multiple instances of the activity will be launched as your application receives SMS messages. Note that in this example, when the activity is in the background (such as when you click the Home button to show the home screen), the activity is brought to the foreground and its TextView is updated with the SMS received. However, if the activity was killed (such as when you click the Back button to destroy it), the activity is launched again but the TextView is not updated. Caveats and Warnings While the capability to send and receive SMS messages makes Android a very compelling platform for developing sophisticated applications, this flexibility comes with a price. A seemingly innocent application may send SMS messages behind the scene without the user knowing, as demonstrated by a recent case of an SMS-based Trojan Android application (see http://forum.vodafone.co.nz/ topic/5719-android-sms-trojan-warning/). Claiming to be a media player, once installed, the application sends SMS messages to a premium-rate number, resulting in huge phone bills for the user. While the user needs to explicitly give permissions (such as accessing the Internet, sending and receiving SMS messages, etc.) to your application, the request for permissions is shown only at installation time. If the user clicks the Install button, he or she is considered to have granted the application permission to send and receive SMS messages. This is dangerous, as after the application is installed it can send and receive SMS messages without ever prompting the user again. In addition to this, the application can also “sniff” for incoming SMS messages. For example, based on the techniques you learned from the previous section, you can easily write an application that checks for certain keywords in the SMS message. When an SMS message contains the keyword you are looking for, you can then use the Location Manager (discussed in Chapter 9) to obtain your geographical location and then send the coordinates back to the sender of the SMS message. The sender could then easily track your location. All these tasks can be done easily without the user knowing it! That said, users should try to avoid installing Android applications that come from dubious sources, such as from unknown websites or strangers. c08.indd 344 25/01/12 9:04 AM
  • ❘ 345 Sending E-mail SENDING E-MAIL Like SMS messaging, Android also supports e-mail. The Gmail/Email application on Android enables you to configure an e-mail account using POP3 or IMAP. Besides sending and receiving e-mails using the Gmail/Email application, you can also send e-mail messages programmatically from within your Android application. The following Try It Out shows you how. TRY IT OUT Sending E-mail Programmatically codefile Emails.zip available for download at Wrox.com 1. 2. Using Eclipse, create a new Android project and name it Emails. Add the following statements in bold to the main.xml fi le, replacing the TextView: <?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <Button android:id=”@+id/btnSendEmail” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Send Email” android:onClick=”onClick” /> </LinearLayout> 3. Add the following statements in bold to the SMSActivity.java fi le: package net.learn2develop.Emails; import import import import import android.app.Activity; android.content.Intent; android.net.Uri; android.os.Bundle; android.view.View; public class EmailsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v) { //---replace the following email addresses with real ones--String[] to = c08.indd 345 25/01/12 9:04 AM
  • 346 ❘ CHAPTER 8 MESSAGING {“someguy@example.com”, “anotherguy@example.com”}; String[] cc = {“busybody@example.com”}; sendEmail(to, cc, “Hello”, “Hello my friends!”); } //---sends an SMS message to another device--private void sendEmail(String[] emailAddresses, String[] carbonCopies, String subject, String message) { Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setData(Uri.parse(“mailto:”)); String[] to = emailAddresses; String[] cc = carbonCopies; emailIntent.putExtra(Intent.EXTRA_EMAIL, to); emailIntent.putExtra(Intent.EXTRA_CC, cc); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, message); emailIntent.setType(“message/rfc822”); startActivity(Intent.createChooser(emailIntent, “Email”)); } } 4. Press F11 to test the application on the Android emulator/device (ensure that you have configured your e-mail before trying this example). Click the Send Email button and you should see the Email application launched in your emulator/device, as shown in Figure 8-6. FIGURE 8-6 c08.indd 346 25/01/12 9:04 AM
  • ❘ 347 Summary AN E-MAIL TIP FOR TESTING PURPOSES If you are a gmail user, for testing purposes you can take advantage of the fact that you can append the plus sign (+) and any string to the user name of your account. You will continue to receive the e-mail as usual, but it can be used for fi ltering. For example, my Gmail account is weimenglee@gmail.com. When I am doing testing I will configure the recipient address as “weimenglee+android_book@gmail.com” and then I can easily fi lter out those messages for deletion later. In addition, I can also add periods to my Gmail address. For example, if you want to test sending an e-mail to a couple of people, you can send to weimeng.lee@gmail.com, wei.meng .lee@gmail.com, and wei.menglee@gmail.com; an e-mail to all these e-mail addresses will end up in my weimenglee@gmail.com account as a single message. This is very useful for testing! Finally, take a look at http://smtp4dev.codeplex.com, which contains a dummy SMTP server that allows you to debug e-mail messages. How It Works In this example, you are launching the built-in Email application to send an e-mail message. To do so, you use an Intent object and set the various parameters using the setData(), putExtra(), and setType() methods: Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setData(Uri.parse(“mailto:”)); String[] to = emailAddresses; String[] cc = carbonCopies; emailIntent.putExtra(Intent.EXTRA_EMAIL, to); emailIntent.putExtra(Intent.EXTRA_CC, cc); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, message); emailIntent.setType(“message/rfc822”); startActivity(Intent.createChooser(emailIntent, “Email”)); SUMMARY This chapter described the two key ways for your application to communicate with the outside world. You fi rst learned how to send and receive SMS messages. Using SMS, you can build a wide variety of applications that rely on the service provided by your mobile operator. Chapter 9 shows you a good example of how to use SMS messaging to build a location tracker application. You also learned how to send e-mail messages from within your Android application. You do that by invoking the built-in Email application through the use of an Intent object. c08.indd 347 25/01/12 9:04 AM
  • 348 ❘ CHAPTER 8 MESSAGING EXERCISES 1. Name the two ways in which you can send SMS messages in your Android application. 2. Name the permissions you need to declare in your AndroidManifest.xml file for sending and receiving SMS messages. 3. How do you notify an activity from a BroadcastReceiver? Answers to the exercises can be found in Appendix C. c08.indd 348 25/01/12 9:04 AM
  • Summary ❘ 349 WHAT YOU LEARNED IN THIS CHAPTER TOPIC Programmatically sending SMS messages Use the SmsManager class. Getting feedback on messages sent Use two PendingIntent objects in the sendTextMessage() method. Sending SMS messages using Intent Set the intent type to vnd.android-dir/mms-sms. Receiving SMS messages Implement a BroadcastReceiver and set it in the AndroidManifest.xml file. Sending e-mail using Intent c08.indd 349 KEY CONCEPTS Set the intent type to message/rfc822. 25/01/12 9:04 AM
  • c08.indd 350 25/01/12 9:04 AM
  • 9 Location-Based Services WHAT YOU WILL LEARN IN THIS CHAPTER ➤ Displaying Google Maps in your Android application ➤ Displaying zoom controls on the map ➤ Switching between the different map views ➤ Adding markers to maps ➤ How to get the address location touched on the map ➤ Performing geocoding and reverse geocoding ➤ Obtaining geographical data using GPS, Cell-ID, and Wi-Fi triangulation ➤ How to monitor for a location ➤ Building a Location Tracker application You have all seen the explosive growth of mobile apps in recent years. One category of apps that is very popular is location-based services, commonly known as LBS. LBS apps track your location, and may offer additional services such as locating amenities nearby, as well as offer suggestions for route planning, and so on. Of course, one of the key ingredients in an LBS app is maps, which present a visual representation of your location. In this chapter, you will learn how to make use of Google Maps in your Android application, and how to manipulate it programmatically. In addition, you will learn how to obtain your geographical location using the LocationManager class available in the Android SDK. This chapter ends with a project to build a Location Tracker application that you can install on an Android device and can use to track the location of friends and relatives using SMS messaging. c09.indd 351 25/01/12 9:29 AM
  • 352 ❘ CHAPTER 9 LOCATION-BASED SERVICES DISPLAYING MAPS Google Maps is one of the many applications bundled with the Android platform. In addition to simply using the Maps application, you can also embed it into your own applications and make it do some very cool things. This section describes how to use Google Maps in your Android applications and programmatically perform the following: ➤ Change the views of Google Maps. ➤ Obtain the latitude and longitude of locations in Google Maps. ➤ Perform geocoding and reverse geocoding (translating an address to latitude and longitude and vice versa). ➤ Add markers to Google Maps. Creating the Project To get started, you need to first create an Android project so that you can display Google Maps in your activity. TRY IT OUT Creating the Google APIs Project codefile LBS.zip available for download at Wrox.com 1. Using Eclipse