Head First Android Development A Brain Friendly Guide, 2nd Edition

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 930

DownloadHead First Android Development A Brain-Friendly Guide, 2nd Edition
Open PDF In BrowserView PDF
Android
Development

d n
2n itio
Ed

Head First

A Brain-Friendly Guide

Put fragments
under the
microscope

Learn how
Constraint
Layouts can
change your life

Avoid
embarrassing
activities

Create
out-of-this-world
services

Find your way
with Android's
Location Services

Fool around
in the Design
Support Library

Dawn Griffiths & David
GriffiRobson
ths & Eric Freeman
Elisabeth

Head First
Android Development
Wouldn’t it be dreamy if there
were a book on developing Android
apps that was easier to understand
than the space shuttle flight manual? I
guess it’s just a fantasy…

Dawn Griffiths
David Griffiths

Boston

Head First Android Development
by Dawn Griffiths and David Griffiths
Copyright © 2017 David Griffiths and Dawn Griffiths. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/institutional
sales department: (800) 998-9938 or corporate@oreilly.com.

Series Creators:		

Kathy Sierra, Bert Bates

Editor:			Dawn Schanafelt
Cover Designer:		

Karen Montgomery

Production Editor:		

Kristen Brown

Proofreader:			

Rachel Monaghan

Indexer:			 Angela Howard
Page Viewers:

Mum and Dad, Rob and Lorraine

Printing History:
June 2015: First Edition.
August 2017: Second Edition

Mum and Dad

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations,
Head First Android Development, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
No kittens were harmed in the making of this book, but several pizzas were eaten.

ISBN: 978-1-491-97405-6
[M]									

Rob and
Lorraine

To our friends and family. Thank you so
much for all your love and support.

the authors

Authors of He ad First Android Development

s

David Griffith

Dawn Griffiths
Dawn Griffiths started life as a mathematician at

a top UK university, where she was awarded a firstclass honors degree in mathematics. She went on to
pursue a career in software development and has over
20 years’ experience working in the IT industry.

Before writing Head First Android Development, Dawn
wrote three other Head First books (Head First Statistics,
Head First 2D Geometry, and Head First C). She also
created the video course The Agile Sketchpad with her
husband, David, to teach key concepts and techniques
in a way that keeps your brain active and engaged.
When Dawn’s not working on Head First books or
creating videos, you’ll find her honing her Tai Chi skills,
reading, running, making bobbin lace, or cooking. She
particularly enjoys spending time with her wonderful
husband, David.

David Griffiths began programming at age 12,

when he saw a documentary on the work of Seymour
Papert. At age 15, he wrote an implementation of
Papert’s computer language LOGO. After studying
pure mathematics at university, he began writing code
for computers and magazine articles for humans. He’s
worked as an Agile coach, a developer, and a garage
attendant, but not in that order. He can write code
in over 10 languages and prose in just one, and when
not writing, coding, or coaching, he spends much
of his spare time traveling with his lovely wife—and
coauthor—Dawn.

Before writing Head First Android Development, David
wrote three other Head First books—Head First Rails,
Head First Programming, and Head First C—and created
The Agile Sketchpad video course with Dawn.
You can follow us on Twitter at https://twitter.com/
HeadFirstDroid and visit the book’s website at https://
tinyurl.com/HeadFirstAndroid.

iv

table of contents

Table of Contents (Summary)
Intro

xxix

1

Getting Started: Diving in

1

2

Building Interactive Apps: Apps that do something

37

3

Multiple Activities and Intents: State your intent

77

4

The Activity Lifecycle: Being an activity

119

5

Views and View Groups Enjoy the view

169

6

Constraint Layouts: Put things in their place

221

7

List views and Adapters: Getting organized

247
289

8

Support Libraries and App Bars: Taking shortcuts

9

Fragments: Make it modular

339

10

Fragments for Larger Interfaces: Different size, different interface

393

11

Dynamic Fragments: Nesting fragments

433

12

Design Support Library: Swipe right

481

13

Recycler Views and Card Views: Get recycling

537

14

Navigation Drawers: Going places

579

15

SQLite Databases: Fire up the database

621

16

Basic cursors: Getting data out

657

17

Cursors and AsyncTasks: Staying in the background

693

18

Started Services: At your service

739

19

Bound Services and Permissions: Bound together

767

i

Relative and Grid Layouts: Meet the relatives

817

ii

Gradle: The Gradle build tool

833

iii

ART: The Android Runtime

841

iv

ADB: The Android debug bridge

849

v

The Android Emulator: Speeding things up

857

vi

Leftovers: The top ten things (we didn’t cover)

861

v

table of contents

Table of Contents (the real thing)
Intro
Your brain on Android. Here

you are trying to learn something, while

here your brain is, doing you a favor by making sure the learning doesn’t stick. Your
brain’s thinking, “Better leave room for more important things, like which wild
animals to avoid and whether naked snowboarding is a bad idea.” So how do you
trick your brain into thinking that your life depends on knowing how to develop
Android apps?

I wonder how
I can trick my brain
into remembering
this stuff…

vi

Authors of Head First Android Development

iv

Who is this book for?

xxx

We know what you’re thinking

xxxi

We know what your brain is thinking

xxxi

Metacognition: thinking about thinking

xxxiii

Here’s what WE did

xxxiv

Read me

xxxvi

The technical review team

xxxviii

Acknowledgments

xxxix

Safari® Books Online

xl

table of contents

1

getting started
Diving In
Android has taken the world by storm.
Everybody wants a smartphone or tablet, and Android devices are hugely popular. In
this book, we’ll teach you how to develop your own apps, and we’ll start by getting
you to build a basic app and run it on an Android Virtual Device. Along the way, you’ll
meet some of the basic components of all Android apps, such as activities and
layouts. All you need is a little Java know-how...

DK
dS

An

oi
dr

Welcome to Androidville

2

The Android platform dissected

3

Here’s what we’re going to do

4

Your development environment

5

Install Android Studio

6

Build a basic app

7

How to build the app

8

Activities and layouts from 50,000 feet

12

How to build the app (continued)

13

You’ve just created your first Android app

15

Android Studio creates a complete folder structure for you

16

Useful files in your project

17

Edit code with the Android Studio editors

18

Run the app in the Android emulator

23

Creating an Android Virtual Device

24

Run the app in the emulator

27

You can watch progress in the console

28

What just happened?

30

Refining the app

31

What’s in the layout?

32

activity_main.xml has two elements

33

Update the text displayed in the layout

34

Take the app for a test drive

35

Your Android Toolbox

36





Device




Activity

Layout

vii

table of contents

2

building interactive apps
Apps That Do Something
Most apps need to respond to the user in some way.
In this chapter, you’ll see how you can make your apps a bit more interactive. You’ll
learn how to get your app to do something in response to the user, and how to get
your activity and layout talking to each other like best buddies. Along the way, we’ll
take you a bit deeper into how Android actually works by introducing you to R, the
hidden gem that glues everything together.




Layout

BeerExpert

viii

38
40

We’ve created a default activity and layout

41



A closer look at the design editor

42



Add a button using the design editor

43

activity_find_beer.xml has a new button

44

A closer look at the layout code

45

strings.xml

Activity

Let’s build a Beer Adviser app
Create the project

Let’s take the app for a test drive

49

Hardcoding text makes localization hard

50

Create the String resource

51

Use the String resource in your layout

52

The code for activity_find_beer.xml

53

Add values to the spinner

56

Add the string-array to strings.xml

57

Test drive the spinner

58

We need to make the button do something

59

Make the button call a method

60

What activity code looks like

61

Add an onClickFindBeer() method to the activity

62

onClickFindBeer() needs to do something

63

Once you have a View, you can access its methods

64

Update the activity code

65

The first version of the activity

67

What the code does

68

Build the custom Java class

70

What happens when you run the code

74

Test drive your app

75

Your Android Toolbox

76

table of contents

3

multiple activities and intents
State Your Intent
Most apps need more than one activity.
So far we’ve just looked at single-activity apps, which is fine for simple apps. But when
things get more complicated, just having the one activity won’t cut it. We’re going to
show you how to build apps with multiple activities, and how you can get your apps
talking to each other using intents. We’ll also look at how you can use intents to go
beyond the boundaries of your app and make activities in other apps on your
device perform actions. Things are about to get a whole lot more powerful...
Apps can contain more than one activity

Intent

To: AnotherActivity

Hey, user. Which
activity do you want
to use this time?
CreateMessageActivity

78

Here’s the app structure

79

Get started: create the project

79

Update the layout

80

Create the second activity and layout

82

Welcome to the Android manifest file

84

An intent is a type of message

86

What happens when you run the app

88

Pass text to a second activity

90

Update the text view properties

91

putExtra() puts extra info in an intent

92

Update the CreateMessageActivity code

95

Get ReceiveMessageActivity to use the information in the intent

96

What happens when the user clicks the Send Message button

97

We can change the app to send messages to other people

98

How Android apps work

99

Create an intent that specifies an action

101

Change the intent to use an action

102

How Android uses the intent filter

106

What if you ALWAYS want your users to choose an activity?

112

What happens when you call createChooser()

113

Change the code to create a chooser

115

Your Android Toolbox

118

Android
User

ix

table of contents

4

the activity lifecycle
Being an Activity
Activities form the foundation of every Android app.
So far you’ve seen how to create activities, and made one activity start another
using an intent. But what’s really going on beneath the hood? In this chapter,
we’re going to dig a little deeper into the activity lifecycle. What happens when
an activity is created and destroyed? Which methods get called when an activity
is made visible and appears in the foreground, and which get called when the
activity loses the focus and is hidden? And how do you save and restore your

Activity
launched

activity’s state? Read on to find out.

onCreate()

onStart()

onResume()

Activity
running

onPause()

onStop()

onDestroy()

Activity
destroyed
x

onRestart()

How do activities really work?

120

The Stopwatch app

122

Add String resources

123

How the activity code will work

125

Add code for the buttons

126

The runTimer() method

127

The full runTimer() code

129

The full StopwatchActivity code

130

Rotating the screen changes the device configuration

136

The states of an activity

137

The activity lifecycle: from create to destroy

138

The updated StopwatchActivity code

142

What happens when you run the app

143

There’s more to an activity’s life than create and destroy

146

The updated StopwatchActivity code

151

What happens when you run the app

152

What if an app is only partially visible?

154

The activity lifecycle: the foreground lifetime

155

Stop the stopwatch if the activity’s paused

158

Implement the onPause() and onResume() methods

159

The complete StopwatchActivity code

160

What happens when you run the app

163

Your handy guide to the lifecycle methods

167

Your Android Toolbox

168

table of contents

5

views and view groups
Enjoy the View
You’ve seen how to arrange GUI components using a linear
layout, but so far we’ve only scratched the surface. 
In this chapter we’ll look a little deeper and show you how linear layouts really work.
We’ll introduce you to the frame layout, a simple layout used to stack views, and we’ll
also take a tour of the main GUI components and how you use them. By the end of
the chapter, you’ll see that even though they all look a little different, all layouts and GUI
components have more in common than you might think.
Your user interface is made up of layouts and GUI components

Frame layouts let your views overlap one
another. This is useful for displaying
text on top of images, for example.

The linear layout



ViewGroup

layout.xml

170

LinearLayout displays views in a single row or column

171

Add a dimension resource file for consistent padding across layouts

174

Use margins to add distance between views

176

Let’s change up a basic linear layout

177

Make a view streeeeetch by adding weight

179

Values you can use with the android:gravity attribute

183

The full linear layout code

186

Frame layouts stack their views

188

Add an image to your project

189

The full code to nest a layout

192

FrameLayout: a summary

193

Playing with views

201

Editable text view

202

Toggle button

204

Switch

205

Checkboxes

206

Radio buttons

208

Spinner

210

Image view

211

Adding images to buttons

213

Scroll views

215

Toasts

216

Your Android Toolbox

220

The editable
text field

The button
View

View

xi

table of contents

6

constraint layouts
Put Things in Their Place
Let’s face it, you need to know how to create great layouts. 
If you’re building apps you want people to use, you need to make sure they look
exactly the way you want. So far you’ve seen how to use linear and frame layouts, but
what if your design is more complex? To deal with this, we’ll introduce you to Android’s
new constraint layout, a type of layout you build visually using a blueprint. We’ll
show you how constraints let you position and size your views, irrespective of screen
size and orientation. Finally, you’ll find out how to save time by making Android Studio
infer and add constraints on your behalf.
Nested layouts can be inefficient

This time, the Infer
Constraints button
added constraints to
our new EditText.

xii

222

Introducing the Constraint Layout

223

Make sure your project includes the Constraint Layout Library

224

Add the String resources to strings.xml

225

Use the blueprint tool

226

Position views using constraints

227

Add a vertical constraint

228

Changes to the blueprint are reflected in the XML

229

How to center views

230

Adjust a view’s position by updating its bias

231

How to change a view’s size

232

How to align views

238

Let’s build a real layout

239

First, add the top line of views

240

The Infer Constraints feature guesses which constraints to add

241

Add the next line to the blueprint...

242

Finally, add a view for the message

243

Test drive the app

244

Your Android Toolbox

245

table of contents

7

list views and adapters
Getting Organized
Want to know how best to structure your Android app?
You’ve learned about some of the basic building blocks that are used to create apps,
and now it’s time to get organized. In this chapter, we’ll show you how you can take
a bunch of ideas and structure them into an awesome app. You’ll learn how lists
of data can form the core part of your app design, and how linking them together
can create a powerful and easy-to-use app. Along the way, you’ll get your first
glimpse of using event listeners and adapters to make your app more dynamic.

Display a
start screen
with a list of
options.

Display a list
of the drinks
we sell.

Show details
of each
drink.

This is our array.
Drink.
drinks

Every app starts with ideas

248

Use list views to navigate to data

251

The drink detail activity

253

The Starbuzz app structure

254

The Drink class

256

The top-level layout contains an image and a list

258

The full top-level layout code

260

Get list views to respond to clicks with a listener

261

Set the listener to the list view

262

A category activity displays the data for a single category

267

Update activity_drink_category.xml

268

For nonstatic data, use an adapter

269

Connect list views to arrays with an array adapter

270

Add the array adapter to DrinkCategoryActivity

271

App review: where we are

274

How we handled clicks in TopLevelActivity

276

The full DrinkCategoryActivity code

278

Update the views with the data

281

The DrinkActivity code

283

What happens when you run the app

284

Your Android Toolbox

288

We’ll create an array adapter to
bind our list view to our array.
Array
Adapter

This is our list view.
ListView

xiii

table of contents

8

support libraries and app bars
Taking Shortcuts
Everybody likes a shortcut.
And in this chapter you’ll see how to add shortcuts to your apps using app bars. We’ll
show you how to start activities by adding actions to your app bar, how to share content
with other apps using the share action provider, and how to navigate up your app’s
hierarchy by implementing the app bar’s Up button. Along the way we’ll introduce you
to the powerful Android Support Libraries, which are key to making your apps look
fresh on older versions of Android.
Great apps have a clear structure

290

Different types of navigation

291

onCreate(Bundle)

Add an app bar by applying a theme

293

onStart()

Create the Pizza app

295

onRestart()

Add the v7 AppCompat Support Library

296

onResume()

AndroidManifest.xml can change your app bar’s appearance

299

onPause()

How to apply a theme

300

onStop()

Define styles in a style resource file

301

onDestroy()

Customize the look of your app

303

Define colors in a color resource file

304

The code for activity_main.xml

305

ActionBar vs. Toolbar

306

Include the toolbar in the activity’s layout

312

Add actions to the app bar

315

Activity

onSaveInstanceState()
FragmentActivity

AppCompatActivity

YourActivity

Change the app bar text by adding a label

318

The code for AndroidManifest.xml

319

Control the action’s appearance

322

The full MainActivity.java code

325

Enable Up navigation

327

onCreate(Bundle)

Share content on the app bar

331

yourMethod()

Add a share action provider to menu_main.xml

332

Specify the content with an intent

333

The full MainActivity.java code

334

Your Android Toolbox

337

Intent

xiv

ShareActionProvider

ACTION_SEND
type: “text/plain”
messageText: ”Hi!”

AppActivity

table of contents

9

fragments
Make It Modular
You’ve seen how to create apps that work in the same way
no matter what device they’re running on.
But what if you want your app to look and behave differently depending on whether
it’s running on a phone or a tablet? In this case you need fragments, modular code
components that can be reused by different activities. We’ll show you how to create
basic fragments and list fragments, how to add them to your activities, and how to
get your fragments and activities to communicate with one another.
Your app needs to look great on ALL devices

340

Your app may need to behave differently too

341

Fragments allow you to reuse code

342

The phone version of the app

343

Create the project and activities

345

Add a button to MainActivity’s layout

346

How to add a fragment to your project

348



The fragment’s onCreateView() method

350



Add a fragment to an activity’s layout

352

Get the fragment and activity to interact

359

The Workout class

360

Hmmm, a
 element.
I need to know what
goes here.

activity_detail

A list fragment
comes complete with
its own list view so
you don’t need to
add it yourself. You
just need to provide
the list fragment
with data.

onCreate()

MainActivity

Pass the workout ID to the fragment

361

Get the activity to set the workout ID

363

The fragment lifecycle

365

Set the view’s values in the fragment’s onStart() method

367

How to create a list fragment

374

The updated WorkoutListFragment code

377

The code for activity_main.xml

381

Connect the list to the detail

384

The code for WorkoutListFragment.java

387

MainActivity needs to implement the interface

388

DetailActivity needs to pass the ID to WorkoutDetailFragment

389

Your Android Toolbox

392

Fragment Manager

WorkoutDetail
Fragment

xv

table of contents

10

fragments for larger interfaces
Different Size, Different Interface
So far we’ve only run our apps on devices with a small
screen.But what if your users have tablets? In this chapter you’ll see how to create
flexible user interfaces by making your app look and behave differently depending
on the device it’s running on. We’ll show you how to control the behavior of your app
when you press the Back button by introducing you to the back stack and fragment
transactions. Finally, you’ll find out how to save and restore the state of your
fragment.

The device screen's
large, so I'll use the large
version of the layout.

layout-large



Android

activity_main.xml

I'm committed.
Make it so!
MainActivity

FragmentTransaction

xvi

The Workout app looks the same on a phone and a tablet

394

Designing for larger interfaces

395

The phone version of the app

396

The tablet version of the app

397

Create a tablet AVD

399

Put screen-specific resources in screen-specific folders

402

The different folder options

403

Tablets use layouts in the layout-large folder

408

What the updated code does

410

We need to change the itemClicked() code

412

You want fragments to work with the Back button

413

Welcome to the back stack

414

Back stack transactions don’t have to be activities

415

Use a frame layout to replace fragments programmatically

416

Use layout differences to tell which layout the device is using

417

The revised MainActivity code

418

Using fragment transactions

419

The updated MainActivity code

423

Rotating the tablet breaks the app

427

Saving an activity’s state (revisited)

428

The updated code for WorkoutDetailFragment.java

430

Your Android Toolbox

432

Tablet

table of contents

11

dynamic fragments
Nesting Fragments
So far you’ve seen how to create and use static
fragments.But what if you want your fragments to be more dynamic?
Dynamic fragments have a lot in common with dynamic activities, but there are
crucial differences you need to be able to deal with. In this chapter you’ll see how
to convert dynamic activities into working dynamic fragments. You’ll find out
how to use fragment transactions to help maintain your fragment state. Finally,
you’ll discover how to nest one fragment inside another, and how the child
fragment manager helps you control unruly back stack behavior.

Whenever I see
android:onClick, I
assume it’s all about
me. My methods run,
not the fragment’s.

Adding dynamic fragments

434

The new version of the app

436

Create TempActivity

437

TempActivity needs to extend AppCompatActivity

438

The StopwatchFragment.java code

444

The StopwatchFragment layout

447

Add StopwatchFragment to TempActivity’s layout

449

The onClick attribute calls methods in the activity, not the fragment 452

Activity

Attach the OnClickListener to the buttons

457

The StopwatchFragment code

458

Rotating the device resets the stopwatch

462

Use  for static fragments...

463

Change activity_temp.xml to use a FrameLayout

464

The full code for TempActivity.java

467

Add the stopwatch to WorkoutDetailFragment

469

The full WorkoutDetailFragment.java code

476

Your Android Toolbox

480

I display workout
details, and I also
display the stopwatch.

The transaction to add
StopwatchFragment is nested
inside the transaction to add
WorkoutDetailFragment.

Workout Details

Stopwatch
xvii

table of contents

12

design support library
Swipe Right
Ever wondered how to develop apps with a rich, slick UI?
With the release of the Android Design Support Library, it became much easier
to create apps with an intuitive UI. In this chapter, we’ll show you around some of
the highlights. You’ll see how to add tabs so that your users can navigate around
your app more easily. You’ll discover how to animate your toolbars so that
they can collapse or scroll on a whim. You’ll find out how to add floating action
buttons for common user actions. Finally, we’ll introduce you to snackbars, a way
of displaying short, informative messages to the user that they can interact with.

We’ll get the toolbar to scroll when the
user scrolls the content in TopFragment.

We’ll add this scrollable
content to TopFragment.

xviii

The Bits and Pizzas app revisited

482

The app structure

483

Use a view pager to swipe through fragments

489

Add a view pager to MainActivity’s layout

490

Tell a view pager about its pages using a fragment pager adapter

491

The code for our fragment pager adapter

492

The full code for MainActivity.java

494

Add tab navigation to MainActivity

498

How to add tabs to your layout

499

Link the tab layout to the view pager

501

The full code for MainActivity.java

502

The Design Support Library helps you implement material design

506

Make the toolbar respond to scrolls

508

Add a coordinator layout to MainActivity’s layout

509

How to coordinate scroll behavior

510

Add scrollable content to TopFragment

512

The full code for fragment_top.xml

515

Add a collapsing toolbar to OrderActivity

517

How to create a plain collapsing toolbar

518

How to add an image to a collapsing toolbar

523

The updated code for activity_order.xml

524

FABs and snackbars

526

The updated code for activity_order.xml

528

The full code for OrderActivity.java

533

Your Android Toolbox

535

table of contents

13

recycler views and card views
Get Recycling
You’ve already seen how the humble list view is a key part of
most apps.But compared to some of the material design components we’ve seen,
it’s somewhat plain. In this chapter, we’ll introduce you to the recycler view, a more
advanced type of list that gives you loads more flexibility and fits in with the material
design ethos. You’ll see how to create adapters tailored to your data, and how to
completely change the look of your list with just two lines of code. We’ll also show you
how to use card views to give your data a 3D material design appearance.

ViewHolder
CardView

Each of our ViewHolders will
contain a CardView. We created
the layout for this CardView
earlier in the chapter.

There’s still work to do on the Bits and Pizzas app

538

Recycler views from 10,000 feet

539

Add the pizza data

541

Display the pizza data in a card

542

How to create a card view

543

The full card_captioned_image.xml code

544

Add a recycler view adapter

546

Define the adapter’s view holder

548

Override the onCreateViewHolder() method

549

Add the data to the card views

550

The full code for CaptionedImagesAdapter.java

551

Create the recycler view

553

Add the RecyclerView to PizzaFragment’s layout

554

The full PizzaFragment.java code

555

A recycler view uses a layout manager to arrange its views

556

Specify the layout manager

557

The full PizzaFragment.java code

558

Make the recycler view respond to clicks

566

Create PizzaDetailActivity

567

The code for PizzaDetailActivity.java

569

Get a recycler view to respond to clicks

570

You can listen for view events from the adapter

571

Keep your adapters reusable

572

Add the interface to the adapter

573

Implement the listener in PizzaFragment.java

575

Your Android Toolbox

578

xix

table of contents

14

navigation drawers
Going Places
You’ve already seen how tabs help users navigate your apps.
But if you need a large number of them, or want to split them into sections, the navigation
drawer is your new BFF. In this chapter, we’ll show you how to create a navigation drawer
that slides out from the side of your activity at a single touch. You’ll learn how to give it a
header using a navigation view, and provide it with a structured set of menu items
to take the user to all the major hubs of your app. Finally, you’ll discover how to set up a
navigation view listener so that the drawer responds to the slightest touch and swipe.

This is the CatChat app.

xx

Tab layouts allow easy navigation...

580

We’re going to create a navigation drawer for a new email app

581

Navigation drawers deconstructed

582

Create the CatChat project

584

Create InboxFragment

585

Create DraftsFragment

586

Create SentItemsFragment

587

Create TrashFragment

588

Create a toolbar layout

589

Update the app’s theme

590

Create HelpActivity

591

Create FeedbackActivity

592

Create the navigation drawer’s header

594

The full nav_header.xml code

595

How to group items together

598

Add the support section as a submenu

600

The full menu_nav.xml code

601

How to create a navigation drawer

602

The full code for activity_main.xml

603

Add InboxFragment to MainActivity’s frame layout

604

Add a drawer toggle

607

Respond to the user clicking items in the drawer

608

Implement the onNavigationItemSelected() method

609

Close the drawer when the user presses the Back button

614

The full MainActivity.java code

615

Your Android Toolbox

619

table of contents

15

SQLite databases
Fire Up the Database
If you’re recording high scores or saving tweets, your app
will need to store data. A nd on Android you usually keep your data safe
inside a SQLite database. In this chapter, we’ll show you how to create a database,
add tables to it, and prepopulate it with data, all with the help of the friendly SQLite
helper. You’ll then see how you can cleanly roll out upgrades to your database
structure, and how to downgrade it if you need to undo any changes.

Your database,
sir. Will that
be all?

onCreate()

SQLite helper

DRINK
Name: “starbuzz”
Version: 1

SQLite database

Back to Starbuzz

622

Android uses SQLite databases to persist data

623

Android comes with SQLite classes

624

The current Starbuzz app structure

625

Let’s change the app to use a database

626

The SQLite helper manages your database

627

Create the SQLite helper

628

Inside a SQLite database

630

You create tables using Structured Query Language (SQL)

631

Insert data using the insert() method

632

Insert multiple records

633

The StarbuzzDatabaseHelper code

634

What the SQLite helper code does

635

What if you need to make changes to the database?

636

SQLite databases have a version number

637

What happens when you change the version number

638

Upgrade your database with onUpgrade()

640

Downgrade your database with onDowngrade()

641

Let’s upgrade the database

642

Upgrade an existing database

645

Update records with the update() method

646

Apply conditions to multiple columns

647

Change the database structure

649

Delete tables by dropping them

650

The full SQLite helper code

651

Your Android Toolbox

656

xxi

table of contents

16

basic cursors
Getting Data Out
So how do you connect your app to a SQLite database?
 o far you’ve seen how to create a SQLite database using a SQLite helper. The
S
next step is to get your activities to access it. In this chapter, we’ll focus on how you
read data from a database. You’ll find out how to use cursors to get data from the
database. You’ll see how to navigate cursors, and how to get access to their data.
Finally, you’ll discover how to use cursor adapters to bind cursors to list views.
The story so far...

658

The new Starbuzz app structure

659

What we’ll do to change DrinkActivity to use the Starbuzz database 660

Hey, Cursor, I need
more... Cursor? Hey,
buddy, are you there?

Cursor
CursorAdapter

If you close the cursor too
soon, the cursor adapter
won’t be able to get more
data from the cursor.

xxii

The current DrinkActivity code

661

Get a reference to the database

662

Get data from the database with a cursor

663

Return all the records from a table

664

Return records in a particular order

665

Return selected records

666

The DrinkActivity code so far

669

To read a record from a cursor, you first need to navigate to it

670

Navigate cursors

671

Get cursor values

672

The DrinkActivity code

673

What we’ve done so far

675

The current DrinkCategoryActivity code

677

Get a reference to the Starbuzz database...

678

How do we replace the array data in the list view?

679

A simple cursor adapter maps cursor data to views

680

How to use a simple cursor adapter

681

Close the cursor and database

682

The story continues

683

The revised code for DrinkCategoryActivity

688

The DrinkCategoryActivity code (continued)

689

Your Android Toolbox

691

table of contents

17

cursors and asynctasks
Staying in the Background
In most apps, you’ll need your app to update its data.
 o far you’ve seen how to create apps that read data from a SQLite database. But
S
what if you want to update the app’s data? In this chapter you’ll see how to get your
app to respond to user input and update values in the database. You’ll also find
out how to refresh the data that’s displayed once it’s been updated. Finally, you’ll

onPreExecute

see how writing efficient multithreaded code with AsyncTasks will keep your app
speedy.

doInBackground

onProgressUpdate

onPostExecute

We want our Starbuzz app to update database data

694

Add a checkbox to DrinkActivity’s layout

696

Display the value of the FAVORITE column

697

Respond to clicks to update the database

698

The full DrinkActivity.java code

701

Display favorites in TopLevelActivity

705

Refactor TopLevelActivity.java

707

The new TopLevelActivity.java code

710

Change the cursor with changeCursor()

715

What code goes on which thread?

723

AsyncTask performs asynchronous tasks

724

The onPreExecute() method

725

The doInBackground() method

726

The onProgressUpdate() method

727

The onPostExecute() method

728

The AsyncTask class parameters

729

The full UpdateDrinkTask class

730

The full DrinkActivity.java code

732

A summary of the AsyncTask steps

737

Your Android Toolbox

737

Latte
Cappuccino
Filter

ListView

CursorAdapter

Cursor

Database

xxiii

table of contents

started services

18

At Your Service
There are some operations you want to keep on running,
irrespective of which app has the focus. If you start downloading a
file, for instance, you don’t want the download to stop when you switch to another app. In
this chapter we’ll introduce you to started services, components that run operations in the
background. You’ll see how to create a started service using the IntentService class,
and find out how its lifecycle fits in with that of an activity. Along the way, you’ll discover how

Service created

to log messages, and keep users informed using Android’s built-in notification service.

onCreate()
onStartCommand()
onHandleIntent()

Service running

onDestroy()

Service destroyed



Services work in the background

740

We’ll create a STARTED service

741

Use the IntentService class to create a basic started service

742

How to log messages

743

The full DelayedMessageService code

744

You declare services in AndroidManifest.xml

745

Add a button to activity_main.xml

746

You start a service using startService()

747

The states of a started service

750

The started service lifecycle: from create to destroy

751

Your service inherits the lifecycle methods

752

Android has a built-in notification service

755

We’ll use notifications from the AppCompat Support Library

756

First create a notification builder

757

Issue the notification using the built-in notification service

759

The full code for DelayedMessageService.java

760

Your Android Toolbox

765

MainActivity will use this layout.



activity_main.xml

The activity will pass
text to the service.
xxiv

MainActivity.java

Text

1...2..3...4...5...6...7
...8...9...10... Here’s
the text.

The service will
display the text
after 10 seconds.
DelayedMessageService.java

table of contents

19

bound services and permissions
Bound Together
Started services are great for background operations, but
what if you need a service that’s more interactive? In this
chapter you’ll discover how to create a bound service, a type of service your activity
can interact with. You’ll see how to bind to the service when you need it, and how
to unbind from it when you’re done to save resources. You’ll find out how to use
Android’s Location Services to get location updates from your device GPS. Finally,
you’ll discover how to use Android’s permission model, including handling runtime
permission requests.

Are we nearly
there yet?

OdometerService

Bound services are bound to other components

768

Create a new service

770

Implement a binder

771

Add a getDistance() method to the service

772

Update MainActivity’s layout

773

Create a ServiceConnection

775

Use bindService() to bind the service

778

Use unbindService() to unbind from the service

779

Call OdometerService’s getDistance() method

780

The full MainActivity.java code

781

The states of a bound service

787

Add the AppCompat Support Library

790

Add a location listener to OdometerService

792

Here’s the updated OdometerService code

795

Calculate the distance traveled

796

The full OdometerService.java code

798

Get the app to request permission

802

Check the user’s response to the permission request

805

Add notification code to onRequestPermissionsResults()

809

The full code for MainActivity.java

811

Your Android Toolbox

815

It’s been great having you here in Androidville

816

Intent

Android

Odometer
Service

onBind()

OdometerService

xxv

table of contents

i

relative and grid layouts
Meet the Relatives
There are two more layouts you will often meet in
Androidville. In this book we’ve concentrated on using simple linear and frame
layouts, and introduced you to Android’s new constraint layout. But there are two more
layouts we’d like you to know about: relative layouts and grid layouts. They’ve largely
been superseded by the constraint layout, but we have a soft spot for them, and we
think they’ll stay around for a few more years.

Each of
these
areas is
a cell.

ii

gradle
The Gradle Build Tool
Most Android apps are created using a build tool called
Gradle. Gradle works behind the scenes to find and download libraries, compile
and deploy your code, run tests, clean the grouting, and so on. Most of the time you
might not even realize it’s there because Android Studio provides a graphical interface
to it. Sometimes, however, it’s helpful to dive into Gradle and hack it manually. In this
appendix we’ll introduce you to some of Gradle’s many talents.

xxvi

table of contents

iii

art
The Android Runtime
Ever wonder how Android apps can run on so many kinds of
devices? Android apps run in a virtual machine called the Android runtime (ART),
not the Oracle Java Virtual Machine (JVM). This means that your apps are quicker to
start on small, low-powered devices and run more efficiently. In this appendix, we’ll look
at how ART works.

classes.dex

aapt
.apk

Resources

iv

adb
The Android Debug Bridge
In this book, we’ve focused on using an IDE for all your
Android needs. B ut there are times when using a command-line tool can be plain
useful, like those times when Android Studio can’t see your Android device but you just
know it’s there. In this chapter, we’ll introduce you to the Android Debug Bridge (or
adb), a command-line tool you can use to communicate with the emulator or Android
devices.

adb

adbd

adb command

adb daemon
process

Device

Device

xxvii

table of contents

v

the android emulator
Speeding Things Up
Ever felt like you were spending all your time waiting for the
emulator? There’s no doubt that using the Android emulator is useful. It allows
you to see how your app will run on devices other than the physical ones you have
access to. But at times it can feel a little...sluggish. In this appendix, we’ll explain why
the emulator can seem slow. Even better, we’ll give you a few tips we’ve learned for
speeding it up.

AVD

All the Android Virtual Devices run
on an emulator called QEMU.

vi

AVD

AVD

AVD

AVD

QEMU Emulator

leftovers
The Top Ten Things (we didn’t cover)
Even after all that, there’s still a little more. 
There are just a few more things we think you need to know. We wouldn’t feel right
about ignoring them, and we really wanted to give you a book you’d be able to lift
without extensive training at the local gym. Before you put down the book, read
through these tidbits.

The battery’s
running low, in case
anyone’s interested.

Android

xxviii

1. Distributing your app

862

2. Content providers

863

3. Loaders

864

4. Sync adapters

864

5. Broadcasts

865

6. The WebView class

866

7. Settings

867

8. Animation

868

9. App widgets

869

10. Automated testing

870

the intro

how to use this book

Intro
I can’t believe
they put that in an
Android book.

ning question:
In this section, we answer thein bur
book on Android?”
“So why DID they put that a

you are here 4  xxix

how to use this book

Who is this book for?
If you can answer “yes” to all of these:
1

Do you already know how to program in Java?

2

Do you want to master Android app development, create
the next big thing in software, make a small fortune, and
retire to your own private island?

3

Do you prefer actually doing things and applying the stuff
you learn over listening to someone in a lecture rattle on
for hours on end?

tle
OK, maybe that one’s a litta
got
far-fetched. But you
start somewhere, right?

this book is for you.

Who should probably back away f rom this book?
If you can answer “yes” to any of these:
1

Are you looking for a quick introduction or reference book
to developing Android apps?

2

Would you rather have your toenails pulled out by 15
screaming monkeys than learn something new? Do
you believe an Android book should cover everything,
especially all the obscure stuff you’ll never use, and if it
bores the reader to tears in the process, then so much
the better?

this book is not for you.

[Note from Marketing: this
book is for anyone with a credit
card or a PayPal account]

xxx  intro

the intro

We know what you’re thinking
“How can this be a serious book on developing Android apps?”
“What’s with all the graphics?”
“Can I actually learn it this way?”

We know what your brain is thinking

Your bra
THIS is imin thinks
portant.

Your brain craves novelty. It’s always searching, scanning, waiting for
something unusual. It was built that way, and it helps you stay alive.
So what does your brain do with all the routine, ordinary, normal things
you encounter? Everything it can to stop them from interfering with the
brain’s real job—recording things that matter. It doesn’t bother saving
the boring things; they never make it past the “this is obviously not
important” filter.
How does your brain know what’s important? Suppose you’re out for a day
hike and a tiger jumps in front of you—what happens inside your head and
body?
Great. Only 900
more dull, dry,
boring pages.

Neurons fire. Emotions crank up. Chemicals surge.
And that’s how your brain knows…
This must be important! Don’t forget it!
But imagine you’re at home or in a library. It’s a safe, warm, tiger‑free zone.
You’re studying. Getting ready for an exam. Or trying to learn some tough
3
technical
topic your boss thinks will take a week, ten days at the most.

in thinks
Your bran’t worth
THIS is
saving.

Just one problem. Your brain’s trying to do you a big favor. It’s trying to
make sure that this obviously unimportant content doesn’t clutter up scarce
resources. Resources that are better spent storing the really big things.
Like tigers. Like the danger of fire. Like how you should never have
posted those party photos on your Facebook page. And there’s no
simple way to tell your brain, “Hey brain, thank you very much, but
no matter how dull this book is, and how little I’m registering on the
emotional Richter scale right now, I really do want you to keep this
stuff around.”

you are here 4  xxxi

how to use this book

er as a learner.

t” read
We think of a “Head Firs

n make sure
you have to get it, the
st,
Fir
?
ng
thi
me
so
e to learn
on the latest
So what does it tak
o your head. Based
int
ts
fac
ing
sh
pu
t
learning
It’s not abou
ational psychology,
you don’t forget it.
obiology, and educ
ur
ne
,
ce
ien
sc
e
itiv
on.
research in cogn
what turns your brain
on a page. We know
t
tex
n
tha
re
mo
lot
takes a

ciples:
First lear ning prin
Some of the Head

much
ne, and make learning
morable than words alo
me
re
mo
far
more
s
are
ng
s
age
dies). It also makes thi
Make it visual. Im
recall and transfer stu
in
ent
than on
vem
pro
her
im
rat
89%
hics they relate to,
more effective (up to
or near the gr ap
in
th
wi
s
ated to the
rd
rel
wo
ms
e
th
as likely to solve proble
understandable. Put
rs will be up to twice
rne
lea
and
e,
pag
r
the
the bottom or on ano
t.
ten
con
performed up
ent studies, students
alized style. In rec
on
rs
pe
d
an
l
first-person,
a
na
to the reader, using
Use a conver satio
content spoke directly
the
if
ts
tes
g
casual language.
nin
Use
.
ear
s instead of lec turing
to 40% better on post-l
rie
sto
l
Tel
e.
ton
l
ma
ner-party
her than tak ing a for
ion to: a stimulating din
conversational style rat
uld you pay more attent
wo
ich
Wh
.
sly
iou
ser
Don’t take yourself too
e?
companion, or a lec tur
your neurons,
, unless you actively flex
eply. In other words
de
re
mo
inspired to
ink
and
th
s,
d, engaged, curiou
Get the learner to
der has to be motivate
rea
A
d.
hea
llenges,
r
cha
you
d
in
ns
d for that, you nee
nothing much happe
ate new knowledge. An
ner
ge
and
and
ns,
in
sio
bra
clu
the
con
olve both sides of
solve problems, draw
s, and activities that inv
ion
est
qu
ing
vok
pro
exercises, and thoughtmultiple senses.
rn this, but I can’t
the “I really want to lea
had
all
’ve
We
.
ion
nt
the ordinary,
he reader’s atte
things that are out of
Get—and keep—t
brain pays attention to
r
You
e.
enc
eri
esn’t have to be
exp
do
e”
ic
on
gh, technical top
stay awake past page
ed. Learning a new, tou
ect
exp
un
,
ing
tch
-ca
interesting, strange, eye
ick ly if it’s not.
l learn much more qu
boring. Your brain wil
is largely dependent
remember something
w that your ability to
kno
w
no
We
s.
you feel something.
ion
You remember when
Touch their emot
ut.
abo
e
car
you
at
otions like
t. You remember wh
dog. We’re talking em
on its emotional conten
s about a boy and his
rie
sto
ng
chi
ren
ve a puzzle, learn
sol
rt‑w
hea
e!” that comes when you
No, we’re not talking
rul
“I
of
ling
fee
the
technical than
, “what the…?” , and
ething that “I’m more
surprise, curiosity, fun
realize you know som
or
d,
har
is
nks
thi
e
els
y
something everybod
ering doesn’t.
ine
Eng
m
fro
b
Bo
u”
tho

xxxii  intro

the intro

Me tacognition: thinking about thinking
If you really want to learn, and you want to learn more quickly and more
deeply, pay attention to how you pay attention. Think about how you think.
Learn how you learn.
Most of us did not take courses on metacognition or learning theory when we
were growing up. We were expected to learn, but rarely taught to learn.

I wonder how
I can trick my brain
into remembering
this stuff…

But we assume that if you’re holding this book, you really want to learn how
to develop Android apps. And you probably don’t want to spend a lot of time.
If you want to use what you read in this book, you need to remember what you
read. And for that, you’ve got to understand it. To get the most from this book,
or any book or learning experience, take responsibility for your brain. Your
brain on this content.
The trick is to get your brain to see the new material you’re learning as
Really Important. Crucial to your well‑being. As important as a tiger.
Otherwise, you’re in for a constant battle, with your brain doing its best to
keep the new content from sticking.
So just how DO you get your brain to treat Android
development like it was a hungry tiger?
There’s the slow, tedious way, or the faster, more effective way. The
slow way is about sheer repetition. You obviously know that you are able to learn
and remember even the dullest of topics if you keep pounding the same thing into your
brain. With enough repetition, your brain says, “This doesn’t feel important to him, but
he keeps looking at the same thing over and over and over, so I suppose it must be.”
The faster way is to do anything that increases brain activity, especially different
types of brain activity. The things on the previous page are a big part of the solution,
and they’re all things that have been proven to help your brain work in your favor. For
example, studies show that putting words within the pictures they describe (as opposed to
somewhere else in the page, like a caption or in the body text) causes your brain to try to
makes sense of how the words and picture relate, and this causes more neurons to fire.
More neurons firing = more chances for your brain to get that this is something worth
paying attention to, and possibly recording.
A conversational style helps because people tend to pay more attention when they
perceive that they’re in a conversation, since they’re expected to follow along and hold up
their end. The amazing thing is, your brain doesn’t necessarily care that the “conversation”
is between you and a book! On the other hand, if the writing style is formal and dry, your
brain perceives it the same way you experience being lectured to while sitting in a roomful
of passive attendees. No need to stay awake.
But pictures and conversational style are just the beginning…

you are here 4  xxxiii

how to use this book

Here’s what WE did
We used pictures, because your brain is tuned for visuals, not text. As far as your brain’s
concerned, a picture really is worth a thousand words. And when text and pictures work
together, we embedded the text in the pictures because your brain works more effectively
when the text is within the thing it refers to, as opposed to in a caption or buried in the body
text somewhere.
We used redundancy, saying the same thing in different ways and with different media types,
and multiple senses, to increase the chance that the content gets coded into more than one area
of your brain.
We used concepts and pictures in unexpected ways because your brain is tuned for novelty,
and we used pictures and ideas with at least some emotional content, because your brain
is tuned to pay attention to the biochemistry of emotions. That which causes you to feel
something is more likely to be remembered, even if that feeling is nothing more than a little
humor, surprise, or interest.
We used a personalized, conversational style, because your brain is tuned to pay more
attention when it believes you’re in a conversation than if it thinks you’re passively listening
to a presentation. Your brain does this even when you’re reading.
We included activities, because your brain is tuned to learn and remember more when
you do things than when you read about things. And we made the exercises challenging-yetdoable, because that’s what most people prefer.
We used multiple learning styles, because you might prefer step-by-step procedures, while
someone else wants to understand the big picture first, and someone else just wants to see
an example. But regardless of your own learning preference, everyone benefits from seeing the
same content represented in multiple ways.
We include content for both sides of your brain, because the more of your brain you
engage, the more likely you are to learn and remember, and the longer you can stay focused.
Since working one side of the brain often means giving the other side a chance to rest, you
can be more productive at learning for a longer period of time.
And we included stories and exercises that present more than one point of view,
because your brain is tuned to learn more deeply when it’s forced to make evaluations and
judgments.
We included challenges, with exercises, and by asking questions that don’t always have
a straight answer, because your brain is tuned to learn and remember when it has to work at
something. Think about it—you can’t get your body in shape just by watching people at the
gym. But we did our best to make sure that when you’re working hard, it’s on the right things.
That you’re not spending one extra dendrite processing a hard-to-understand example,
or parsing difficult, jargon-laden, or overly terse text.
We used people. In stories, examples, pictures, and the like, because, well, you’re a person.
And your brain pays more attention to people than it does to things.

xxxiv  intro

the intro

Here’s what YOU can do to bend
your brain into submission

So, we did our part. The rest is up to you. These tips are a
starting point; listen to your brain and figure out what works
for you and what doesn’t. Try new things.

Cut this out and sti
on your refrigerator.ck it
1

Slow down. The more you understand, the
less you have to memorize.

Don’t just read. Stop and think. When the book asks
you a question, don’t just skip to the answer. Imagine
that someone really is asking the question. The
more deeply you force your brain to think, the better
chance you have of learning and remembering.
2

3

4

6

Listen to your brain.

8

Feel something.

Read “There Are No Dumb Questions.”

That means all of them. They’re not optional
sidebars, they’re part of the core content!
Don’t skip them.
Make this the last thing you read before bed.
Or at least the last challenging thing.

Part of the learning (especially the transfer to
long-term memory) happens after you put the book
down. Your brain needs time on its own, to do more
processing. If you put in something new during that
processing time, some of what you just learned will
be lost.
5 Talk about it. Out loud.
Speaking activates a different part of the brain. If
you’re trying to understand something, or increase
your chance of remembering it later, say it out loud.
Better still, try to explain it out loud to someone else.
You’ll learn more quickly, and you might uncover
ideas you hadn’t known were there when you were
reading about it.

Your brain works best in a nice bath of fluid.
Dehydration (which can happen before you ever
feel thirsty) decreases cognitive function.

7

Do the exercises. Write your own notes.

We put them in, but if we did them for you, that
would be like having someone else do your workouts
for you. And don’t just look at the exercises. Use a
pencil. There’s plenty of evidence that physical
activity while learning can increase the learning.

Drink water. Lots of it.

9

Pay attention to whether your brain is getting
overloaded. If you find yourself starting to skim
the surface or forget what you just read, it’s time
for a break. Once you go past a certain point, you
won’t learn faster by trying to shove more in, and
you might even hurt the process.

Your brain needs to know that this matters. Get
involved with the stories. Make up your own
captions for the photos. Groaning over a bad joke
is still better than feeling nothing at all.
Write a lot of code!

There’s only one way to learn to develop Android
apps: write a lot of code. And that’s what you’re
going to do throughout this book. Coding is a skill,
and the only way to get good at it is to practice.
We’re going to give you a lot of practice: every
chapter has exercises that pose a problem for you
to solve. Don’t just skip over them—a lot of the
learning happens when you solve the exercises. We
included a solution to each exercise—don’t be afraid
to peek at the solution if you get stuck! (It’s easy
to get snagged on something small.) But try to solve
the problem before you look at the solution. And
definitely get it working before you move on to the
next part of the book.

you are here 4  xxxv

how to use this book

Re ad me
This is a learning experience, not a reference book. We deliberately stripped out everything
that might get in the way of learning whatever it is we’re working on at that point in the
book. And the first time through, you need to begin at the beginning, because the book
makes assumptions about what you’ve already seen and learned.
We assume you’re new to Android, but not to Java.
We’re going to be building Android apps using a combination of Java and XML. We
assume that you’re familiar with the Java prorgamming language. If you’ve never done any
Java programming at all, then you might want to read Head First Java before you start on
this one.
We start off by building an app in the very first chapter.
Believe it or not, even if you’ve never developed for Android before, you can jump right
in and start building apps. You’ll also learn your way around Android Studio, the official
IDE for Android development.
The examples are designed for learning.
As you work through the book, you’ll build a number of different apps. Some of these are
very small so you can focus on a specific part of Android. Other apps are larger so you can
see how different components fit together. We won’t complete every part of every app, but
feel free to experiment and finish them yourself. It’s all part of the learning experience. The
source code for all the apps is here: https://tinyurl.com/HeadFirstAndroid.
The activities are NOT optional.
The exercises and activities are not add-ons; they’re part of the core content of the book.
Some of them are to help with memory, some are for understanding, and some will help
you apply what you’ve learned. Don’t skip the exercises.

xxxvi  intro

the intro

The redundancy is intentional and important.
One distinct difference in a Head First book is that we want you to really get it. And we
want you to finish the book remembering what you’ve learned. Most reference books
don’t have retention and recall as a goal, but this book is about learning, so you’ll see
some of the same concepts come up more than once.
The Brain Power exercises don’t have answers.
For some of them, there is no right answer, and for others, part of the learning
experience of the Brain Power activities is for you to decide if and when your answers
are right. In some of the Brain Power exercises, you will find hints to point you in the
right direction.

you are here 4  xxxvii

the review team

The technical revie w te am
Andy

Jacqui

Technical reviewers:
Andy Parker is currently working as a development
manager, but has been a research physicist, teacher,
designer, reviewer, and team leader at various points
in his career. Through all of his roles, he has never lost
his passion for creating top quality, well-designed, and
well-engineered software. Nowadays, he spends most
of his time managing great Agile teams and passing on
his wide range of experience to the next generation of
developers.

xxxviii  intro

Jacqui Cope started coding to avoid school netball
practice. Since then she has gathered 30 years’
experience working with a variety of financial software
systems, from coding in COBOL to test management.
Recently she has gained her MSc in Computer
Security and has moved into software Quality
Assurance in the higher education sector.
In her spare time, Jacqui likes to cook, walk in the
countryside, and watch Doctor Who from behind the
sofa.

the intro

Acknowledgments
Our editor:
Heartfelt thanks to our wonderful editor Dawn Schanafelt for
picking up the reins on the second edition. She has truly been
amazing, and a delight to work with. She made us feel valued
and supported every step of the way, and gave us invaluable
feedback and insight exactly when we needed it. We’ve
appreciated all the many times she told us our sentences had all
the right words, but not necessarily in the right order.

Dawn Schanafelt

Thanks also to Bert Bates for teaching us to throw away the
old rulebook and for letting us into his brain.
The O’Reilly team:
A big thank you goes to Mike Hendrickson for having confidence in us and
asking us to write the first edition of the book; Heather Scherer for her behindthe-scenes organization skills and herding; the early release team for making
early versions of the book available for download; and the design team for all
their extra help. Finally, thanks go to the production team for expertly steering
the book through the production process and for working so hard behind the
scenes.
Family, friends, and colleagues:
Writing a Head First book is a rollercoaster of a ride, even when it’s a second
edition, and this one’s been no exception. We’ve truly valued the kindness and
support of our family and friends along the way. Special thanks go to Ian, Steve,
Colin, Angela, Paul B, Chris, Michael, Mum, Dad, Carl, Rob, and
Lorraine.
The without-whom list:
Our technical review team did a great job of keeping us straight, and making sure
that what we covered was spot on. We’re also grateful to Ingo Krotzky for his
valuable feedback on the early release of this book, and all the people who gave us
feedback on the first edition. We think the book’s much, much better as a result.
Finally, our thanks to Kathy Sierra and Bert Bates for creating this
extraordinary series of books.

you are here 4  xxxix

o’reilly safari

O’Reilly Safari®
Safari (formerly Safari Books Online) is a membership-based training and
reference platform for enterprise, government, educators, and individuals.
Members have access to thousands of books, training videos, Learning Paths,
interactive tutorials, and curated playlists from over 250 publishers, including
O’Reilly Media, Harvard Business Review, Prentice Hall Professional, AddisonWesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal
Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM
Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGrawHill, Jones & Bartlett, and Course Technology, among others.
For more information, please visit http://oreilly.com/safari.

xl  intro

1 getting started

Diving In

Android has taken the world by storm.
Everybody wants a smartphone or tablet, and Android devices are hugely popular. In this
book, we’ll teach you how to develop your own apps, and we’ll start by getting you to
build a basic app and run it on an Android Virtual Device. Along the way, you’ll meet some
of the basic components of all Android apps, such as activities and layouts. All you
need is a little Java know-how...

this is a new chapter   1

android overview

Welcome to Androidville
Android is the world’s most popular mobile platform. At the
last count, there were over two billion active Android devices
worldwide, and that number is growing rapidly.
Android is a comprehensive open source platform based
on Linux and championed by Google. It’s a powerful
development framework that includes everything you need
to build great apps using a mix of Java and XML. What’s
more, it enables you to deploy those apps to a wide variety of
devices—phones, tablets, and more.
So what makes up a typical Android app?

Layouts define what each screen
looks like
A typical Android app is composed of one
or more screens. You define what each
screen looks like using a layout to define its
appearance. Layouts are usually defined in
XML, and can include GUI components such
as buttons, text fields, and labels.

We’re going to build our
Android apps using a mixture
of Java and XML. We’ll
explain things along the way,
but you’ll need to have a fair
understanding of Java to get
the most out of this book.

Layouts
tell Android
what the
screens in
your app
look like.

Activities define what the app
does
Layouts only define the appearance of the app.
You define what the app does using one or
more activities. An activity is a special Java
class that decides which layout to use and
tells the app how to respond to the user. As
an example, if a layout includes a button, you
need to write Java code in the activity to define
what the button should do when you press it.

Activities define
what the app
should do.

Sometimes extra resources are needed too
In addition to activities and layouts, Android apps often need
extra resources such as image files and application data. You
can add any extra files you need to the app.
Android apps are really just a bunch of files in particular
directories. When you build your app, all of these files get
bundled together, giving you an app you can run on your
device.
2  Chapter 1

Resources
can include
sound and
image files.

getting started

The Android platform dissected

		Don’t worry if this seems
like a lot to take in.

The Android platform is made up of a number of
different components. It includes core applications such
as Contacts, a set of APIs to help you control what your
app looks like and how it behaves, and a whole load of
supporting files and libraries. Here’s a quick look at how
they all fit together:
Android comes with a
set of core applications
such as Contacts, Phone,
Calendar, and a browser.
When you build apps, you
have access to the same
APIs used by the core
applications. You use
these APIs to control
what your app looks like
and how it behaves.
Underneath the
application framework
lies a set of C and C++
libraries. These libraries
get exposed to you
through the framework
APIs.
Underneath everything
else lies the Linux kernel.
Android relies on the
kernel for drivers, and
also core services such
as security and memory
management.

We’re just giving you an
overview of what’s included in
the Android platform. We’ll explain the different
components in more detail as and when we
need to.

Applications
Contacts

Home

Browser

Phone

...

Application Framework
Window
Manager

Activity
Manager
Package
Manager

Telephony
Manager

View
System

Content
Providers

Location
Manager

Resource
Manager

Libraries

Notification
Manager

Android Runtime

Surface
Manager

Media
Framework

SQLite

OpenGL |
ES

FreeType

WebKit

SGL

SSL

libc

Core
Libraries

The Android
runtime
comes with a
set of core
libraries that
implement
most of
the Java
programming
language.
Each Android
app runs
in its own
process.

Linux Kernel
Display
Driver

Camera
Driver

Flash Memory
Driver

Binder (IPC)
Driver

Keypad
Driver

WiFi
Driver

Audio
Drivers

Power
Management

The great news is that all of the powerful Android libraries
are exposed through the APIs in the application framework,
and it’s these APIs that you use to create great Android
apps. All you need to begin is some Java knowledge and a
great idea for an app.
you are here 4  3

steps

Here’s what we’re going to do
So let’s dive in and create a basic Android app. There are just
a few things we need to do:
1

Set up a development environment.
We need to install Android Studio, which
includes all the tools you need to develop
Android apps.

2

Build a basic app.
We’ll build a simple app using Android
Studio that will display some sample text on
the screen.

3

Run the app in the Android emulator.
We’ll use the built-in emulator to see the app
up and running.

4

Change the app.
Finally, we’ll make a few tweaks to the app we
created in step 2, and run it again.

Q:

Are all Android apps developed
in Java?

A:

You can develop Android apps in
other languages, too. Most developers use
Java, so that’s what we’re covering in this
book.

4  Chapter 1

Q:

How much Java do I need to know
for Android app development?

A:

You really need experience with Java
SE (Standard Edition). If you’re feeling
rusty, we suggest getting a copy of Head
First Java by Kathy Sierra and Bert Bates.

Q:

Do I need to know about Swing
and AWT?

A:

Android doesn’t use Swing or AWT,
so don’t worry if you don’t have Java
desktop GUI experience.

You are here.

Your development environment
Java is the most popular language used to develop Android applications.
Android devices don’t run .class and .jar files. Instead, to improve speed
and battery performance, Android devices use their own optimized
formats for compiled code. That means that you can’t use an ordinary
Java development environment—you also need special tools to convert
your compiled code into an Android format, to deploy them to an
Android device, and to let you debug the app once it’s running.

getting started

Set up environment
Build app
Run app
Change app

All of these come as part of the Android SDK. Let’s take a look at
what’s included.

The Android SDK
The Android Software Development Kit contains the libraries and tools
you need to develop Android apps. Here are some of the main points:
SDK Platform
There’s one of these for each
version of Android.

Documentation
So you can access the latest
API documentation offline.

SDK Tools
Tools for debugging and testing,
plus other useful utilities. The
SDK also features a set of
platform dependent tools.
Sample apps
If you want practical code
examples to help you understand
how to use some of the APIs, the
sample apps might help you.

oid
dr

SDK

An

Android support
Extra APIs that aren’t available
in the standard platform.
Google Play Billing
Allows you to integrate billing
services in your app.

Android Studio is a special version of IntelliJ IDEA
IntelliJ IDEA is one of the most popular IDEs for Java development.
Android Studio is a version of IDEA that includes a version of
the Android SDK and extra GUI tools to help you with your app
development.
In addition to providing you with an editor and access to the tools and
libraries in the Android SDK, Android Studio gives you templates you
can use to help you create new apps and classes, and it makes it easy to
do things such as package your apps and run them.
you are here 4  5

installation

Set up environment
Build app
Run app
Change app

Install Android Studio
Before we go any further, you need to install Android Studio on
your machine. We’re not including the installation instructions
in this book as they can get out of date pretty quickly, but you’ll
be fine if you follow the online instructions.
First, check the Android Studio system requirements here:

We’re using Android Studio version 2.3.
You’ll need to use this version or above
to get the most out of this book.

http://developer.android.com/sdk/index.html#Requirements
Then follow the Android Studio installation instructions here:

https://developer.android.com/sdk/installing/index.html?pkg=studio
Once you’ve installed Android Studio, open it and follow the
instructions to add the latest SDK tools and Support Libraries.
When you’re done, you should see the Android Studio welcome
screen. You’re now ready to build your first Android app.

This is the Android
Studio welcome
screen. It includes a
set of options for
things you can do.

6  Chapter 1

Google sometimes changes their
URLs. If these URLs don’t
work, search for Android Studio
and you should find them.

getting started

Q:

You say we’re going to use
Android Studio to build the Android
apps. Do I have to?

A:

Strictly speaking, you don’t have to
use Android Studio to build Android apps.
All you need is a tool that will let you write
and compile Java code, plus a few other
tools to convert the compiled code into a
form that Android devices can run.

Android Studio is the official Android IDE,
and the Android team recommends using
it. But quite a lot of people use IntelliJ IDEA
instead.

Q:

Can I write Android apps without
using an IDE?

A:

It’s possible, but it’s more work. Most
Android apps are now created using a build
tool called Gradle. Gradle projects can be
created and built using a text editor and a
command line.

Q:

ANT?

A build tool? So is gradle like

Q:

Most apps are built using Gradle?
I thought you said most developers use
Android Studio.

A:

Android Studio provides a graphical
interface to Gradle, and also to other tools
for creating layouts, reading logs, and
debugging.

You can find out more about Gradle in
Appendix II.

A:

It’s similar, but Gradle is much more
powerful than ANT. Gradle can compile and
deploy code, just like ANT, but it also uses
Maven to download any third-party libraries
your code needs. Gradle also uses Groovy as
a scripting language, which means you can
easily create quite complex builds with Gradle.

Build a basic app
Now that you’ve set up your development environment, you’re
ready to create your first Android app. Here’s what the app will look
like:

This is the name
of the application.

This is a very simple
app, but that’s all you
need for your very
first Android app.

There’ll be a small piece
of sample text right here
that Android Studio will
put in for us.
you are here 4  7

create project

You’ve completed this step
now, so we’ve checked it off.

How to build the app
Whenever you create a new app, you need to create a
new project for it. Make sure you have Android Studio
open, and follow along with us.

1. Create a new project
The Android Studio welcome screen gives you a number of options.
We want to create a new project, so click on the option for “Start a
new Android Studio project.”

Click on this option to start
a new Android Studio project.

Any projects you create
will appear here. This is our
first project, so this area is
currently empty.

8  Chapter 1

Set up environment
Build app
Run app
Change app

getting started

Set up environment
Build app
Run app
Change app

How to build the app (continued)
2. Configure the project
You now need to configure the app by telling Android Studio what
you want to call it, what company domain to use, and where you
would like to store the files.

The package
name must
stay the same
for the lifetime
of your app.

Android Studio uses the company domain and application name to
form the name of the package that will be used for your app. As an
example, if you give your app a name of “My First App” and use
a company domain of “hfad.com”, Android Studio will derive a
package name of com.hfad.myfirstapp. The package name
is really important in Android, as it’s used by Android devices to
uniquely identify your app.
Enter an application name of “My First App”, enter a company
domain of “hfad.com”, uncheck the option to include C++ support,
and accept the default project location. Then click on the Next
button.

Some versions of Android
Studio may have an extra
option asking if you want
to include Kotlin support.
Uncheck this option if it’s
there.

It’s a unique identifier for your
app and used to manage
multiple versions of the same
app.

The application name is shown
in the Google Play Store and
various other places, too.

The wizard forms the
package name by combining
the application name and
the company domain.

Use a company domain
of hfad.com.
Uncheck the option to include C++
support. If prompted, also uncheck
the option to include Kotlin support.

All of the files for your project will be stored

here.

you are here 4  9

api level

How to build the app (continued)

Set up environment
Build app
Run app
Change app

3. Specify the minimum SDK
You now need to indicate the minimum SDK of Android your app will use.
API levels increase with every new version of Android. Unless you only want
your app to run on the very newest devices, you’ll probably want to specify one
of the older APIs.
Here, we’re choosing a minimum SDK of API level 19, which means it will be
able to run on most devices. Also, we’re only going to create a version of our
app to run on phones and tablets, so we’ll leave the other options unchecked.
When you’ve done this, click on the Next button.

The minimum required SDK
is the lowest version your
app will support. Your app
will run on devices with
this level API or higher. It
won’t run on devices with
a lower API.

10  Chapter 1

There’s more about the
different API levels on
the next page.

getting started

Android Versions Up Close
You’ve probably heard a lot of things about Android that sound
tasty, like Jelly Bean, KitKat, Lollipop, and Nougat. So what’s
with all the confectionary?
Android versions have a version number and a codename. The
version number gives the precise version of Android (e.g., 7.0),
while the codename is a more generic “friendly” name that may
cover several versions of Android (e.g., Nougat). The API level
refers to the version of the APIs used by applications. As an
example, the equivalent API level for Android version 7.1.1 is 25.

Version

Codename

1.0

API level
1

1.1

2

1.5

Cupcake

3

1.6

Donut

4

2.0–2.1

Eclair

5–7

2.2.x

Froyo

8

2.3–2.3.7

Gingerbread

9–10

3.0 - 3.2

Honeycomb

11–13

4.0–4.0.4

Ice Cream Sandwich

14–15

4.1 - 4.3

Jelly Bean

16–18

4.4

KitKat

19–20

5.0–5.1

Lollipop

21–22

6.0

Marshmallow

23

7.0

Nougat

24

7.1–7.1.2

Nougat

25

Hardly anyone uses
these versions anymore.

Most devices use
one of these APIs.

When you develop Android apps, you really need to consider
which versions of Android you want your app to be compatible
with. If you specify that your app is only compatible with the
very latest version of the SDK, you might find that it can’t be
run on many devices. You can find out the percentage of devices
running particular versions here: https://developer.android.com/
about/dashboards/index.html.

you are here 4  11

50,000 feet

Set up environment
Build app
Run app
Change app

Activities and layouts from 50,000 feet
The next thing you’ll be prompted to do is add an activity to your project.
Every Android app is a collection of screens, and each screen is composed
of an activity and a layout.
An activity is a single, defined thing that your user can do. You
might have an activity to compose an email, take a photo, or find a contact.
Activities are usually associated with one screen, and they’re written in Java.
A layout describes the appearance of the screen. Layouts are written
as XML files and they tell Android how the different screen elements are
arranged.
Let’s look in more detail at how activities and layouts work together to
create a user interface:
1

The device launches
your app and creates
an activity object.

2

The activity object
specifies a layout.
The activity tells
Android to display the
layout onscreen.

4

The user interacts
with the layout that’s
displayed on the device.

5

The activity responds
to these interactions by
running application code.

6

The activity updates
the display...

7

...which the user sees
on the device.

2

Device

Activity

Layout

3

4

User






Now that you know a bit more about what activities and layouts are,
let’s go through the last couple of steps in the Create New Project
wizard and get it to create an activity and layout.
12  Chapter 1

Activities define
actions.

1



3

Layouts define how
the user interface is
presented.

7

5

Device

6

Activity

getting started

How to build the app (continued)

Set up environment
Build app
Run app
Change app

4. Add an activity
The next screen lets you choose among a series of templates you
can use to create an activity and layout. We’re going to create
an app with an empty activity and layout, so choose the Empty
Activity option and click the Next button.

There are other types
of activity you can
choose from, but for
this exercise make sure
you select the Empty
Activity option.

you are here 4  13

customize activity

How to build the app (continued)

Set up environment
Build app
Run app
Change app

5. Customize the activity
You will now be asked what you want to call the screen’s activity and layout.
Enter an activity name of “MainActivity”, make sure the option to generate a
layout file is checked, enter a layout name of “activity_main”, and then uncheck
the Backwards Compatibility (AppCompat) option. The activity is a Java class,
and the layout is an XML file, so the names we’ve given here will create a Java
class file called MainActivity.java and an XML file called activity_main.xml.
When you click on the Finish button, Android Studio will build your app.

Uncheck the Backwards
Compatibility (AppCompat) option.
You’ll find out more about this
setting later in the book.

14  Chapter 1

Name the activity
“MainActivity”
and the layout
“activity_main”.
Also make sure the
option to generate
the layout is
checked.

getting started

Set up environment
Build app
Run app
Change app

You’ve just created your first Android app
So what just happened?

¥

The Create New Project wizard created a project for your app,
configured to your specifications.
You defined which versions of Android the app should be compatible with, and
the wizard created all of the files and folders needed for a basic valid app.

¥

The wizard created an activity and layout with template code.
The template code includes layout XML and activity Java code, with sample
“Hello World!” text in the layout.

When you finish creating your project by going through the
wizard, Android Studio automatically displays the project for
you.
Here’s what our project looks like (don’t worry if it looks
complicated—we’ll break it down over the next few pages):

This is the project in Android Studio.

you are here 4  15

folder structure

Set up environment
Build app
Run app
Change app

Android Studio creates
a complete folder structure for you
An Android app is really just a bunch of valid files in a particular folder structure,
and Android Studio sets all of this up for you when you create a new app. The
easiest way of looking at this folder structure is with the explorer in the leftmost
column of Android Studio.
The explorer contains all of the projects that you currently have open. To expand
or collapse folders, just click on the arrows to the left of the folder icons.

Click on the arrow
here and choose
the Project option
to see the files and
folders that make
up your project.

The folder structure includes
different types of files
If you browse through the folder structure, you’ll
see that the wizard has created various types of
files and folders for you:

This is the
name of
the project.

These files and
folders are all
included in your
project.

Click on
these
arrows
to
expand
or
collapse
the
folders.

¥

Java and XML source files
These are the activity and layout files for
your app.

¥

Android-generated Java files
There are some extra Java files you
don’t need to touch that Android Studio
generates for you automatically.

¥

Resource files
These include default image files for
icons, styles your app might use, and any
common String values your app might
want to look up.

¥

Android libraries
In the wizard, you specified the minimum
SDK version you want your app to be
compatible with. Android Studio makes
sure your app includes the relevant
Android libraries for that version.

¥

Configuration files
The configuration files tell Android what’s
actually in the app and how it should run.

Let’s take a closer look at some of the key files and
folders in Androidville.
16  Chapter 1

getting started

Set up environment
Build app
Run app
Change app

Useful files in your project
Android Studio projects use the Gradle build system to compile and
deploy apps. Gradle projects have a standard structure. Here are
some of the key files and folders you’ll be working with:
The app folder is a
module in your project.

MyFirstApp

The root folder
is the name of
your project. All
the files for your
project go in here.

The build folder contains files that
Android Studio creates for you. You
don’t usually edit anything in this folder.

app
build

Every Android project needs a file
called R.java, which is created for
you and which lives in the generated/
source folder. Android uses this file
to keep track of resources in the app.

generated/source

r/debug

The src folder contains
source code you write
and edit.

com/hfad/myfirstapp
src

The java folder contains any
Java code you write. Any
activities you create live here.
You can find app resources in the
res folder. For example, the layout
subfolder contains layouts, and the
values subfolder contains resource
files for values such as Strings. You
can get other types of resources
too.
Every Android app must include a
file called AndroidManifest.xml
at its root. The manifest file
contains essential information
about the app, such as what
components it contains, required
libraries, and other declarations.

R.java

main
java
com/hfad/myfirstapp

MainActivity.java

MainActivity.java defines
an activity. An activity tells
Android how the app should
interact with the user.

res
layout




activity_main.xml defines a
layout. A layout tells Android
how your app should look.

activity_main.xml

values







AndroidManifest.xml

strings.xml

strings.xml is a String resource
file. It includes Strings such as
the app’s name and any default
text values. Other files such as
layouts and activities can look up
text values from here.
you are here 4  17

meet the editors

Edit code with the Android Studio editors
You view and edit files using the Android Studio editors. Double-click
on the file you want to work with, and the file’s contents will appear in
the middle of the Android Studio window.

Set up environment
Build app
Run app
Change app

The code editor
Most files get displayed in
the code editor, which is
just like a text editor, but
with extra features such
as color coding and code
checking.

Double-click on the file
in the explorer and the
file contents appear in
the editor panel.

The design editor
If you’re editing a
layout, you have an
extra option. Rather
than edit the XML
(such as that shown on
the next page), you can
use the design editor,
which allows you to
drag GUI components
onto your layout, and
arrange them how you
want. The code editor
and design editor give
different views of the
same file, so you can
switch back and forth
between the two.

18  Chapter 1

You dictate
which editor
you’re using with
these tabs.

You can edit layouts using the
visual editor by dragging and
dropping components.

getting started

Here’s the code from an example layout file (not the one Android Studio
generated for us). We know you’ve not seen layout code before, but just
see if you can match each of the descriptions at the bottom of the page to the
correct lines of code. We’ve done one to get you started.
activity_main.xml







Add padding to the screen
margins.

Include a  GUI
component for displaying
text.
Make the GUI component just
large enough for its content.

Display the String “Hello
World!”

Make the layout the same
width and height as the
screen size on the device.

you are here 4  19

solution

SOLUTION
Here’s the code from an example layout file (not the one Android Studio
generated for us). We know you’ve not seen layout code before, but just
see if you can match each of the descriptions at the bottom of the page to the
correct lines of code. We’ve done one to get you started.
activity_main.xml







Add padding to the screen
margins.

Include a  GUI
component for displaying
text.

Make the GUI component just
large enough for its content.

20  Chapter 1

Display the String “Hello
World!”

Make the layout the same
width and height as the
screen size on the device.

getting started

Now let’s see if you can do the same thing for some activity code. This
is example code, and not necessarily the code that Android
Studio will have generated for you. Match the descriptions below
to the correct lines of code.

MainActivity.java
package com.hfad.myfirstapp;
import android.os.Bundle;

import android.app.Activity;
public class MainActivity extends Activity {
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

}

setContentView(R.layout.activity_main);

This is the package name.

These are Android classes
used in MainActivity.

Implement the onCreate()
method from the Activity
class. This method is called
when the activity is first
created.

MainActivity extends the
Specify which layout to use.

Android class

android.app.Activity.

you are here 4  21

another solution

SOLUTION
Now let’s see if you can do the same thing for some activity code. This
is example code, and not necessarily the code that Android
Studio will have generated for you. Match the descriptions below
to the correct lines of code.

MainActivity.java
package com.hfad.myfirstapp;
import android.os.Bundle;

import android.app.Activity;
public class MainActivity extends Activity {
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

)

}

setContentView(R.layout.activity_main);

This is the package name.

These are Android classes
used in MainActivity.

Implement the onCreate()
method from the Activity
class. This method is called
when the activity is first
created.

MainActivity extends the
Specify which layout to use.

22  Chapter 1

Android class

android.app.Activity.

getting started

Run the app in the Android emulator
So far you’ve seen what your Android app looks like in Android
Studio and got a feel for how it hangs together. But what you
really want to do is see it running, right?
You have a couple of options when it comes to running your
apps. The first option is to run them on a physical device. But
what if you don’t have one with you, or you want to see how
your app looks on a type of device you don’t have?
In that case, you can use the Android emulator that’s built
into the Android SDK. The emulator enables you to set up one
or more Android virtual devices (AVDs) and then run your
app in the emulator as though it’s running on a physical device.

So what does the emulator look like?
Here’s an AVD running in the Android emulator. It looks just
like a phone running on your computer.
The emulator recreates the
exact hardware environment
of an Android device: from its
CPU and memory through to
the sound chips and the video
display. The emulator is built
on an existing emulator called
QEMU (pronounced “queue em
you”), which is similar to other
virtual machine applications you
may have used, like VirtualBox or
VMWare.

Set up environment
Build app
Run app
Change app

The Android emulator allows
you to run your app on an
Android virtual device (AVD),
which behaves just like a
physical Android device. You
can set up numerous AVDs,
each emulating a different
type of device.
Once you’ve set up an
AVD, you'll be able to
see your app running
on it. Android Studio
launches the emulator
for you.

The exact appearance and
behavior of the AVD depends on
how you’ve set up the AVD in the
first place. The AVD here is set
up to mimic a Nexus 5X, so it will
look and behave just like a Nexus
5X on your computer.
Let’s set up an AVD so that you
can see your app running in the
emulator.

you are here 4  23

create avd

Create an Android Virtual Device
There are a few steps you need to go through in order to set up an AVD
within Android Studio. We’ll set up a Nexus 5X AVD running API level
25 so that you can see how your app looks and behaves running on this
type of device. The steps are pretty much identical no matter what type
of virtual device you want to set up.

Set up environment
Build app
Run app
Change app

Open the Android Virtual Device Manager
The AVD Manager allows you to set up new AVDs, and
view and edit ones you’ve already created. Open it by
selecting Android on the Tools menu and choosing AVD
Manager.
If you have no AVDs set up already, you’ll be presented
with a screen prompting you to create one. Click on the
“Create Virtual Device” button.

Click on this button
to create an AVD.

Select the
hardware
On the next screen,
you’ll be prompted
to choose a device
definition. This is
the type of device
your AVD will
emulate. You can
choose a variety of
phone, tablet, wear,
or TV devices.
We’re going to see
what our app looks
like running on a
Nexus 5X phone.
Choose Phone from
the Category menu
and Nexus 5X from
the list. Then click
the Next button.
24  Chapter 1

When you
select a
device, its
details
appear
here.

getting started

Creating an AVD (continued)

Set up environment
Build app
Run app
Change app

Select a system image
Next, you need to select a system image. The system image gives you an
installed version of the Android operating system. You can choose the
version of Android you want to be on your AVD.
You need to choose a system image for an API level that’s compatible with
the app you’re building. As an example, if you want your app to work on
a minimum of API level 19, choose a system image for at least API level 19.
We want our AVD to run API level 25, so choose the system image with a
release name of Nougat and a target of Android 7.1.1 (API level 25). Then
click on the Next button.

If you don’t
have this system
image installed,
you’ll be given
the option to
download it.

We’ll continue setting up the AVD on the next page.

you are here 4  25

check configuration

Set up environment
Build app
Run app
Change app

Creating an AVD (continued)
Verify the AVD configuration
On the next screen, you’ll be asked to verify the AVD configuration. This screen
summarizes the options you chose over the last few screens, and gives you the
option of changing them. Accept the options, and click on the Finish button.

These are the options
you chose over the
past few pages.

The AVD Manager will create the AVD for you, and when it’s done, display it in
the AVD Manager list of devices. You may now close the AVD Manager.

Your Nexus 5X AVD has been created.
26  Chapter 1

getting started

Set up environment
Build app
Run app
Change app

Run the app in the emulator
Now that you’ve set up your AVD, let’s run the app on it. To do
this, choose the “Run ‘app’” command from the Run menu. When
you’re asked to choose a device, select the Nexus 5X AVD you just
created. Then click OK.
The AVD can take a few minutes to appear, so while we wait, let’s
take a look at what happens when you choose Run.

Compile, package, deploy, and run
The Run command doesn’t just run your app. It also handles all the
preliminary tasks that are needed for the app to run:

This is the AVD we just created.
Libraries

2

1

Java file

Resources

APK
Bytecode

Run

APK file

3

4

5

Emulator

An APK file is an
Android application
package. It’s
basically a JAR
or ZIP file for
Android applications.

Emulator

1

The Java source files get compiled to
bytecode.

2

An Android application package, or APK
file, gets created.
The APK file includes the compiled Java files,
along with any libraries and resources needed
by your app.

3

Assuming there’s not one already
running, the emulator gets launched
and then runs the AVD.

4

Once the emulator has been launched
and the AVD is active, the APK file is
uploaded to the AVD and installed.

5

The AVD starts the main activity
associated with the app.
Your app gets displayed on the AVD screen,
and it’s all ready for you to test out.

you are here 4  27

be patient

Set up environment
Build app
Run app
Change app

You can watch progress in the console
It can sometimes take quite a while for the emulator to launch with your
AVD—often several minutes. If you like, you can watch what’s happening
using the Android Studio console. The console gives you a blow-by-blow
account of what the build system is doing, and if it encounters any errors,
you’ll see them highlighted in the text.

We suggest finding something else to do
while waiting for the emulator to start.
Like quilting, or cooking a small meal.

You can find the console at the bottom of the Android Studio screen
(click on the Run option at the bottom of the screen if it doesn’t appear
automatically):

Here’s the output from our console window when we ran our app:
03/13 10:45:41: Launching app

Install the app.

$ adb install-multiple -r /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/
split-apk/debug/dep/dependencies.apk /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/
intermediates/split-apk/debug/slices/slice_1.apk /Users/dawng/AndroidStudioProjects/MyFirstApp/
app/build/intermediates/split-apk/debug/slices/slice_2.apk /Users/dawng/AndroidStudioProjects/
MyFirstApp/app/build/intermediates/split-apk/debug/slices/slice_0.apk /Users/dawng/
AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/debug/slices/slice_3.apk /Users/
dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/debug/slices/slice_6.apk /
Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/debug/slices/slice_4.
apk /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/debug/slices/
slice_5.apk /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/debug/
slices/slice_7.apk /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/split-apk/
debug/slices/slice_8.apk /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/intermediates/
split-apk/debug/slices/slice_9.apk /Users/dawng/AndroidStudioProjects/MyFirstApp/app/build/outputs/
apk/app-debug.apk
Split APKs installed

$ adb shell am startservice com.hfad.myfirstapp/com.android.tools.fd.runtime.InstantRunService

$ adb shell am start -n "com.hfad.myfirstapp/com.hfad.myfirstapp.MainActivity" -a android.intent.
action.MAIN -c android.intent.category.LAUNCHER
Connected to process 2685 on device Nexus_5X_API_25 [emulator-5554]

28  Chapter 1

Android Studio has finished
launching the AVD we just set up.

The emulator launches our app by starting the main activity
for it. This is the activity the wizard created for us.

getting started

Test drive
So let’s look at what actually happens onscreen when you run
your app.
First, the emulator fires up in a separate window. The
emulator takes a while to load the AVD, but then you see
what looks like an actual Android device.

...and here’s the AVD home
screen. It looks and behaves
just like a real Nexus 5X device.

The emulator
launches...

Wait a bit longer, and you’ll see the app you
just created. The application name appears
at the top of the screen, and the default
sample text “Hello World!” is displayed in
the middle of the screen.

Android Studio created the
sample text “Hello World!”
without us telling it to.

Set up environment
Build app
Run app
Change app

This is the name of the app.

The wizard created
sample text for us.
Here’s the app
running on the AVD.
you are here 4  29

what happened

Set up environment
Build app
Run app
Change app

What just happened?
Let’s break down what happens when you run the app:
1

Android Studio launches the emulator,
loads the AVD, and installs the app.

2

When the app gets
launched, an activity
object is created from
MainActivity.java.

3

4

The activity specifies
that it uses the layout
activity_main.xml.

1

2
3







Device

The activity tells Android to
display the layout on the screen.
The text “Hello World!” gets displayed.

4

Activity

Layout

In this particular
instance, we’re using
a virtual device.

Q:

You mentioned that when you create an APK file, the
Java source code gets compiled into bytecode and added to
the APK. Presumably you mean it gets compiled into Java
bytecode, right?

A:

It does, but that’s not the end of the story. Things work a little
differently on Android.

The big difference with Android is that your code doesn’t actually
run inside an ordinary Java VM. It runs on the Android runtime
(ART) instead, and on older devices it runs in a predecessor to ART
called Dalvik. This means that you write your Java source code and
compile it into .class files using the Java compiler, and then the
.class files get stitched into one or more files in DEX format, which is
smaller, more efficient bytecode. ART then runs the DEX code. You
can find more details about this in Appendix III.

Q:

That sounds complicated. Why not just use the normal
Java VM?

A:

ART can convert the DEX bytecode into native code that can
run directly on the CPU of the Android device. This makes the app
run a lot faster, and use a lot less battery power.

Q:
A:
Q:

Is a Java virtual machine really that much overhead?

Yes. Because on Android, each app runs inside its own
process. If it used ordinary JVMs, it would need a lot more memory.

app?

A:

Do I need to create a new AVD every time I create a new

No, once you’ve created the AVD you can use it for any of
your apps. You may find it useful to create multiple AVDs in order to
test your apps in different situations. As an example, in addition to
a phone AVD you might want to create a tablet AVD so you can see
how your app looks and behaves on larger devices.

30  Chapter 1

getting started

Set up environment
Build app
Run app
Change app

Refine the app
Over the past several pages, you’ve built a basic Android app
and seen it running in the emulator. Next, we’re going to refine
the app.
At the moment, the app displays the sample text “Hello World!”
that the wizard put in as a placeholder. You’re going to change
that text to say something else instead. So what do we need to
change in order to achieve that? To answer that, let’s take a
step back and look at how the app is currently built.

Your app currently says
“Hello World!” but we're
going to change it to
something else instead.

The app has one activity and one layout
When we built the app, we told Android Studio how to
configure it, and the wizard did the rest. The wizard created an
activity for us, and also a default layout.

Our activity
specifies what the
app does and how
it should interact
with the user.

The activity controls what the app does
Android Studio created an activity for us called
MainActivity.java. The activity specifies what the app
does and how it should respond to the user.

MainActivity.java

The layout controls the app's appearance
MainActivity.java specifies that it uses the layout Android
Studio created for us called activity_main.xml. The layout
specifies what the app looks like.

We want to change the appearance of the app by changing the
text that’s displayed. This means that we need to deal with the
Android component that controls what the app looks like, so we
need to take a closer look at the layout.



Our layout specifies
what the app looks
like.


activity_main.xml

you are here 4  31

the layout

What’s in the layout?
We want to change the sample “Hello World!”
text that Android Studio created for us, so let’s
start with the layout file activity_main.xml. If it
isn’t already open in an editor, open it now by
finding the file in the app/src/main/res/layout
folder in the explorer and double-clicking on it.

If you can’t
see the folder
structure in the
explorer, try
switching to
Project view.

Click on
this arrow
to change
how the
files and
folders are
shown.

The design editor

The design editor
As you learned earlier, there are two ways of
viewing and editing layout files in Android
Studio: through the design editor and
through the code editor.
When you choose the design option, you
can see that the sample text “Hello World!”
appears in the layout as you might expect.
But what’s in the underlying XML?
Let’s see by switching to the code editor.

You can see the design editor
by choosing “Design” here.

The code editor
When you choose the code editor
option, the content of activity_main.xml
is displayed. Let’s take a closer look
at it.

The code
editor

To see the code editor, click
on “Text” in the bottom tab.
32  Chapter 1

Here’s the sample text.

getting started

Set up environment
Build app
Run app
Change app

activity_main.xml has two elements
Below is the code from activity_main.xml that Android Studio
generated for us. We’ve left out some of the details you don’t
need to think about just yet; we’ll cover them in more detail
through the rest of the book.
Here’s our code:

This is the full path
of activity_main.xml.



This element
determines
how
components
should be
displayed, in
this case the
“Hello World!”
text.

 element.

... >

MyFirstApp

app/src/main

android:layout_width="wrap_content"

res

android:layout_height="wrap_content"

layout

android:text="Hello World!"
... />




We’ve left out some of the
 XML too.

activity_main.xml



As you can see, the code contains two elements.
The first is an  element. This is a type of layout
element that tells Android how to display components on
the device screen. There are various types of layout element
available for you to use, and you’ll find out more about these
later in the book.
The most important element for now is the second element,
the . This element is used to display text to the
user, in our case the sample text “Hello World!”
The key part of the code within the  element
is the line starting with android:text. This is a text
property describing the text that should be displayed:

The  element
describes the text in
the layout.

		Don’t worry if your
layout code looks
different from ours.
Android Studio
may give you slightly different XML
depending on which version you’re using.
You don’t need to worry about this,
because from the next chapter onward
you’ll learn how to roll your own layout
code, and replace a lot of what Android
Studio gives you.



This is the text that's being displayed.

Let’s change the text to something else.
you are here 4  33

update text

Set up environment
Build app
Run app
Change app

Update the text displayed in the layout
The key part of the  element is this line:
android:text="Hello World!" />

android:text means that this is the text property of
the  element, so it specifies what text should be
displayed in the layout. In this case, the text that’s being displayed
is “Hello World!”

Display the text...

...“Hello World!”

android:text="Hello World!" />

To update the text that’s displayed in the layout, simply change
the value of the text property from "Hello World!" to
"Sup doge". The new code for the  should look
like this:
...

We’ve left out some
of the code, as all
we’re doing for now
is changing the text
that’s displayed.



Change the text here from
“Hello World!” to “Sup doge”.

Once you’ve updated the file, go to the File menu and choose the
Save All option to save your change.

Q:
A:

My layout code looks different from yours. Is that OK?

Yes, that’s fine. Android Studio may generate slightly different
code if you’re using a different version than us, but that doesn’t
really matter. From now on you’ll be learning how to create your
own layout code, and you’ll replace a lot of what Android Studio
gives you.

Q:

Am I right in thinking we’re hardcoding the text that's
displayed?

A:

Yes, purely so that you can see how to update text in
the layout. There’s a better way of displaying text values than
hardcoding them in your layouts, but you’ll have to wait for the next
chapter to learn what it is.

34  Chapter 1

MyFirstApp
app/src/main
res
layout



activity_main.xml

Q:

The folders in my project explorer pane look different
from yours. Why’s that?

A:

Android Studio lets you choose alternate views for how to
display the folder hierarchy, and it defaults to the “Android” view.
We prefer the “Project” view, as it reflects the underlying folder
structure. You can change your explorer to the “Project” view by
clicking on the arrow at the top of the explorer pane, and selecting
the “Project” option.

We’re
using the
Project
view.

Click on
this arrow
to change
the explorer
view.

getting started

Take the app for a test drive
Once you’ve edited the file, try running your app in the
emulator again by choosing the “Run ‘app’” command from
the Run menu. You should see that your app now says “Sup
doge” instead of “Hello World!”

Set up environment
Build app
Run app
Change app

Here’s the
updated version
of our app.

The sample text now
says “Sup doge” instead
of “Hello World!”

You’ve now built and updated your first Android app.

you are here 4  35

toolbox

CHAPTER 1

Your Android Toolbox
You’ve got Chapter 1 under
your belt and now you’ve
added Android basic concepts
to your toolbox.

ƒƒ Versions of Android have a version
number, API level, and code name.
ƒƒ Android Studio is a special version of
IntelliJ IDEA that interfaces with the
Android Software Development Kit
(SDK) and the Gradle build system.
ƒƒ A typical Android app is composed of
activities, layouts, and resource files.
ƒƒ Layouts describe what your app
looks like. They’re held in the app/
src/main/res/layout folder.
ƒƒ Activities describe what your app
does, and how it interacts with the
user. The activities you write are held
in the app/src/main/java folder.

36  Chapter 1

You can download
the full code for
the chapter from
https://tinyurl.com/
HeadFirstAndroid.

ƒƒ AndroidManifest.xml contains
information about the app itself. It
lives in the app/src/main folder.
ƒƒ An AVD is an Android Virtual Device.
It runs in the Android emulator and
mimics a physical Android device.
ƒƒ An APK is an Android application
package. It’s like a JAR file for
Android apps, and contains your
app’s bytecode, libraries, and
resources. You install an app on a
device by installing the APK.
ƒƒ Android apps run in separate
processes using the Android runtime
(ART).
ƒƒ The  element is used
for displaying text.

2 building interactive apps

Apps That Do Something
I wonder what
happens if I press
the button marked
“ejector seat”?

Most apps need to respond to the user in some way.
In this chapter, you’ll see how you can make your apps a bit more interactive. You’ll
learn how to get your app to do something in response to the user, and how to get your
activity and layout talking to each other like best buddies. Along the way, we’ll take you
a bit deeper into how Android actually works by introducing you to R, the hidden gem
that glues everything together.

this is a new chapter   37

beer adviser

Let’s build a Beer Adviser app
In Chapter 1, you saw how to create an app using the Android
Studio New Project wizard, and how to change the text
displayed in the layout. But when you create an Android app,
you’re usually going to want the app to do something.
In this chapter, we’re going to show you how to create an app
that the user can interact with: a Beer Adviser app. In the app,
users can select the types of beer they enjoy, click a button, and
get back a list of tasty beers to try out.

Choose your
beer type, click
the button...
...and the
app comes
up with a
list of beer
suggestions.

Here’s how the app will be structured:
1

The layout specifies what the app will
look like.
It includes three GUI components:
• A drop-down list of values called a spinner,
which allows the user to choose which type of
beer they want.
• A button that when pressed will return a
selection of beer types.
• A text field that displays the types of beer.

2

3

4

This is what the
layout looks like.
1

The file strings.xml includes any String
resources needed by the layout—for
example, the label of the button
specified in the layout and the types of
beer.
The activity specifies how the app
should interact with the user.
It takes the type of beer the user chooses, and
uses this to display a list of beers the user might
be interested in. It achieves this with the help of a
custom Java class.
The custom Java class contains the
application logic for the app.
It includes a method that takes a type of beer as a
parameter, and returns a list of beers of this type.
The activity calls the method, passes it the type of
beer, and uses the response.

38  Chapter 2




Layout

2




strings.xml

3
Activity

4
Custom Java

building interactive apps

Here’s what we’re going to do
So let’s get to work. There are a few steps you need to go
through to build the Beer Adviser app (we’ll tackle these
throughout the rest of the chapter):
1

2

3

4

Create a project.
You’re creating a brand-new app, so you’ll need to create a
new project. Just like before, you’ll need to create an empty
activity with a layout.

We’ll show you the
details of how to do
this on the next page.

Update the layout.
Once you have the app set up, you need to amend the layout so
that it includes all the GUI components your app needs.

Connect the layout to the activity.
The layout only creates the visuals. To add smarts to your
app, you need to connect the layout to the Java code in your
activity.




Layout

Activity

Write the application logic.
You’ll add a Java custom class to the app, and use it to make
sure users get the right beer based on their selection.
Layout
you are here 4  39

create project

Create project
Update layout
Connect activity
Write logic

Create the project
Let’s begin by creating the new app (the steps are similar to
those we used in the previous chapter):
1

 pen Android Studio and choose “Start a new Android Studio project” from
O
the welcome screen. This starts the wizard you saw in Chapter 1.

2

 hen prompted, enter an application name of “Beer Adviser” and a
W
company domain of “hfad.com”, making your package name com.hfad.
beeradviser. Make sure you uncheck the option to include C++ support.

3

4

 e want the app to work on most phones and tablets, so choose a minimum
W
SDK of API 19, and make sure the option for “Phone and Tablet” is selected.
This means that any phone or tablet that runs the app must have API 19
installed on it as a minimum. Most Android devices meet this criterion.

If your version of
Android Studio
has an option to
include Kotlin
support, uncheck
this option too.

Choose an empty activity for your default activity. Call the activity
“FindBeerActivity” and the accompanying layout “activity_find_beer”.
Make sure the option to generate the layout is selected and you uncheck the
Backwards Compatibility (AppCompat) option.

s,
The wizard will take you through these step
r
“Bee
ion
icat
appl
just like before. Call your
Adviser,” make sure it uses a minimum SDK yof
API 19, and then tell it to create an emptlayout
activity called “FindBeerActivity” and a
called “activity_find_beer”.

2

3

Make
sure you
choose
the
Empty
Activity
option.

40  Chapter 2

4

Make sure you
UNCHECK the
Backwards
Compatibility
(AppCompat)
option.

building interactive apps

We’ve created a default activity and layout
When you click on the Finish button, Android Studio creates a
new project containing an activity called FindBeerActivity.java and a
layout called activity_find_beer.xml.
Let’s start by changing the layout file. To do this, switch to the
Project view of Android Studio’s explorer, go to the app/src/main/
res/layout folder, and open the file activity_find_beer.xml. Then switch
to the text version of the code to open the code editor, and replace
the code in activity_find_beer.xml with the following (we’ve bolded all
the new code):

Click on the Text
tab to open the
code editor.

We’re replacing the code Android
Studio generated for us.





layout



We’ve just changed the code Android Studio gave us so that
it uses a . This is used to display GUI
components next to each other, either vertically or horizontally.
If it’s vertically, they’re displayed in a single column, and if it’s
horizontally, they’re displayed in a single row. You’ll find out
more about how this works as we go through the chapter.
Any changes you make to a layout’s XML are reflected in
Android Studio’s design editor, which you can see by clicking on
the Design tab. We’ll look at this in more detail on the next page.




activity_
find_beer.xml

Click on the
Design tab to open
the design editor.

you are here 4  41

design editor

A closer look at the design editor
The design editor presents you with a more visual way of editing
your layout code than editing XML. It features two different
views of the layouts design. One shows you how the layout will
look on an actual device, and the other shows you a blueprint of
its structure:

This view of the
design gives you an
idea of how your
layout will look on
an actual device.

The text view in our
layout's XML code
appears in both views
of the design editor.

Create project
Update layout
Connect activity
Write logic

If Android Studio doesn’t show
you both views of the layout, click
on the “Show Design + Blueprint”
icon in the design editor’s toolbar.

This is the
blueprint
view, which
focuses more
on the layout's
structure.

To the left of the design editor, there’s a palette that
contains components you can drag to your layout.
We’ll use this next.

This list shows you the different
categories of component you can
add to your layout. You can click
on them to filter the components
displayed in the palette.
You can increase the size of the
palette by clicking on this area
and dragging it downward.
42  Chapter 2

These
are the
components
You'll find
out more
about them
later in the
book.

building interactive apps

Create project
Update layout
Connect activity
Write logic

Add a button using the design editor
We’re going to add a button to our layout using the design editor. Find the
Button component in the palette, click on it, and then drag it into the
design editor so that it’s positioned above the text view. The button appears
in the layout’s design:

Here’s the
Button
component.
Drag it into
the design
editor.
Put the button above
the text. You can add
it to either view of
the design.

Changes in the design editor are reflected in the XML
Dragging GUI components to the layout like this is a convenient way of
updating the layout. If you switch to the code editor, you’ll see that adding
the button via the design editor has added some lines of code to the file:
...

There’s a new 

Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.6
Linearized                      : No
Page Layout                     : SinglePage
Tagged PDF                      : No
XMP Toolkit                     : Adobe XMP Core 5.2-c001 63.139439, 2010/09/27-13:37:26
Modify Date                     : 2017:07:27 10:09:33-04:00
Create Date                     : 2017:07:21 13:06:36-04:00
Metadata Date                   : 2017:07:27 10:09:33-04:00
Creator Tool                    : Adobe InDesign CC 2017 (Macintosh)
Format                          : application/pdf
Title                           : Head First Android Development
Creator                         : Dawn Griffiths and David Griffiths
Document ID                     : uuid:30167307-8351-7f49-966d-1c658614e634
Instance ID                     : uuid:b1b8626b-a382-4548-a7a3-2f608c293dfb
Producer                        : Adobe PDF Library 15.0
Page Mode                       : UseOutlines
Has XFA                         : No
Page Count                      : 930
Author                          : Dawn Griffiths
EBX PUBLISHER                   : O'Reilly Media
EXIF Metadata provided by EXIF.tools

Navigation menu