Android Cookbook.Pdf

Total Page:16

File Type:pdf, Size:1020Kb

Android Cookbook.Pdf Android Cookbook Android Cookbook Android Community Experts Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Android Cookbook by Android Community Experts Copyright © 2011 Ian Darwin and Contributors. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or [email protected]. Editor: Ian F. Darwin Indexer: Production Editor: Cover Designer: Copyeditor: Interior Designer: Proofreader: Illustrators: and November 2011: First Edition. Revision History for the First Edition: See http://oreilly.com/catalog/errata.csp?isbn=9781449388416 for release details. Android is a trademark of Google, Inc. for their open-source operating environment for mobile devices. Linux is a trademark of Linus Torvalds. Java is a trademark of Oracle America Corporation (formerly Sun Microsystems). Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. !!FILL THIS IN!! and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations uses by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-38841-6 [?] 1318018084 Table of Contents Preface .................................................................... xiii 1. Getting Started ......................................................... 1 1.1 Introduction: Getting Started 1 1.2 Learning the Java Language 1 1.3 Hello World - Command Line 3 1.4 Hello World - Eclipse Version 5 1.5 Set Up an Android Virtual Device for Apps Testing 10 1.6 Set Up an IDE on Windows to Develop for Android 23 1.7 Android Lifecycle 32 1.8 Opening a Web Page, Phone Number or anything else with an Intent 33 1.9 Email Text From a View 35 1.10 Sending an email with attachments 38 1.11 Installing .apk files on the emulator 40 1.12 Installing apps onto an Android Emulator 40 1.13 Android Epoch HTML/Javascript Calendar 43 1.14 Sharing Java classes from another Eclipse Project 48 1.15 Referencing libraries to implement external functionality 49 1.16 Use SDK Samples to Help Avoid Head Scratching 50 1.17 Keeping the Android SDK Updated 55 1.18 Five Ways to Wire Up an Event Listener 61 1.19 Taking a Screenshot from the Emulator/Android Device 69 1.20 Program: A Simple CountDownTimer example 70 1.21 Program: Tipster, a tip calculator for the Android OS 73 2. Designing a successful Application ........................................ 91 2.1 Introduction: Designing a Successful Android application 91 2.2 Keeping a Service running while other apps are on display 95 2.3 Starting a service after phone reboot 97 2.4 Exception Handling 98 2.5 Sending/Receive broadcast message 100 v 2.6 Android's Application Object as a "Singleton" 101 2.7 Keeping data when the user rotates the device 103 2.8 Creating a Responsive Application using Threads 105 2.9 Eating Too Much CPU Time In The UI Causes A Nasty Result 107 2.10 AsyncTask: Do background tasks and modify the GUI 113 2.11 Monitoring the Battery Level of your Android Device 114 2.12 Splash Screens in Android: Method 1 115 2.13 Splash Screens in Android: Method 2 117 2.14 Designing a Conference/*Camp/Hackathon App 123 2.15 Implementing Autocompletion in Android. 124 2.16 Using Google Analytics in Android Application 126 2.17 Using AsyncTask to do background processing 128 2.18 A Simple Torch Light 134 2.19 Adapting Android Phone Application to Tablet 136 2.20 First Run preferences 138 2.21 Formatting the time and date display 139 2.22 Controlling Input with KeyListeners 142 2.23 Android Application Data Backup 145 2.24 Making password fields 151 2.25 Working Without Tooltips: Use Hints Instead 152 3. Testing .............................................................. 157 3.1 Introduction: Testing 157 3.2 How to TDD(test driven development) Android App 157 3.3 How to troubleshoot "The application has stopped unexpectedly. Please try again" 158 3.4 Debugging using Log.d and LogCat 161 3.5 Keep Your App Snappy With StrictMode 163 3.6 Barrel of Monkeys 164 3.7 Sending text messages and placing calls between AVDs 165 3.8 Activity LifeCycle Scenarios for Testing 166 4. Content Providers ..................................................... 175 4.1 Introduction: Content Providers 175 4.2 Retrieving Data from a Content Provider 175 4.3 Writing a Content Provider 177 4.4 Android Remote Service 179 5. Graphics ............................................................. 185 5.1 Introduction: Graphics 185 5.2 Getting Screenshots 185 5.3 Using a Custom Font 186 5.4 Draw a spinning cube with OpenGL ES 191 vi | Table of Contents 5.5 Adding control to the OpenGL spinning cube 196 5.6 Taking a Picture Using an Intent 199 5.7 Taking a Picture Using android.media.Camera 201 5.8 Using AndroidPlot to display charts and graphs in your Android application. 205 5.9 Use Inkscape to Create an Android Launcher Icon 207 5.10 Easy Launcher Icons from OpenClipArt.org using Paint.NET 216 5.11 Android HTML5 RGraph Charting 228 5.12 Simple Raster Animation 232 6. Graphical User Interface ............................................... 239 6.1 Introduction: GUI 239 6.2 User Interface Guidelines (placeholder) 240 6.3 SlidingDrawer Overlapping other UI components 240 6.4 Android 3.0 Photo Gallery 244 6.5 Building a UI using Fragments API of Android 3.0 in Android 2.2 246 6.6 Haptic Feedback 250 6.7 Handling Configuration Changes by Decoupling View from Model 254 6.8 Let Them See Stars: Using RatingBar 257 6.9 Invoke an action handler when a Button is pressed 260 6.10 Creating an Alert Dialog. 263 6.11 Customize the SlidingDrawer component to animate/transition from the top down. 264 6.12 Use a Timepicker widget 266 6.13 Formatting with Correct Plurals 268 6.14 Feed AutoCompleteTextView using a SQLite database query 272 6.15 Change The Enter Key to "Next" on the Soft Keyboard 273 6.16 How to Create a Simple Widget 277 6.17 Make a View Shake 280 6.18 Using CheckBoxes and RadioButtons 281 6.19 Creating a Notification in the Status Bar 286 6.20 Autocompletion with Icons/Images 288 6.21 Creating your own Custom Title Bar 295 6.22 iPhone-like wheel picker for selection 298 6.23 Simple Calendar 302 6.24 Formatting Numbers 310 6.25 Start a Second Screen from the First 314 6.26 Creating a Tabbed Dialog 322 6.27 Creating a Custom Dialog with buttons, images and text 326 6.28 Create a Custom Menu 328 6.29 Loading Screen in between two Activities 330 6.30 Implementing reactions on click of items in a Custom Menu. 333 6.31 Navigate different activities within a TabView 336 Table of Contents | vii 6.32 Drop-down Chooser via the Spinner Class 338 6.33 Effective UI design using Image Buttons 340 6.34 Pinch to zoom 343 6.35 Add a Border with Rounded Corners to a Layout 346 6.36 Creating a ProgressDialog in Android. 347 6.37 Creating a Submenu. 349 6.38 Processing key press events in an Activity. 351 6.39 Constrain EditText Values with Attributes and the TextWatcher Interface 352 6.40 Gesture Detection in Android 355 6.41 Customizing the Look of a Toast 362 6.42 Using SlidingDrawer to Overlap Other Components 363 7. GUI: ListView ......................................................... 367 7.1 Introduction: ListView 367 7.2 Building list-based applications with ListView 367 7.3 'No data' View for Lists 372 7.4 Advanced ListView: populating a list with images and text 373 7.5 ListView with Icons/images 379 7.6 Sectioned Headers in ListViews 386 7.7 Making Lists Behave Nicely 392 7.8 Writing A Custom List Adapter 393 7.9 Orientation Changes : From ListView data values to Landscape Charting 396 8. Multimedia .......................................................... 407 8.1 Introduction: Multimedia 407 8.2 Play a Youtube Video 407 8.3 Using Gallery with ImageSwitcher 408 8.4 Grabbing a video using MediaRecorder 411 8.5 Android Face Detection 414 8.6 Playing audio from a file 417 8.7 Playing Audio without Interaction 420 8.8 Using Speech to Text 421 8.9 Making the Device Speak with TTS 423 9. Data Persistence ...................................................... 427 9.1 Listing a Directory 427 9.2 Default shared preferences consistency check 429 9.3 Advanced text search 431 9.4 How to push string-values using Intent.putExtra() 437 9.5 Retrieving data from a Sub-Activity back to your Main Activity 439 9.6 Getting total and free space on the SD card 442 viii | Table of Contents 9.7 Creating a SQLite database in an Android application. 442 9.8 Retrieving data from a SQLite database. 444 9.9 Inserting values into a SQLite database. 445 9.10 Work With Dates in SQLite 445 9.11 Parsing JSON using the Jackson Parser 448 9.12 Parsing an XML document using the DOM API 451 9.13 Parsing an XML document using an XmlPullParser 453 9.14 Accessing data from a file shipped with the App rather than in the filesystem 456 9.15 Adding a Contact 457 9.16 Reading Contact Data 461 9.17 Parsing JSON using JSONObject 463 10.
Recommended publications
  • Javascript (HTML5, CSS3) Toolkits for Infovis (Graphics)
    JavaScript (HTML5, CSS3) Toolkits for InfoVis (Graphics) Group 2 Amir Kanuric, Raoul Rubien, Jorg¨ Schlager 706.057 Information Visualisation SS 2012 Graz University of Technology 2 May 2012 Abstract Graphical data visualization is used to display data in a comprehensive way in form of various types of graphs. There are a number of technologies used for graph generation. In this survey reader can get an overview over some most popular JavaScript information visualization libraries that are available today. Such libraries sim- plify the development process of custom visualization tools or provide ready to use functionalities which can be used for rendering of basic types of graphs like: Pie charts, Line charts, Area charts or to render special graphs like: Sunburst (with Highcharts), Gauges (with RGraph), Stepped plots (with Flot) or BoxPlot (with Sparklines). The libraries listed below use JavaScript, HTML5, CSS, CSS3, canvas, SVG, and VML as tech- nique for rendering. The purpose of information visualization is to amplify cognitive performance, not just to create interesting pictures. Information visualizations should do for the mind what automobiles do for the feet. Contents 1 Motivation 1 1.1 Motivation . .1 2 Low Level Graphics Toolkits3 2.1 mxGraph . .3 2.1.1 What is mxGraph? . .3 2.1.2 How to begin using mxGraph: Hello World example . .3 2.1.3 Technology . .4 2.1.4 Documentation and examples . .4 2.2 Raphael..............................................¨ 4 2.2.1 Technology . .5 2.2.2 Browser Compatibility . .5 2.2.3 Usage Examples . .5 2.3 Comparison of Low-Level Toolkits . .6 3 Chart Toolkits 9 3.1 Highcharts .
    [Show full text]
  • 2D Information Visualization SVG and HTML5
    2D Information Visualization SVG and HTML5 Version of 20 May 2011 Christian Kantner Albert Leimuller¨ Evgenia Popova Michael Steurer Abstract There are many different solutions and technologies to create a web-based 2D information visualization. The most common are Java Applets, JavaScript and Ajax, Flash, SVG and since 2008 HTML5 with the Canvas element. In this paper a brief overview of these technologies and short introduction by example in SVG and HTML5 Canvas architectures is conducted. A few libraries which enable the use of high quality 2D information visualizations like Protovis, RGrapgh, Google Chart Tool and the Jit is demonstrated in this work and finally a performance benchmarking of the introduced technologies which allows to judge upon technologies’ potential. Contents 1 Introduction 1 1.1 HTML . .1 1.1.1 Idea . .1 1.1.2 Markup languages . .1 1.1.3 JavaScript . .2 1.2 Java Applet, Adobe Flash . .2 1.3 JSON . .3 1.4 Overview 2D Graphics . .3 1.4.1 SVG . .3 1.4.2 HTML5 Canvas Element . .3 2 Scalable Vector Graphics5 2.1 About SVG . .5 2.2 History of SVG . .5 2.3 SVG and HTML5 . .5 2.4 Embedding SVG in HTML5 documents . .6 2.5 SVG Elements and Functions . .6 2.6 Styling, Scripting and Animations with SVG using CSS and JavaScript . .7 2.7 The Google Chart Tools . .8 2.8 SVG in Action: Protovis . 10 3 HTML5 13 3.1 An Introduction to the Standard . 13 3.2 Feature Space . 13 3.3 Canvas HTML5 API . 14 3.3.1 The Canvas element .
    [Show full text]
  • Android Cookbook
    Android Cookbook wnload from Wow! eBook <www.wowebook.com> o D Ian F. Darwin Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Android Cookbook by Ian F. Darwin Copyright © 2012 O’Reilly Media, Inc.. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editors: Mike Loukides and Courtney Nash Indexer: Lucie Haskins Production Editor: Teresa Elsey Cover Designer: Karen Montgomery Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreader: Stacie Arellano Illustrators: Robert Romano and Rebecca Demarest April 2012: First Edition. Revision History for the First Edition: 2012-04-05 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449388416 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Android Cookbook, the image of a marine iguana, and related trade dress are trade- marks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein.
    [Show full text]
  • The Missing Manual by Matthew Macdonald
    HTML5 The book that should have been in the box® Matthew MacDonald Beijing | Cambridge | Farnham | Köln | Sebastopol | Tokyo HTML5: The Missing Manual by Matthew MacDonald Copyright © 2011 Matthew MacDonald. All rights reserved. Printed in the Unites States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles: http://my.safaribooksonline.com. For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Printing History: August 2011: First Edition. Nutshell Handbook, the Nutshell Handbook logo, the O’Reilly logo, and “The book that should have been in the box” are registered trademarks of O’Reilly Media, Inc. HTML5: The Missing Manual, The Missing Manual logo, Pogue Press, and the Pogue Press logo are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-30239-9 [LSI] [2013-06-28] Table of Contents The Missing Credits .............................. xi Introduction ................................... 1 Part One: Meet the New Language Chapter 1: Introducing HTML5 ....................... 11 The Story of HTML5 ......................................
    [Show full text]