100% found this document useful (1 vote)
4 views

Android Recipes A Problem-Solution Approach Dave Smith instant download

The document is a promotional text for the book 'Android Recipes: A Problem-Solution Approach' by Dave Smith and Erik Hellman, which provides useful code snippets and recipes for Android development. It includes links to various other programming books in a similar problem-solution format. The book covers topics such as layouts, user interaction, networking, and device hardware interaction.

Uploaded by

kulykregusoy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
4 views

Android Recipes A Problem-Solution Approach Dave Smith instant download

The document is a promotional text for the book 'Android Recipes: A Problem-Solution Approach' by Dave Smith and Erik Hellman, which provides useful code snippets and recipes for Android development. It includes links to various other programming books in a similar problem-solution format. The book covers topics such as layouts, user interaction, networking, and device hardware interaction.

Uploaded by

kulykregusoy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Android Recipes A Problem-Solution Approach Dave

Smith download

https://textbookfull.com/product/android-recipes-a-problem-
solution-approach-dave-smith/

Download more ebook from https://textbookfull.com


We believe these products will be a great fit for you. Click
the link to download now, or visit textbookfull.com
to discover even more!

Raku Recipes: A Problem-Solution Approach J.J. Merelo

https://textbookfull.com/product/raku-recipes-a-problem-solution-
approach-j-j-merelo/

C Recipes - A Problem-Solution Approach 1st Edition


Shirish Chavan

https://textbookfull.com/product/c-recipes-a-problem-solution-
approach-1st-edition-shirish-chavan/

JavaScript Recipes: A Problem-Solution Approach 1st


Edition Russ Ferguson

https://textbookfull.com/product/javascript-recipes-a-problem-
solution-approach-1st-edition-russ-ferguson/

wxPython Recipes: A Problem - Solution Approach 1st


Edition Mike Driscoll

https://textbookfull.com/product/wxpython-recipes-a-problem-
solution-approach-1st-edition-mike-driscoll/
MATLAB Recipes: a problem-solution approach 2nd Edition
Michael Paluszek

https://textbookfull.com/product/matlab-recipes-a-problem-
solution-approach-2nd-edition-michael-paluszek/

Java 9 Recipes: A Problem-Solution Approach 3rd Edition


Josh Juneau

https://textbookfull.com/product/java-9-recipes-a-problem-
solution-approach-3rd-edition-josh-juneau/

Spring 5 Recipes: A Problem-Solution Approach 4th


Edition Marten Deinum

https://textbookfull.com/product/spring-5-recipes-a-problem-
solution-approach-4th-edition-marten-deinum/

PrestaShop Recipes : A Problem-Solution Approach 1st


Edition Arnaldo Pérez Castaño (Auth.)

https://textbookfull.com/product/prestashop-recipes-a-problem-
solution-approach-1st-edition-arnaldo-perez-castano-auth/

MATLAB Machine Learning Recipes: A Problem-Solution


Approach 2nd Edition Michael Paluszek

https://textbookfull.com/product/matlab-machine-learning-recipes-
a-problem-solution-approach-2nd-edition-michael-paluszek/
Android
Recipes
A Problem-Solution Approach

Useful code snippets and recipes using
Android 7.0 and more

Fifth Edition

Dave Smith
Erik Hellman
Android Recipes
A Problem-Solution Approach

Fifth Edition

Dave Smith
Erik Hellman
Android Recipes: A Problem-Solution Approach
Dave Smith Erik Hellman
Littleton, Colorado, USA Sundbyberg, Sweden

ISBN-13 (pbk): 978-1-4842-2258-4 ISBN-13 (electronic): 978-1-4842-2259-1


DOI 10.1007/978-1-4842-2259-1

Library of Congress Control Number: 2016961316

Copyright © 2016 by Dave Smith and Erik Hellman


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter
developed.
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.

The images of the Android Robot (01/Android Robot) are reproduced from work created and shared by Google and
used according to terms described in the Creative Commons 3.0 Attribution License. Android and all Android and
Google-based marks are trademarks or registered trademarks of Google Inc. in the United States and other countries.
Apress Media LLC is not affiliated with Google Inc., and this book was written without endorsement from Google Inc.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be
made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Managing Director: Welmoed Spahr


Lead Editor: Steve Anglin
Technical Reviewer: Wallace Jackson
Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan,
Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, James Markham,
Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing
Coordinating Editor: Mark Powers
Copy Editor: Mike
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global

Distributed to the book trade worldwide by Springer Science+Business Media New York, 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. Apress Media, LLC is a California LLC and the sole member (owner) is Springer
Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

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.

Any source code or other supplementary materials referenced by the author in this text are available to readers
at www.apress.com/9781484222584. For detailed information about how to locate your book’s source code,
go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary
Material section for each chapter.
Printed on acid-free paper
Contents at a Glance

About the Authors�������������������������������������������������������������������������������������������������xxiii


About the Technical Reviewer�������������������������������������������������������������������������������xxv
Acknowledgments�����������������������������������������������������������������������������������������������xxvii
Introduction����������������������������������������������������������������������������������������������������������xxix


■Chapter 1: Layouts and Views������������������������������������������������������������������������������� 1

■Chapter 2: User Interaction Recipes�������������������������������������������������������������������� 85

■Chapter 3: Communications and Networking���������������������������������������������������� 189

■Chapter 4: Interacting with Device Hardware and Media���������������������������������� 259

■Chapter 5: Persisting Data��������������������������������������������������������������������������������� 367

■Chapter 6: Interacting with the System������������������������������������������������������������� 443

■Chapter 7: Graphics and Drawing���������������������������������������������������������������������� 585

■Chapter 8: Working with Android NDK and RenderScript���������������������������������� 655

Index��������������������������������������������������������������������������������������������������������������������� 699

iii
Contents

About the Authors�������������������������������������������������������������������������������������������������xxiii


About the Technical Reviewer�������������������������������������������������������������������������������xxv
Acknowledgments�����������������������������������������������������������������������������������������������xxvii
Introduction����������������������������������������������������������������������������������������������������������xxix


■Chapter 1: Layouts and Views������������������������������������������������������������������������������� 1
1-1. Styling Common Components����������������������������������������������������������������������������������� 1
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 1
Solution�������������������������������������������������������������������������������������������������������������������������������������������������� 1
How It Works������������������������������������������������������������������������������������������������������������������������������������������� 1

1-2. Toggling System UI Elements���������������������������������������������������������������������������������� 10


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 10
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 11
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 11

1-3. Creating and Displaying Views������������������������������������������������������������������������������� 14


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 14
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 14
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 14

1-4. Animating a View���������������������������������������������������������������������������������������������������� 20


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 20
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 20
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 21

v
■ Contents

1-5. Animating Layout Changes������������������������������������������������������������������������������������� 26


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 26
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 26
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 26

1-6. Implementing Situation-Specific Layouts��������������������������������������������������������������� 29


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 29
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 30
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 30

1-7. Placing Text over Images���������������������������������������������������������������������������������������� 38


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 38
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 38

1-8. Making Extensible Collection Views����������������������������������������������������������������������� 39


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 39
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 39
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 40

1-9. Customizing Empty Lists����������������������������������������������������������������������������������������� 51


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 51
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 51
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 51

1-10. Using CardView in RecyclerView�������������������������������������������������������������������������� 53


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 53
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 53
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 54

1-11. Making RecyclerView Section Headers���������������������������������������������������������������� 57


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 57
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 58
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 58

1-12. Creating Compound Controls�������������������������������������������������������������������������������� 60


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 60
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 61
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 61

vi
■ Contents

1-13. Customizing Transition Animations����������������������������������������������������������������������� 64


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 64
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 64
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 64

1-14. Creating View Transformations����������������������������������������������������������������������������� 74


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 74
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 74
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 74

1-15. Using Android Data Bindings�������������������������������������������������������������������������������� 80


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 80
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 80
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 80

Summary������������������������������������������������������������������������������������������������������������������������ 84

■Chapter 2: User Interaction Recipes�������������������������������������������������������������������� 85
2-1. Leveraging the Action Bar��������������������������������������������������������������������������������������� 85
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 85
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 85
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 86

2-2. Locking Activity Orientation������������������������������������������������������������������������������������ 93


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 93
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 93
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 93

2-3. Performing Dynamic Orientation Locking��������������������������������������������������������������� 94


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 94
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 94
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 94

2-4. Manually Handling Rotation������������������������������������������������������������������������������������ 96


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 96
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 96
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 97

vii
■ Contents

2-5. Creating Contextual Actions������������������������������������������������������������������������������������ 99


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 99
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 100
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 100

2-6. Displaying a User Dialog Box�������������������������������������������������������������������������������� 105


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 105
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 105
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 105

2-7. Customizing Menus and Actions��������������������������������������������������������������������������� 111


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 111
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 111
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 111

2-8. Customizing BACK Behavior��������������������������������������������������������������������������������� 116


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 116
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 116
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 117

2-9. Emulating the HOME Button��������������������������������������������������������������������������������� 120


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 120
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 120
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 120

2-10. Monitoring TextView Changes����������������������������������������������������������������������������� 120


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 120
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 121
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 121

2-11. Customizing Keyboard Actions��������������������������������������������������������������������������� 123


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 123
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 124
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 124

viii
■ Contents

2-12. Dismissing the Soft Keyboard����������������������������������������������������������������������������� 126


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 126
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 127
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 127

2-13. Handling Complex Touch Events������������������������������������������������������������������������� 127


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 127
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 127
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 128

2-14. Forwarding Touch Events������������������������������������������������������������������������������������ 144


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 144
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 144
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 145

2-15. Blocking Touch Thieves��������������������������������������������������������������������������������������� 149


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 149
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 150
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 150

2-16. Making Drag-and-Drop Views����������������������������������������������������������������������������� 153


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 153
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 153
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 154

2-17. Using the Material Design Navigation Drawer���������������������������������������������������� 161


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 161
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 161
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 161

2-18. Swiping Between Views������������������������������������������������������������������������������������� 169


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 169
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 169
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 170

ix
■ Contents

2-19. Navigating with Tabs������������������������������������������������������������������������������������������� 180


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 180
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 180
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 180

2-20. Actionable User Feedback Using Snackbar�������������������������������������������������������� 183


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 183
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 183
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 184

Summary���������������������������������������������������������������������������������������������������������������������� 188

■Chapter 3: Communications and Networking���������������������������������������������������� 189
3-1. Displaying Web Information���������������������������������������������������������������������������������� 189
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 189
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 189
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 189

3-2. Intercepting WebView Events������������������������������������������������������������������������������� 194


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 194
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 194
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 195

3-3. Accessing WebView with JavaScript�������������������������������������������������������������������� 196


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 196
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 196
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 196

3-4. Displaying Online Images������������������������������������������������������������������������������������� 199


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 199
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 199
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 199

3-5. Downloading Completely in the Background�������������������������������������������������������� 201


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 201
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 201
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 201

x
■ Contents

3-6. Accessing a REST API������������������������������������������������������������������������������������������� 205


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 205
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 205
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 205

3-7. Parsing JSON�������������������������������������������������������������������������������������������������������� 212


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 212
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 212
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 212

3-8. Parsing XML���������������������������������������������������������������������������������������������������������� 216


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 216
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 216
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 216

3-9. Receiving SMS������������������������������������������������������������������������������������������������������ 220


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 220
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 220
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 220

3-10. Sending an SMS Message���������������������������������������������������������������������������������� 223


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 223
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 223
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 224

3-11. Communicating over Bluetooth�������������������������������������������������������������������������� 226


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 226
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 226
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 226

3-12. Querying Network Reachability�������������������������������������������������������������������������� 235


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 235
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 235
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 235

xi
■ Contents

3-13. Transferring Data with NFC��������������������������������������������������������������������������������� 237


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 237
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 238
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 238

3-14. Connecting over USB������������������������������������������������������������������������������������������ 245


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 245
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 246
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 246

3-15. Scanning for Bluetooth Low Energy beacons����������������������������������������������������� 255


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 255
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 255
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 256

Summary���������������������������������������������������������������������������������������������������������������������� 258

■Chapter 4: Interacting with Device Hardware and Media���������������������������������� 259
4-1. Integrating Device Location ��������������������������������������������������������������������������������� 259
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 259
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 259
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 260

4-2. Mapping Locations������������������������������������������������������������������������������������������������ 266


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 266
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 266
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 269

4-3. Annotating Maps��������������������������������������������������������������������������������������������������� 275


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 275
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 275
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 275

4-4. Monitoring Location Regions�������������������������������������������������������������������������������� 291


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 291
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 292
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 292

xii
■ Contents

4-5. Capturing Images and Video��������������������������������������������������������������������������������� 301


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 301
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 301
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 301

4-6. Making a Custom Camera Overlay������������������������������������������������������������������������ 307


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 307
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 307
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 307

4-7. Recording Audio���������������������������������������������������������������������������������������������������� 313


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 313
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 313
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 314

4-8. Capturing Custom Video��������������������������������������������������������������������������������������� 316


Problems�������������������������������������������������������������������������������������������������������������������������������������������� 316
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 316
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 316

4-9. Adding Speech Recognition���������������������������������������������������������������������������������� 320


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 320
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 320
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 321

4-10. Playing Back Audio/Video����������������������������������������������������������������������������������� 323


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 323
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 323
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 323

4-11. Playing Sound Effects����������������������������������������������������������������������������������������� 331


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 331
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 331
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 332

xiii
■ Contents

4-12. Creating a Tilt Monitor���������������������������������������������������������������������������������������� 334


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 334
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 334
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 334

4-13. Monitoring Compass Orientation������������������������������������������������������������������������ 338


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 338
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 338
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 339

4-14. Retrieving Metadata from Media Content����������������������������������������������������������� 342


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 342
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 342
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 342

4-15. Detecting User Motion���������������������������������������������������������������������������������������� 346


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 346
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 346
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 347

4-16. Making Applications Context Aware�������������������������������������������������������������������� 357


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 357
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 357

4-17. Authenticating Users Using Fingerprint Reader�������������������������������������������������� 362


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 362
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 362

4-18. Scanning QR Codes Using Vision API������������������������������������������������������������������ 364


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 364
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 364

Summary���������������������������������������������������������������������������������������������������������������������� 366

■Chapter 5: Persisting Data��������������������������������������������������������������������������������� 367
5-1. Making a Preference Screen�������������������������������������������������������������������������������� 367
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 367
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 367
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 367
xiv
■ Contents

5-2. Displaying Custom Preferences���������������������������������������������������������������������������� 373


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 373
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 373
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 374

5-3. Persisting Simple Data����������������������������������������������������������������������������������������� 379


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 379
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 379
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 379

5-4. Reading and Writing Files������������������������������������������������������������������������������������� 383


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 383
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 383
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 384

5-5. Using Files as Resources�������������������������������������������������������������������������������������� 391


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 391
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 391
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 391

5-6. Managing a Database������������������������������������������������������������������������������������������� 393


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 393
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 393
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 393

5-7. Querying a Database�������������������������������������������������������������������������������������������� 398


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 398
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 398
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 399

5-8. Backing Up Data��������������������������������������������������������������������������������������������������� 400


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 400
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 400
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 400

xv
■ Contents

5-9. Sharing Your Database������������������������������������������������������������������������������������������ 404


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 404
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 405
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 405

5-10. Sharing Your SharedPreferences������������������������������������������������������������������������ 412


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 412
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 412
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 413

5-11. Sharing Your Other Data������������������������������������������������������������������������������������� 421


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 421
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 421
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 422

5-12. Integrating with System Documents������������������������������������������������������������������� 428


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 428
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 428
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 429

Summary���������������������������������������������������������������������������������������������������������������������� 442

■Chapter 6: Interacting with the System������������������������������������������������������������� 443
6-1. Notifying from the Background����������������������������������������������������������������������������� 443
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 443
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 443
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 443

6-2. Creating Timed and Periodic Tasks����������������������������������������������������������������������� 466


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 466
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 466
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 466

6-3. Scheduling a Periodic Task����������������������������������������������������������������������������������� 468


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 468
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 468
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 469

xvi
■ Contents

6-4. Creating Sticky Operations����������������������������������������������������������������������������������� 476


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 476
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 476
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 477

6-5. Running Persistent Background Operations��������������������������������������������������������� 481


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 481
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 481
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 481

6-6. Launching Other Applications������������������������������������������������������������������������������� 488


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 488
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 488
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 488

6-7. Launching System Applications���������������������������������������������������������������������������� 492


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 492
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 492
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 492

6-8. Letting Other Applications Launch Your Application��������������������������������������������� 496


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 496
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 496
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 497

6-9. Interacting with Contacts�������������������������������������������������������������������������������������� 499


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 499
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 499
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 499

6-10. Reading Device Media and Documents�������������������������������������������������������������� 507


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 507
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 507
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 508

xvii
■ Contents

6-11. Saving Device Media and Documents���������������������������������������������������������������� 511


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 511
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 511
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 512

6-12. Reading Messaging Data������������������������������������������������������������������������������������ 516


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 516
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 516
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 518

6-13. Interacting with the Calendar����������������������������������������������������������������������������� 528


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 528
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 528
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 528

6-14. Logging Code Execution������������������������������������������������������������������������������������� 534


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 534
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 534
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 534

6-15. Creating a Background Worker��������������������������������������������������������������������������� 537


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 537
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 537
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 537

6-16. Customizing the Task Stack�������������������������������������������������������������������������������� 541


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 541
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 541
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 542

6-17. Implementing AppWidgets���������������������������������������������������������������������������������� 549


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 549
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 549
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 550

xviii
■ Contents

6-18. Supporting Restricted Profiles���������������������������������������������������������������������������� 570


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 570
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 570
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 571

Summary���������������������������������������������������������������������������������������������������������������������� 584

■Chapter 7: Graphics and Drawing���������������������������������������������������������������������� 585
7-1. Creating Drawables as Backgrounds�������������������������������������������������������������������� 585
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 585
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 585
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 586

7-2. Creating Custom State Drawables������������������������������������������������������������������������ 593


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 593
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 593
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 593

7-3. Applying Masks to Images������������������������������������������������������������������������������������ 598


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 598
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 598
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 599

7-4. Drawing over View Content����������������������������������������������������������������������������������� 609


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 609
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 609
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 610

7-5. High-Performance Drawing���������������������������������������������������������������������������������� 625


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 625
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 626
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 626

7-6. Custom Drawables������������������������������������������������������������������������������������������������ 637


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 637
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 637
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 637

xix
■ Contents

7-7. Tinting Drawable Elements����������������������������������������������������������������������������������� 639


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 639
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 639
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 639

7-8. Using Scalable Vector Assets�������������������������������������������������������������������������������� 644


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 644
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 644
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 645

Summary���������������������������������������������������������������������������������������������������������������������� 653

■Chapter 8: Working with Android NDK and RenderScript���������������������������������� 655
Android NDK����������������������������������������������������������������������������������������������������������������� 655
8-1. Adding Native Bits with JNI���������������������������������������������������������������������������������� 658
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 658
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 658
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 659

8-2. Building a Purely Native Activity��������������������������������������������������������������������������� 665


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 665
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 665
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 666

RenderScript���������������������������������������������������������������������������������������������������������������� 674
Using the RenderScript Support Package������������������������������������������������������������������������������������������ 675

8-3. Filtering Images with RenderScript���������������������������������������������������������������������� 676


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 676
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 676
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 676

8-4. Manipulating Images with RenderScript�������������������������������������������������������������� 681


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 681
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 681
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 681

xx
■ Contents

8-5. Faking Translucent Overlays with Blur������������������������������������������������������������������ 687


Problem���������������������������������������������������������������������������������������������������������������������������������������������� 687
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 687
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 688

Summary���������������������������������������������������������������������������������������������������������������������� 697

Index��������������������������������������������������������������������������������������������������������������������� 699

xxi
About the Authors

Dave Smith is a professional engineer developing hardware and software


for mobile and embedded platforms. Dave's engineering efforts are
currently focused full-time on Android development. Since 2009, Dave has
worked on developing at all levels of the Android platform, from writing
user applications using the software development kit to building and
customizing the Android source code. Dave regularly communicates via
his development blog (http://blog.wiresareobsolete.com) and Twitter
stream @devunwired.

Erik Hellman is a professional software engineer who has worked on everything from small embedded
systems to large backend systems for telecom and banking. He is currently focused on everything around
the Android platform, and has been doing so since 2009. During this time he has been a frequent speaker
at conferences and meetup events, teaching new developers how to work with the platform, has been
developing a wide variety of apps, and worked for OEMs on integrating Android to new hardware devices.
Erik regularly communicates via his development blog (http://www.hellsoft.se) and Twitter stream
@ErikHellman.

xxiii
About the Technical Reviewer

Wallace Jackson has been writing for leading multimedia publications


about his work in new media content development since the advent of
Multimedia Producer Magazine nearly two decades ago. He has authored
a half-dozen Android book titles for Apress, including four titles in the
popular Pro Android series. Wallace received his undergraduate degree in
business economics from the University of California at Los Angeles and a
graduate degree in MIS design and implementation from the University of
Southern California. He is currently the CEO of Mind Taffy Design, a new
media content production and digital campaign design and development
agency.

xxv
Acknowledgments

First and foremost, I would like to thank my wife, Lorie, for her eternal patience and support during the long
hours I spent compiling and constructing the materials for this book. Second, I send a huge thank you to the
editorial team that Apress brought together to work with me and make the book the best it could possibly
be; you guys are the ones who make me look good. Without your time and effort, this project would not even
exist.
—Dave Smith
I want to start with thanking my wife. Without the patience and support from her, my contributions to this
book would never have happened. I’d also like to thank Apress and Dave for giving me the opportunity to
work on the fifth edition of Android Recipes. Finally, a big thank you to all the people who keep inspiring me
to keep writing.
—Erik Hellman

xxvii
Introduction

Welcome to the fifth edition of Android Recipes!


If you are reading this book, you probably don’t need to be told of the immense opportunity that mobile
devices represent for software developers and users. In recent years, Android has become one of the top
mobile platforms for device users. This means that you, as a developer, must know how to harness Android
so you can stay connected to this market and the potential that it offers. But any new platform brings with it
uncertainty about best practices and solutions to common needs and problems.
What we aim to do with Android Recipes is give you the tools to write applications for the Android
platform through direct examples targeted at the specific problems you are trying to solve. This book is not a
deep dive into the Android SDK, NDK, or any of the other tools. We don’t weigh you down with all the details
and theory behind the curtain. That’s not to say that those details aren’t interesting or important. You should
take the time to learn them, as they may save you from making future mistakes. However, more often than
not, they are simply a distraction when you are just looking for a solution to an immediate problem.
This book is not meant to teach you Java programming or even the building blocks of an Android
application. You won’t find many basic recipes in this book (such as how to display text with TextView, for
instance), as we feel these are tasks easily remembered once learned. Instead, we set out to address tasks
that developers, once comfortable with Android, need to do often but find too complex to accomplish with a
few lines of code.
Treat Android Recipes as a reference to consult, a resource-filled cookbook that you can always open to
find the pragmatic advice you need to get the job done quickly and well.

What Will You Find in the Book?


We dive into using the Android SDK to solve real problems. You will learn tricks for effectively creating a user
interface that runs well across device boundaries. You will become a master at incorporating the collection
of hardware (radios, sensors, and cameras) that makes mobile devices unique platforms. We’ll even discuss
how to make the system work for you by integrating with the services and applications provided by Google
and various device manufacturers.
Performance matters if you want your applications to succeed. Most of the time, this isn’t a problem,
because the Android runtime engines get progressively better at compiling bytecode into the device’s native
code. However, you might need to leverage the Android NDK to boost performance. Chapter 8 offers you an
introduction to the NDK and integrating native code into your application using Java Native Interface (JNI)
bindings.
The NDK is a complex technology, which can also reduce your application's portability. Also, while
good at increasing performance, the NDK doesn't address multicore processing very well for heavy
workloads. Fortunately, Google has eliminated this tedium and simplified the execute-on-multiple-cores
task while achieving portability by introducing RenderScript. Chapter 8 introduces you to RenderScript and
shows you how to use its compute engine (and automatically leverage CPU cores) to process images.

xxix
■ Introduction

Keep a Level Eye on the Target


Throughout the book, you will see that we have marked most recipes with the minimum API level that is
required to support them. Most of the recipes in this book are marked API Level 9, meaning that the code
used can be run in applications targeting any version of Android since 2.3 or later. This is one of the biggest
changes in this edition of the book, and the reason is that Google recently deprecated the support for earlier
API levels in their Support Library. Where necessary, we use APIs introduced in later versions. Pay close
attention to the API level marking of each recipe to ensure that you are not using code that doesn’t match up
with the version of Android your application is targeted to support.

xxx
CHAPTER 1

Layouts and Views

The Android platform is designed to operate on a variety of device types, screen sizes, and screen
resolutions. To assist developers in meeting this challenge, Android provides a rich toolkit of user interface
(UI) components to utilize and customize to the needs of their specific applications. Android also relies
heavily on an extensible XML framework and set resource qualifiers to create liquid layouts that can adapt to
these environmental changes. In this chapter, we take a look at some practical ways to shape this framework
to fit your specific development needs.

1-1. Styling Common Components


Problem
You want to create a consistent look and feel for your application across all the versions of Android your
users may be running, while reducing the amount of code required to maintain those customizations.

Solution
(API Level 1)
You can abstract common attributes that define the look and feel of your application views into XML styles.
Styles are collections of view attribute customizations, such as text size or background color, that should be
applied to multiple views throughout the application. Abstracting these attributes into a style allows the
common elements to be defined in a single location, making the code easier to update and maintain.
Android also supports grouping multiple styles together in a global element called a theme. Themes
apply to an entire context (such as an activity or application), and define styles that should apply to all the
views within that context. Every activity launch in your application has a theme applied to it, even if you
don’t define one. In such cases, the default system theme is applied instead.

How It Works
To explore the styles concept, let’s create an activity layout that looks like Figure 1-1.

Electronic supplementary material The online version of this chapter (doi:10.1007/978-1-4842-2259-1_1)


contains supplementary material, which is available to authorized users.

© Dave Smith and Erik Hellman 2016 1


D. Smith and E. Hellman, Android Recipes, DOI 10.1007/978-1-4842-2259-1_1
Chapter 1 ■ Layouts and Views

Figure 1-1. Styled widgets

As you can see, this view has some elements that we want to customize to look different than they
normally do with the styling from the default system theme applied. One option would be to define all the
attributes for all the views directly in our activity layout. If we were to do so, it would look like Listing 1-1.

■■Note Although it is possible to apply a style to a standard component to look completely different, one
should avoid changing the appearance too much as it could confuse users. Refer to the Material Design
specification at http://material.google.com for details on the recommendations for different UI components.

Listing 1-1. res/layout/activity_styled.xml


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="bold"
android:text="Select One"/>

2
Chapter 1 ■ Layouts and Views

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="@dimen/buttonHeight"
android:button="@null"
android:background="@drawable/background_radio"
android:gravity="center"
android:text="One"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="@dimen/buttonHeight"
android:button="@null"
android:background="@drawable/background_radio"
android:gravity="center"
android:text="Two"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="@dimen/buttonHeight"
android:button="@null"
android:background="@drawable/background_radio"
android:gravity="center"
android:text="Three"/>
</RadioGroup>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="bold"
android:text="Select All"/>
<TableRow>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="@dimen/buttonHeight"
android:minWidth="@dimen/checkboxWidth"
android:button="@null"
android:gravity="center"
android:textStyle="italic"
android:textColor="@color/text_checkbox"
android:text="One"/>

3
Random documents with unrelated
content Scribd suggests to you:
The sight re-nerved my courser's feet,
A moment staggering, feebly fleet,
A moment, with a faint low neigh,690
He answer'd, and then fell;
With gasps and glazing eyes he lay,
And reeking limbs immoveable;
His first and last career is done!
On came the troop—they saw him stoop,695
They saw me strangely bound along
His back with many a bloody thong:
They stop—they start—they snuff the air,
Gallop a moment here and there,
Approach, retire, wheel round and round,700
Then plunging back with sudden bound,
Headed by one black mighty steed
Who seem'd the patriarch of his breed,
Without a single speck or hair
Of white upon his shaggy hide.705
They snort—they foam—neigh—swerve aside,
And backward to the forest fly,
By instinct, from a human eye.—
They left me there to my despair,
Link'd to the dead and stiffening wretch,710
Whose lifeless limbs beneath me stretch,
Relieved from that unwonted weight,
From whence I could not extricate
Nor him nor me—and there we lay
The dying on the dead!715
I little deem'd another day
Would see my houseless, helpless head.

"And there from morn till twilight bound,


I felt the heavy hours toil round,
With just enough of life to see720
My last of suns go down on me,
In hopeless certainty of mind,
That makes us feel at length resign'd
To that which our foreboding years
Presents the worst and last of fears725
Inevitable—even a boon,
Nor more unkind for coming soon;
Yet shunn'd and dreaded with such care,
As if it only were a snare
That prudence might escape:730
At times both wish'd for and implored,
At times sought with self-pointed sword,
Yet still a dark and hideous close
To even intolerable woes,
And welcome in no shape.735
And, strange to say, the sons of pleasure,
They who have revell'd beyond measure
In beauty, wassail, wine, and treasure,
Die calm, or calmer oft than he
Whose heritage was misery:740
For he who hath in turn run through
All that was beautiful and new,
Hath nought to hope, and nought to leave;
And, save the future (which is view'd
Not quite as men are base or good,745
But as their nerves may be endued,)
With nought perhaps to grieve:—
The wretch still hopes his woes must end,
And Death, whom he should deem his friend,
Appears, to his distemper'd eyes,750
Arrived to rob him of his prize,
The tree of his new Paradise.
To-morrow would have given him all,
Repaid his pangs, repair'd his fall;
To-morrow would have been the first755
Of days no more deplored or curst,
But bright, and long, and beckoning years,
Seen dazzling through the mist of tears,
Guerdon of many a painful hour;
To-morrow would have given him power760
To rule, to shine, to smite, to save—
And must it dawn upon his grave?

XVIII
"The sun was sinking—still I lay
Chain'd to the chill and stiffening steed;
I thought to mingle there our clay;765
And my dim eyes of death had need,
No hope arose of being freed.
I cast my last looks up the sky,
And there between me and the sun
I saw the expecting raven fly,770
Who scarce would wait till both should die
Ere his repast begun.
He flew, and perch'd, then flew once more,
And each time nearer than before;
I saw his wing through twilight flit,775
And once so near me he alit
I could have smote, but lack'd the strength;
But the slight motion of my hand,
And feeble scratching of the sand,
The exerted throat's faint struggling noise,780
Which scarcely could be call'd a voice,
Together scared him off at length.—
I know no more—my latest dream
Is something of a lovely star
Which fix'd my dull eyes from afar,785
And went and came with wandering beam,
And of the cold, dull, swimming, dense
Sensation of recurring sense,
And then subsiding back to death,
And then again a little breath,790
A little thrill, a short suspense,
An icy sickness curdling o'er
My heart, and sparks that cross'd my brain—
A gasp, a throb, a start of pain,
A sigh, and nothing more.795
XIX
"I woke—Where was I?—Do I see
A human face look down on me?
And doth a roof above me close?
Do these limbs on a couch repose?
Is this a chamber where I lie?800
And is it mortal, yon bright eye
That watches me with gentle glance?
I closed my own again once more,
As doubtful that the former trance
Could not as yet be o'er.805
A slender girl, long-hair'd, and tall,
Sate watching by the cottage wall:
The sparkle of her eye I caught,
Even with my first return of thought;
For ever and anon she threw810
A prying, pitying glance on me
With her black eyes so wild and free.
I gazed, and gazed, until I knew
No vision it could be,—
But that I lived, and was released815
From adding to the vulture's feast.
And when the Cossack maid beheld
My heavy eyes at length unseal'd,
She smiled—and I essay'd to speak,
But fail'd—and she approach'd, and made820
With lip and finger signs that said,
I must not strive as yet to break
The silence, till my strength should be
Enough to leave my accents free;
And then her hand on mine she laid,825
And smooth'd the pillow for my head,
And stole along on tiptoe tread,
And gently oped the door, and spake
In whispers—ne'er was voice so sweet!
Even music follow'd her light feet;—830
But those she call'd were not awake,
And she went forth; but, ere she pass'd,
Another look on me she cast,
Another sign she made, to say,
That I had nought to fear, that all835
Were near at my command or call,
And she would not delay
Her due return:—while she was gone,
Methought I felt too much alone.
XX
"She came with mother and with sire—840
What need of more?—I will not tire
With long recital of the rest,
Since I became the Cossack's guest.
They found me senseless on the plain—
They bore me to the nearest hut—845
They brought me into life again—
Me—one day o'er their realm to reign!
Thus the vain fool who strove to glut
His rage, refining on my pain,
Sent me forth to the wilderness,850
Bound, naked, bleeding, and alone,
To pass the desert to a throne,—
What mortal his own doom may guess?—
Let none despond, let none despair!
To-morrow the Borysthenes855
May see our coursers graze at ease
Upon his Turkish bank,—and never
Had I such welcome for a river
As I shall yield when safely there.
Comrades, good night!"—The Hetman threw860
His length beneath the oak-tree shade,
With leafy couch already made,
A bed nor comfortless nor new
To him who took his rest whene'er
The hour arrived, no matter where:865
His eyes the hastening slumbers steep.
And if ye marvel Charles forgot
To thank his tale he wonder'd not,—
The king had been an hour asleep.

THE DESTRUCTION OF SENNACHERIB

The Assyrian came down like the wolf on the fold,


And his cohorts were gleaming in purple and gold;
And the sheen of their spears was like stars on the sea,
When the blue wave rolls nightly on deep Galilee.

Like the leaves of the forest when Summer is green,5


That host with their banners at sunset were seen:
Like the leaves of the forest when Autumn hath blown,
That host on the morrow lay withered and strown.

For the Angel of Death spread his wings on the blast,


And breathed in the face of the foe as he passed;10
And the eyes of the sleepers waxed deadly and chill,
And their hearts but once heaved, and forever grew still!

And there lay the steed with his nostril all wide,
But through it there rolled not the breath of his pride;
And the foam of his gasping lay white on the turf,15
And cold as the spray of the rock-beating surf.

And there lay the rider distorted and pale,


With the dew on his brow, and the rust on his mail,
And the tents were all silent, the banners alone,
The lances unlifted, the trumpet unblown.20

And the widows of Ashur are loud in their wail,


And the idols are broke in the temple of Baal;
And the might of the Gentile, unsmote by the sword,
Hath melted like snow in the glance of the Lord!
JOHN KEATS
THE EVE OF ST. AGNES

I
St. Agnes' Eve—Ah, bitter chill it was!
The owl, for all his feathers, was a-cold;
The hare limp'd trembling through the frozen grass,
And silent was the flock in woolly fold:
Numb were the Beadsman's[143] fingers, while he told5
His rosary, and while his frosted breath,
Like pious incense from a censer old,
Seem'd taking flight for heaven, without a death,
Past the sweet Virgin's picture, while his prayer he saith.

II
His prayer he saith, this patient, holy man;10
Then takes his lamp, and riseth from his knees
And back returneth, meagre, barefoot, wan,
Along the chapel aisle by slow degrees:
The sculptured dead, on each side, seem to freeze,
Emprison'd in black, purgatorial rails:15
Knights, ladies, praying in dumb orat'ries,
He passeth by; and his weak spirit fails
To think how they may ache in icy hoods and mails.

III
Northward he turneth through a little door,
And scarce three steps, ere Music's golden tongue20
Flatter'd to tears this aged man and poor;
But no—already had his death-bell rung;
The joys of all his life were said and sung:
His was harsh penance on St. Agnes' Eve;
Another way he went, and soon among25
Rough ashes sat he for his soul's reprieve,
And all night kept awake, for sinners' sake to grieve.
IV
That ancient Beadsman heard the prelude soft;
And so it chanced, for many a door was wide,
From hurry to and fro. Soon, up aloft,30
The silver, snarling[144] trumpets 'gan to chide:
The level chambers, ready with their pride,
Were glowing to receive a thousand guests:
The carved angels, ever eager-eyed,
Stared, where upon their heads the cornice rests,35
With hair blown back, and wings put crosswise on their breasts.

V
At length burst in the argent revelry,
With plume, tiara, and all rich array,
Numerous as shadows haunting fairily
The brain, new-stuff'd, [145]in youth, with triumphs gay40
Of old romance. These let us wish away,
And turn, sole-thoughted, to one Lady there,
Whose heart had brooded, all that wintry day,
On love, and wing'd St. Agnes' saintly care,
As she had heard old dames full many times declare.45

VI
They told her how, upon St. Agnes' Eve,[146]
Young virgins might have visions of delight,
And soft adorings from their loves receive
Upon the honey'd middle of the night,
If ceremonies due they did aright;50
As, supperless to bed they must retire,
And couch supine their beauties, lily white;
Nor look behind, nor sideways, but require
Of Heaven with upward eyes for all that they desire.

VII
Full of this whim was thoughtful Madeline:55
The music, yearning like a God in pain,
She scarcely heard: her maiden eyes divine,
Fix'd on the floor, saw many a sweeping train
Pass by—she heeded not at all: in vain
Came many a tiptoe, amorous cavalier,60
And back retired; not cool'd by high disdain,
But she saw not: her heart was otherwhere;
She sigh'd for Agnes' dreams, the sweetest of the year.

VIII
She danced along with vague, regardless eyes,
Anxious her lips, her breathing quick and short:65
The hallow'd hour was near at hand: she sighs
Amid the timbrels, and the throng'd resort
Of whisperers in anger, or in sport;
'Mid looks of love, defiance, hate, and scorn,
Hoodwink'd with faery fancy; all amort,[147]70
Save to St. Agnes and her lambs[148] unshorn,
And all the bliss to be before to-morrow morn.

IX
So, purposing each moment to retire,
She lingered still. Meantime, across the moors,
Had come young Porphyro,[149] with heart on fire75
For Madeline. Beside the portal doors,
Buttress'd[150] from moonlight, stands he, and implores
All saints to give him sight of Madeline,
But for one moment in the tedious hours,
That he might gaze and worship all unseen;80
Perchance speak, kneel, touch, kiss—in sooth[151] such
things have been.
X
He ventures in: let no buzz'd whisper tell:
All eyes be muffled, or a hundred swords
Will storm his heart, Love's fev'rous citadel:
For him, those chambers held barbarian hordes,85
Hyena[152] foemen, and hot-blooded lords,
Whose very dogs would execrations howl
Against his lineage: not one breast affords
Him any mercy, in that mansion foul,
Save one old beldame,[153] weak in body and in soul.90

XI
Ah, happy chance! the aged creature came,
Shuffling along with ivory-headed wand,
To where he stood, hid from the torch's flame,
Behind a broad hall-pillar, far beyond
The sound of merriment and chorus bland:95
He startled her; but soon she knew his face,
And grasp'd his fingers in her palsied hand,
Saying, "Mercy, Porphyro! hie thee from this place;
They are all here to-night, the whole bloodthirsty race!

XII
"Get hence! get hence! there's dwarfish Hildebrand;100
He had a fever late, and in the fit
He cursed thee and thine, both house and land:
Then there's that old Lord Maurice, not a whit
More tame for his gray hairs—Alas me! flit!
Flit like a ghost away."—Ah, Gossip[154] dear,105
We're safe enough; here in this armchair sit,
And tell me how"—"Good Saints! not here, not here;
Follow me, child, or else these stones will be thy bier."

XIII
He follow'd through a lowly arched way,
Brushing the cobwebs with his lofty plume;110
And as she mutter'd "Well-a—well-a-day!"
He found him in a little moonlight room,
Pale, latticed, chill, and silent as a tomb.
"Now tell me where is Madeline," said he,
"O tell me, Angela, by the holy loom[155]115
Which none but secret sisterhood may see,
When they St. Agnes' wool are weaving piously."

XIV
"St. Agnes! Ah! it is St. Agnes' Eve—
Yet men will murder upon holy days:
Thou must hold water in a witch's sieve,[156]120
And be liege-lord of all the Elves and Fays,
To venture so: it fills me with amaze
To see thee, Porphyro!—St. Agnes' Eve!
God's help! my lady fair the conjuror plays
This very night: good angels her deceive!125
But let me laugh awhile, I've mickle[157] time to grieve."

XV
Feebly she laugheth in the languid moon,
While Porphyro upon her face doth look,
Like puzzled urchin on an aged crone
Who keepeth closed a wond'rous riddlebook,130
As spectacled she sits in chimney nook.
But soon his eyes grew brilliant, when she told
His lady's purpose; and he scarce could brook
Tears, at the thought of those enchantments cold,
And Madeline asleep in lap[158] of legends old.135

XVI
Sudden a thought came like a full-blown rose,
Flushing his brow, and in his pained heart
Made purple riot[159]: then doth he propose
A stratagem, that makes the beldame start:
"A cruel man and impious thou art:140
Sweet lady, let her pray, and sleep, and dream
Alone with her good angels, far apart
From wicked men like thee. Go, go! I deem
Thou canst not surely be the same that thou didst seem."

XVII
"I will not harm her, by all saints I swear,"145
Quoth Porphyro: "O may I ne'er find grace
When my weak voice shall whisper its last prayer,
If one of her soft ringlets I displace,
Or look with ruffian passion in her face:
Good Angela, believe me by these tears;150
Or I will, even in a moment's space,
Awake, with horrid shout, my foemen's ears,
And beard them, though they be more fang'd than wolves and
bears."
XVIII
"Ah! why wilt thou affright a feeble soul?
A poor, weak, palsy-stricken, church-yard thing,155
Whose passing-bell may ere the midnight toll;
Whose prayers for thee, each morn and evening,
Were never miss'd." Thus plaining, doth she bring
A gentler speech from burning Porphyro;
So woful, and of such deep sorrowing,160
That Angela gives promise she will do
Whatever he shall wish, betide her weal or woe.

XIX
Which was, to lead him, in close secrecy,
Even to Madeline's chamber, and there hide
Him in a closet, of such privacy165
That he might see her beauty unespied,
And win perhaps that night a peerless bride,
While legion'd fairies paced the coverlet,
And pale enchantment held her sleepy-eyed.
Never on such a night have lovers met,170
Since Merlin[160] paid his Demon all the monstrous debt.

XX
"It shall be as thou wishest," said the Dame:
"All cates[161] and dainties shall be stored there
Quickly on this feast-night: by the tambour frame[162]
Her own lute thou wilt see: no time to spare,175
For I am slow and feeble, and scarce dare
On such a catering trust my dizzy head.
Wait here, my child, with patience; kneel in prayer
The while: Ah! thou must needs the lady wed,
Or may I never leave my grave among the dead."180
XXI
So saying she hobbled off with busy fear.
The lover's endless minutes slowly pass'd;
The Dame return'd, and whisper'd in his ear
To follow her; with aged eyes aghast
From fright of dim espial. Safe at last,185
Through many a dusky gallery, they gain
The maiden's chamber, silken, hush'd and chaste;
Where Porphyro took covert, pleased amain.
His poor guide hurried back with agues in her brain.

XXII
Her falt'ring hand upon the balustrade,190
Old Angela was feeling for the stair,
When Madeline, St. Agnes' charmed maid,
Rose, like a mission'd spirit, unaware:
With silver taper's light, and pious care,
She turn'd, and down the aged gossip led195
To a safe level matting. Now prepare,
Young Porphyro, for gazing on that bed;
She comes, she comes again, like ring-dove fray'd and fled.

XXIII
Out went the taper as she hurried in;
Its little smoke, in pallid moonshine, died:200
She closed the door, she panted, all akin
To spirits of the air, and visions wide:
No uttered syllable, or, woe betide!
But to her heart, her heart was voluble,
Paining with eloquence her balmy side;205
As though a tongueless nightingale should swell
Her throat in vain, and die, heart-stifled in her dell.
XXIV
A casement high[163] and triple arch'd there was,
All garlanded with carven imag'ries
Of fruits, and flowers, and bunches of knot-grass,210
And diamonded with panes of quaint device,
Innumerable of stains and splendid dyes,
As are the tiger-moth's deep-damask'd wings;
And in the midst, 'mong thousand heraldries,[164]
And twilight saints, and dim emblazonings,[165]215
A shielded scutcheon blush'd with blood of queens and kings.

XXV
Full on this casement shone the wintry moon,
And threw warm gules[166] on Madeline's fair breast,
As down she knelt for heaven's grace and boon;
Rose-bloom fell on her hands, together prest,220
And on her silver cross soft amethyst,
And on her hair a glory, like a saint:
She seem'd a splendid angel, newly drest,
Save wings, for heaven:—Porphyro grew faint;
She knelt, so pure a thing, so free from mortal taint.225

XXVI
Anon his heart revives: her vespers done,
Of all its wreathed pearls her hair she frees;
Unclasps her warmed jewels one by one;
Loosens her fragrant bodice; by degrees
Her rich attire creeps rustling to her knees:230
Half-hidden, like a mermaid in sea-weed,
Pensive awhile she dreams awake, and sees,
In fancy, fair St. Agnes in her bed,
But dares not look behind, or all the charm is fled.
XXVII
Soon, trembling in her soft and chilly nest,235
In sort of wakeful swoon, perplex'd she lay,
Until the poppied warmth of sleep oppress'd
Her soothed limbs, and soul fatigued away;
Flown, like a thought, until the morrow-day;
Blissfully haven'd both from joy and pain;240
Clasp'd like a missal[167] where swart Paynims pray;
Blinded alike from sunshine and from rain,
As though a rose should shut, and be a bud again.

XXVIII
Stol'n to this paradise, and so entranced,
Porphyro gazed upon her empty dress,245
And listen'd to her breathing, if it chanced
To wake into a slumberous tenderness;
Which when he heard, that minute did he bless,
And breathed himself: then from the closet crept,
Noiseless as fear in a wide wilderness,250
And over the hush'd carpet, silent, stept,
And 'tween the curtains peep'd, where, lo!—how fast she slept.

XXIX
Then by the bed-side, where the faded moon
Made a dim, silver twilight, soft he set
A table, and, half anguish'd, threw thereon255
A cloth of woven crimson, gold, and jet:—
O for some drowsy Morphean[168] amulet!
The boisterous, midnight, festive clarion,
The kettle-drum, and far-heard clarionet,
Affray his ears, though but in dying tone:—260
The hall-door shuts again, and all the noise is gone.
XXX
And still she slept an azure-lidded sleep,[169]
In blanched linen, smooth, and lavender'd,
While he from forth the closet brought a heap
Of candied apple, quince, and plum, and gourd;265
With jellies soother[170] than the creamy curd,
And lucent[171] syrops, tinct with cinnamon;
Manna and dates, in argosy transferr'd
From Fez; and spiced dainties, every one,
From silken Samarcand to cedar'd Lebanon.270

XXXI
These delicates he heap'd with glowing hand
On golden dishes and in baskets bright
Of wreathed silver: sumptuous they stand
In the retired quiet of the night,
Filling the chilly room with perfume light.—275
"And now, my love, my seraph fair, awake!
Thou art my heaven, and I thine eremite[172]:
Open thine eyes, for meek St. Agnes' sake,
Or I shall drowse beside thee, so my soul doth ache."

XXXII
Thus whispering, his warm, unnerved arm280
Sank in her pillow. Shaded was her dream
By the dusk curtains:—'twas a midnight charm
Impossible to melt as iced stream:
The lustrous salvers in the moonlight gleam;
Broad golden fringe upon the carpet lies:285
It seem'd he never, never could redeem
From such a steadfast spell his lady's eyes;
So mused awhile, entoil'd in woofed phantasies.
XXXIII
Awakening up, he took her hollow lute,—
Tumultuous,—and, in chords that tenderest be.290
He play'd an ancient ditty, long since mute,
In Provence call'd "La belle dame sans mercy:[173]"
Close to her ear touching the melody;—
Wherewith disturb'd, she utter'd a soft moan:
He ceased—she panted quick—and suddenly295
Her blue affrayed eyes wide open shone:
Upon his knees he sank, pale as smooth-sculptured stone.

XXXIV
Her eyes were open, but she still beheld,
Now wide awake, the vision of her sleep:
There was a painful change, that nigh expell'd300
The blisses of her dream so pure and deep
At which fair Madeline began to weep,
And moan forth witless words with many a sigh;
While still her gaze on Porphyro would keep;
Who knelt, with joined hands and piteous eye,305
Fearing to move or speak, she look'd so dreamingly.

XXXV
"Ah, Porphyro!" said she, "but even now
Thy voice was at sweet tremble in mine ear,
Made tuneable with every sweetest vow;
And those sad eyes were spiritual and clear:310
How changed thou art! how pallid, chill, and drear!
Give me that voice again, my Porphyro,
Those looks immortal, those complainings dear!
Oh leave me not in this eternal woe,
For if thou diest, my Love, I know not where to go."315
XXXVI
Beyond a mortal man impassion'd far
At these voluptuous accents, he arose,
Ethereal, flush'd, and like a throbbing star
Seen 'mid the sapphire heaven's deep repose;
Into her dream he melted, as the rose320
Blendeth its odour with the violet,—
Solution sweet: meantime the frost-wind blows
Like Love's alarum pattering the sharp sleet
Against the window-panes; St. Agnes' moon hath set.

XXXVII
'Tis dark: quick pattereth the flaw-blown sleet:325
"This is no dream, my bride, my Madeline!"
'Tis dark: the iced gusts still rave and beat:
"No dream, alas! alas! and woe is mine!
Porphyro will leave me here to fade and pine.—
Cruel! what traitor could thee hither bring?330
I curse not, for my heart is lost in thine,
Though thou forsakest a deceived thing;—
A dove forlorn and lost with sick unpruned wing."

XXXVIII
"My Madeline! sweet dreamer! lovely bride!
Say, may I be for aye thy vassal blest?335
Thy beauty's shield, heart-shaped and vermeil dyed?
Ah, silver shrine, here will I take my rest
After so many hours of toil and quest,
A famish'd pilgrim,—saved by miracle.
Though I have found, I will not rob thy nest340
Saving of thy sweet self; if thou think'st well
To trust, fair Madeline, to no rude infidel.
XXXIX
"Hark! 'tis an elfin storm from faery land,
Of haggard seeming, but a boon indeed:
Arise—arise! the morning is at hand:—345
The bloated wassailers[174] will never heed:—
Let us away, my love, with happy speed;
There are no ears to hear, or eyes to see,—
Drown'd all in Rhenish and the sleepy mead:
Awake! arise! my love, and fearless be,350
For o'er the southern moors I have a home for thee."

XL
She hurried at his words, beset with fears,
For there were sleeping dragons all around,
At glaring watch, perhaps, with ready spears—
Down the wide stairs a darkling way they found.—355
In all the house was heard no human sound.
A chain-droop'd lamp was flickering by each door;
The arras, rich with horseman, hawk, and hound,
Flutter'd in the besieging wind's uproar;
And the long carpets rose along the gusty floor.360

XLI
They glide, like phantoms, into the wide hall;
Like phantoms to the iron porch they glide,
Where lay the Porter, in uneasy sprawl,
With a huge empty flagon by his side:
The wakeful bloodhound rose, and shook his hide,365
But his sagacious eye an inmate owns:
By one, and one, the bolts full easy slide:—
The chains lie silent on the footworn stones;—
The key turns, and the door upon its hinges groans;
XLII
And they are gone: aye, ages long ago370
These lovers fled away into the storm.
That night the Baron dreamt of many a woe,
And all his warrior-guests, with shade and form
Of witch, and demon, and large coffin-worm,
Were long be-nightmared. Angela[175] the old375
Died palsy-twitch'd, with meagre face deform;
The Beadsman, after thousand aves told,
For aye unsought-for slept among his ashes cold.
ALFRED TENNYSON
DORA
With farmer Allan at the farm abode
William and Dora. William was his son,
And she his niece. He often looked at them,
And often thought, "I'll make them man and wife."
Now Dora felt her uncle's will in all,5
And yearn'd towards William; but the youth, because
He had been always with her in the house,
Thought not of Dora.
Then there came a day
When Allan call'd his son, and said, "My son:
I married late, but I would wish to see10
My grandchild on my knees before I die:
And I have set my heart upon a match.
Now therefore look to Dora; she is well
To look to; thrifty too beyond her age.
She is my brother's daughter: he and I15
Had once hard words, and parted, and he died
In foreign lands; but for his sake I bred
His daughter Dora: take her for your wife;
For I have wish'd this marriage, night and day,
For many years." But William answer'd short:20
"I cannot marry Dora; by my life,
I will not marry Dora." Then the old man
Was wroth, and doubled up his hands, and said:
"You will not, boy! you dare to answer thus!
But in my time a father's word was law,25
And so it shall be now for me. Look to it;
Consider, William: take a month to think,
And let me have an answer to my wish;
Or, by the Lord that made me, you shall pack,
And never more darken my doors again."30
But William answer'd madly; bit his lips,
And broke away. The more he look'd at her
The less he liked her; and his ways were harsh;
But Dora bore them meekly. Then before
The month was out he left his father's house,35
And hired himself to work within the fields;
And half in love, half spite, he woo'd and wed
A laborer's daughter, Mary Morrison.
Then, when the bells were ringing, Allan call'd
His niece and said: "My girl, I love you well;40
But if you speak with him that was my son,
Or change a word with her he calls his wife,
My home is none of yours. My will is law."
And Dora promised, being meek. She thought,
"It cannot be: my uncle's mind will change!"45
And days went on, and there was born a boy
To William; then distresses came on him;
And day by day he pass'd his father's gate,
Heart-broken, and his father help'd him not.
But Dora stored what little she could save,50
And sent it them by stealth, nor did they know
Who sent it; till at last a fever seized
On William, and in harvest time he died.
Then Dora went to Mary. Mary sat
And look'd with tears upon her boy, and thought55
Hard things of Dora. Dora came and said:
"I have obey'd my uncle until now,
And I have sinn'd, for it was all thro' me
This evil came on William at the first.
But, Mary, for the sake of him that's gone,60
And for your sake, the woman that he chose,
And for this orphan, I am come to you:
You know there has not been for these five years
So full a harvest: let me take the boy,
And I will set him in my uncle's eye65
Among the wheat; that when his heart is glad
Of the full harvest, he may see the boy,
And bless him for the sake of him that's gone."
And Dora took the child, and went her way
Across the wheat, and sat upon a mound70
That was unsown, where many poppies grew.
Far off the farmer came into the field
And spied her not; for none of all his men
Dare tell him Dora waited with the child;
And Dora would have risen and gone to him,75
But her heart fail'd her; and the reapers reap'd,
And the sun fell, and all the land was dark.
But when the morrow came, she rose and took
The child once more, and sat upon the mound;
And made a little wreath of all the flowers80
That grew about, and tied it round his hat
To make him pleasing in her uncle's eye.
Then when the farmer pass'd into the field
He spied her, and he left his men at work,
And came and said: "Where were you yesterday?85
Whose child is that? What are you doing here?"
So Dora cast her eyes upon the ground,
And answer'd softly, "This is William's child!"
"And did I not," said Allan, "did I not
Forbid you, Dora?" Dora said again:90
"Do with me as you will, but take the child,
And bless him for the sake of him that's gone!"
And Allan said, "I see it is a trick
Got up betwixt you and the woman there.
I must be taught my duty, and by you!95
You knew my word was law, and yet you dared
To slight it. Well—for I will take the boy;
But go you hence, and never see me more."
So saying, he took the boy, that cried aloud
And struggled hard. The wreath of flowers fell100
At Dora's feet. She bow'd upon her hands,
And the boy's cry came to her from the field,
More and more distant. She bow'd down her head,
Remembering the day when first she came,
And all the things that had been. She bow'd down105
And wept in secret; and the reapers reap'd,
And the sun fell, and all the land was dark.
Then Dora went to Mary's house, and stood
Upon the threshold. Mary saw the boy
Was not with Dora. She broke out in praise110
To God, that help'd her in her widowhood.
And Dora said, "My uncle took the boy;
But, Mary, let me live and work with you:
He says that he will never see me more."
Then answer'd Mary, "This shall never be,115
That thou shouldst take my trouble on thyself:
And, now I think, he shall not have the boy,
For he will teach him hardness, and to slight
His mother; therefore thou and I will go,
And I will have my boy, and bring him home;120
And I will beg of him to take thee back:
But if he will not take thee back again,
Then thou and I will live within one house,
And work for William's child, until he grows
Of age to help us."
So the women kiss'd125
Each other, and set out, and reach'd the farm.
The door was off the latch: they peep'd, and saw
The boy set up betwixt his grandsire's knees,
Who thrust him in the hollows of his arm,
And clapt him on the hands and on the cheeks,130
Like one that loved him: and the lad stretch'd out
And babbled for the golden seal, that hung
From Allan's watch, and sparkled by the fire.
Then they came in: but when the boy beheld
His mother, he cried out to come to her:135
And Allan set him down, and Mary said:
"O Father!—if you let me call you so—
I never came a-begging for myself,
Or William, or this child; but now I come
For Dora: take her back; she loves you well.140
O Sir, when William died, he died at peace
With all men; for I ask'd him, and he said,
He could not ever rue his marrying me—
I had been a patient wife: but, Sir, he said
That he was wrong to cross his father thus:145
'God bless him!' he said, 'and may he never know
The troubles I have gone thro'!' Then he turn'd
His face and pass'd—unhappy that I am!
But now, Sir, let me have my boy, for you
Will make him hard, and he will learn to slight150
His father's memory; and take Dora back,
And let all this be as it was before."
So Mary said, and Dora hid her face
By Mary. There was silence in the room;
And all at once the old man burst in sobs:—155
"I have been to blame—to blame. I have kill'd my son.
I have kill'd him—but I loved him—my dear son.
May God forgive me!—I have been to blame.
Kiss me, my children."
Then they clung about
The old man's neck, and kiss'd him many times160
And all the man was broken with remorse;
And all his love came back a hundredfold;
And for three hours he sobb'd o'er William's child,
Thinking of William.
So those four abode
Within one house together; and as years165
Went forward, Mary took another mate;
But Dora lived unmarried till her death.
ŒNONE—1832
There lies a vale in Ida,[176] lovelier
Than all the valleys of Ionian[177] hills.
The swimming vapour slopes athwart the glen,
Puts forth an arm, and creeps from pine to pine,
And loiters, slowly drawn. On either hand5
The lawns and meadow-ledges midway down
Hang rich in flowers, and far below them roars
The long brook falling thro' the clov'n ravine
In cataract after cataract to the sea.
Behind the valley topmost Gargarus[178]10
Stands up and takes the morning: but in front
The gorges, opening wide apart, reveal
Troas[179] and Ilion's[180] column'd citadel,
The crown of Troas.
Hither came at noon
Mournful Œnone, wandering forlorn15
Of Paris,[181] once her playmate on the hills.
Her cheek had lost the rose, and round her neck
Floated her hair or seem'd to float in rest.
She, leaning on a fragment twined with vine,
Sang to the stillness, till the mountain-shade20
Sloped downward to her seat from the upper cliff.

"O mother Ida, many-fountain'd Ida,


Dear mother Ida, hearken ere I die.
For now the noonday quiet holds the hill:
The grasshopper is silent in the grass:25
The lizard, with his shadow on the stone,
Rests like a shadow, and the winds are dead.
The purple flower droops: the golden bee
Is lily-cradled: I alone awake.
My eyes are full of tears, my heart of love,30
My heart is breaking, and my eyes are dim,
And I am all aweary of my life.
"O mother Ida, many-fountained Ida,
Dear mother Ida, hearken ere I die.
Hear me, O Earth, hear me, O Hills, O Caves35
That house the cold crown'd snake! O mountain brooks,
I am the daughter of a River-God,[182]
Hear me, for I will speak, and build up all
My sorrow with my song, as yonder walls
Rose slowly to a music slowly breathed,[183]40
A cloud that gather'd shape: for it may be
That, while I speak of it, a little while
My heart may wander from its deeper woe.

"O mother Ida, many-fountain'd Ida,


Dear mother Ida, hearken ere I die.45
I waited underneath the dawning hills,
Aloft the mountain lawn was dewy-dark,
And dewy-dark aloft the mountain pine:
Beautiful Paris, evil-hearted Paris,
Leading a jet-black goat white-horn'd, white hooved,50
Came up from reedy Simois[184] all alone.

"O mother Ida, hearken ere I die.


Far-off the torrent call'd me from the cleft:
Far up the solitary morning smote
The streaks of virgin snow. With down-dropt eyes55
I sat alone: white-breasted like a star
Fronting the dawn he moved; a leopard skin
Droop'd from his shoulder, but his sunny hair
Cluster'd about his temples like a God's:
And his cheek brightened as the foam-bow brightens60
When the wind blows the foam, and all my heart
Went forth to embrace him coming ere he came.

"Dear mother Ida, hearken ere I die.


He smiled, and opening out his milk-white palm
Disclosed a fruit of pure Hesperian[185] gold,65
That smelt ambrosially,[186] and while I look'd
And listen'd, the full-flowing river of speech
Came down upon my heart.

"'My own Œnone,


Beautiful-brow'd Œnone, my own soul,
Behold this fruit whose gleaming rind ingrav'n70
"For the most fair," would seem to award it thine
As lovelier than whatever Oread[187] haunt
The knolls of Ida, loveliest in all grace
Of movement and the charm of married brows.'

"Dear mother Ida, hearken ere I die.75


He prest the blossom of his lips to mine,
And added, 'This was cast upon the board,
When all the full-faced presence of the Gods
Ranged in the halls of Peleus[188]; whereupon
Rose feud, with question unto whom 'twere due:80
But light-foot Iris[189] brought it yester-eve,
Delivering, that to me, by common voice
Elected umpire, Herè[190] comes to-day,
Pallas[191] and Aphroditè,[192] claiming each
This meed of fairest. Thou, within the cave85
Behind yon whispering tuft of oldest pine,
Mayst well behold them unbeheld, unheard
Hear all, and see thy Paris judge of Gods.'

"Dear mother Ida, hearken ere I die.


It was the deep midnoon: one silvery cloud90
Had lost his way between the piney sides
Of this long glen. Then to the bower they came,
Naked they came to that smooth-swarded bower.
And at their feet the crocus brake like fire,
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like