Apress Beginning Android 3 (2011).pdf

(14218 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Beginning Android 3
t
■ ■ ■
Mark Murphy
850636238.011.png 850636238.012.png 850636238.013.png 850636238.014.png 850636238.001.png 850636238.002.png 850636238.003.png 850636238.004.png 850636238.005.png 850636238.006.png 850636238.007.png 850636238.008.png 850636238.009.png 850636238.010.png
Beginning Android 3
Copyright © 2011 by Mark Murphy
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information
storage or retrieval system, without the prior written permission of the copyright owner and the
publisher.
ISBN-13 (pbk): 978-1-4302-3297-1
ISBN-13 (electronic): 978-1-4302-3298-8
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
President and Publisher: Paul Manning
Lead Editor: Steve Anglin
Development Editor: Tom Welsh
Technical Reviewer: Dylan Philips
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan
Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, James Markham,
Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke,
Dominic Shakeshaft, Matt Wade, Tom Welsh
Coordinating Editor: Jessica Belanger
Copy Editor: William McManus
Compositor: MacPS, LLC
Indexer: John Collin
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring
Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
orders-ny@springer-sbm.com , or visit www.springeronline.com .
For information on translations, please e-mail rights@apress.com , or visit www.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or
promotional use. eBook versions and licenses are also available for most titles. For more
information, reference our Special Bulk Sales–eBook Licensing web page at
www.apress.com/bulk-sales .
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com .
Contents
Part I: Core Concept............................................................................................ 1
Chapter 1: The Big Picture............................................................................... 3
Benefits and Drawbacks of Smartphone Programming.......................................................................................... 3
What Androids Are Made Of.................................................................................................................................... 4
Stuff at Your Disposal ............................................................................................................................................. 5
The Big Picture...of This Book................................................................................................................................. 6
Chapter 2: How to Get Started ......................................................................... 7
Step 1: Set Up Java................................................................................................................................................. 7
Install the JDK.................................................................................................................................................... 7
Learn Java ......................................................................................................................................................... 8
Step 2: Install the Android SDK ............................................................................................................................... 8
Install the Base Tools......................................................................................................................................... 8
Install the SDKs and Add-ons ............................................................................................................................ 9
Step 3: Install the ADT for Eclipse......................................................................................................................... 12
Step 4: Install Apache Ant..................................................................................................................................... 14
Step 5: Set Up the Emulator.................................................................................................................................. 15
Step 6: Set Up the Device ..................................................................................................................................... 21
Windows .......................................................................................................................................................... 21
Mac OS X and Linux ......................................................................................................................................... 22
Chapter 3: Your First Android Project ........................................................... 23
Step 1: Create the New Project............................................................................................................................. 23
Eclipse ............................................................................................................................................................. 23
Command Line ................................................................................................................................................. 26
Step 2: Build, Install, and Run the Application in Your Emulator or Device .......................................................... 27
Eclipse ................................................................................................................................ ............................. 27
Command Line ................................................................................................................................................. 28
Chapter 4: Examining Your First Project ....................................................... 31
Project Structure................................................................................................................................................... 31
Root Contents .................................................................................................................................................. 31
The Sweat Off Your Brow................................................................................................................................. 32
And Now, the Rest of the Story........................................................................................................................ 32
What You Get Out of It...................................................................................................................................... 33
Inside Your Manifest ............................................................................................................................................. 33
In the Beginning, There Was the Root, and It Was Good.................................................................................. 34
An Application for Your Application ................................................................................................................. 35
Chapter 5: A Bit About Eclipse....................................................................... 37
What the ADT Gives You........................................................................................................................................ 37
Coping with Eclipse .............................................................................................................................................. 38
How to Import a Non-Eclipse Project ............................................................................................................... 38
How to Get to DDMS ........................................................................................................................................ 42
How to Create an Emulator .............................................................................................................................. 43
How to Run a Project ....................................................................................................................................... 44
How Not to Run Your Project ........................................................................................................................... 45
Alternative IDEs..................................................................................................................................................... 45
IDEs and This Book ............................................................................................................................................... 46
Chapter 6: Enhancing Your First Project ....................................................... 47
Supporting Multiple Screen Sizes......................................................................................................................... 47
Specifying Versions .............................................................................................................................................. 48
Part II: Activities ............................................................................................... 49
Chapter 7: Rewriting Your First Project ........................................................ 51
The Activity ........................................................................................................................................................... 51
Dissecting the Activity .......................................................................................................................................... 52
Building and Running the Activity ......................................................................................................................... 53
Chapter 8: Using XML-Based Layouts ........................................................... 55
What Is an XML-Based Layout? ............................................................................................................................ 55
Why Use XML-Based Layouts? ............................................................................................................................. 55
OK, So What Does It Look Like? ............................................................................................................................ 56
What’s with the @ Signs?..................................................................................................................................... 57
And How Do We Attach These to the Java? .......................................................................................................... 57
The Rest of the Story ............................................................................................................................................ 58
Chapter 9: Employing Basic Widgets............................................................. 61
Assigning Labels ................................................................................................................................................... 61
Button, Button, Who’s Got the Button? ................................................................................................................. 62
Fleeting Images .................................................................................................................................................... 63
Fields of Green…or Other Colors.......................................................................................................................... 64
Just Another Box to Check.................................................................................................................................... 66
Turn the Radio Up ................................................................................................................................................. 68
It’s Quite a View .................................................................................................................................................... 70
Padding ............................................................................................................................................................ 70
Other Useful Properties.................................................................................................................................... 71
Useful Methods ................................................................................................................................................ 71
Colors ............................................................................................................................................................... 71
Chapter 10: Working with Containers ........................................................... 73
Thinking Linearly .................................................................................................................................................. 73
LinearLayout Concepts and Properties ............................................................................................................ 74
LinearLayout Example...................................................................................................................................... 76
The Box Model ................................................................................................................................................. 80
All Things Are Relative .......................................................................................................................................... 81
RelativeLayout Concepts and Properties ................................ ......................................................................... 82
RelativeLayout Example................................................................................................................................... 84
Overlap ............................................................................................................................................................. 86
Tabula Rasa .......................................................................................................................................................... 87
TableLayout Concepts and Properties ............................................................................................................. 87
TableLayout Example....................................................................................................................................... 89
Scrollwork ............................................................................................................................................................. 90
Chapter 11: The Input Method Framework.................................................... 93
Keyboards, Hard and Soft ..................................................................................................................................... 93
Tailored to Your Needs.......................................................................................................................................... 94
Tell Android Where It Can Go ................................................................................................................................ 98
Fitting In.............................................................................................................................................................. 100
Jane, Stop This Crazy Thing! .............................................................................................................................. 101
Chapter 12: Using Selection Widgets .......................................................... 103
Adapting to the Circumstances........................................................................................................................... 103
Using ArrayAdapter ........................................................................................................................................ 104
Lists of Naughty and Nice ................................................................................................................................... 104
Selection Modes ............................................................................................................................................ 106
Spin Control ........................................................................................................................................................ 108
Grid Your Lions (or Something Like That...) ........................................................................................................ 111
Fields: Now with 35% Less Typing! .................................................................................................................... 115
Galleries, Give or Take the Art ............................................................................................................................ 118
Chapter 13: Getting Fancy with Lists .......................................................... 119
Getting to First Base ........................................................................................................................................... 119
A Dynamic Presentation...................................................................................................................................... 121
Inflating Rows Ourselves .................................................................................................................................... 123
A Sidebar About Inflation ............................................................................................................................... 123
And Now, Back to Our Story .......................................................................................................................... 125
Better. Stronger. Faster. ..................................................................................................................................... 125
Using convertView ......................................................................................................................................... 125
Using the Holder Pattern................................................................................................................................ 127
Interactive Rows ................................................................................................................................................. 129
Chapter 14: Still More Widgets and Containers........................................... 135
Pick and Choose ................................................................................................................................................. 135
Time Keeps Flowing Like a River ........................................................................................................................ 140
Seeking Resolution ............................................................................................................................................. 141
Putting It on My Tab ............................................................................................................................................ 142
The Pieces ..................................................................................................................................................... 143
Wiring It Together .......................................................................................................................................... 144
Adding Them Up............................................................................................................................................. 146
Zgłoś jeśli naruszono regulamin