Professional Documents
Culture Documents
Textbook Android Recipes A Problem Solution Approach Dave Smith Ebook All Chapter PDF
Textbook Android Recipes A Problem Solution Approach Dave Smith Ebook All Chapter PDF
https://textbookfull.com/product/raku-recipes-a-problem-solution-
approach-j-j-merelo/
https://textbookfull.com/product/c-recipes-a-problem-solution-
approach-1st-edition-shirish-chavan/
https://textbookfull.com/product/javascript-recipes-a-problem-
solution-approach-1st-edition-russ-ferguson/
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/
https://textbookfull.com/product/java-9-recipes-a-problem-
solution-approach-3rd-edition-josh-juneau/
https://textbookfull.com/product/spring-5-recipes-a-problem-
solution-approach-4th-edition-marten-deinum/
https://textbookfull.com/product/prestashop-recipes-a-problem-
solution-approach-1st-edition-arnaldo-perez-castano-auth/
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
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.
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.
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
■
■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
■
■Chapter 1: Layouts and Views������������������������������������������������������������������������������� 1
1-1. Styling Common Components����������������������������������������������������������������������������������� 1
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 1
Solution�������������������������������������������������������������������������������������������������������������������������������������������������� 1
How It Works������������������������������������������������������������������������������������������������������������������������������������������� 1
v
■ Contents
vi
■ Contents
Summary������������������������������������������������������������������������������������������������������������������������ 84
■
■Chapter 2: User Interaction Recipes�������������������������������������������������������������������� 85
2-1. Leveraging the Action Bar��������������������������������������������������������������������������������������� 85
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 85
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 85
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 86
vii
■ Contents
viii
■ Contents
ix
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 188
■
■Chapter 3: Communications and Networking���������������������������������������������������� 189
3-1. Displaying Web Information���������������������������������������������������������������������������������� 189
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 189
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 189
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 189
x
■ Contents
xi
■ Contents
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
xii
■ Contents
xiii
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 366
■
■Chapter 5: Persisting Data��������������������������������������������������������������������������������� 367
5-1. Making a Preference Screen�������������������������������������������������������������������������������� 367
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 367
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 367
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 367
xiv
■ Contents
xv
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 442
■
■Chapter 6: Interacting with the System������������������������������������������������������������� 443
6-1. Notifying from the Background����������������������������������������������������������������������������� 443
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 443
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 443
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 443
xvi
■ Contents
xvii
■ Contents
xviii
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 584
■
■Chapter 7: Graphics and Drawing���������������������������������������������������������������������� 585
7-1. Creating Drawables as Backgrounds�������������������������������������������������������������������� 585
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 585
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 585
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 586
xix
■ Contents
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
RenderScript���������������������������������������������������������������������������������������������������������������� 674
Using the RenderScript Support Package������������������������������������������������������������������������������������������ 675
xx
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 697
Index��������������������������������������������������������������������������������������������������������������������� 699
xxi
About the Authors
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
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
xxix
■ Introduction
xxx
CHAPTER 1
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.
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.
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.
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
Another random document with
no related content on Scribd:
OR IMPLIED, INCLUDING BUT NOT LIMITED TO
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR
ANY PURPOSE.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.