If you have an idea for a killer Android app, this book will help you build your first working application in a jiffy. You’ll learn hands-on how to structure your app, design interfaces, create a database, make your app work on various smartphones and tablets, and much more. It’s like having an experienced Android developer sitting right next to you! All you need is some Java know-how to get started.

Why does this book look so different?

Based on the latest research in cognitive science and learning theory, Head First Android Development uses a visually rich format to engage your mind, rather than a text-heavy approach that puts you to sleep. Why waste your time struggling with new concepts? This multi-sensory learning experience is designed for the way your brain really works.

Table of Contents

Chapter 1
Getting Started: Diving In

Welcome to Androidville

The Android platform dissected

Here’s what we’re going to do

Your development environment

Install Java

Build a basic app

Let’s build the basic app

Activities and layouts from 50,000 feet

Building a basic app (continued)

Building a basic app (continued)

You’ve just created your first Android app

Android Studio creates a complete folder structure for you

Useful files in your project

Edit code with the Android Studio editors

Run the app in the Android emulator

Creating an Android Virtual Device

Run the app in the emulator

You can watch progress in the console

Test drive

What just happened?

Refining the app

What’s in the layout?

activity_main.xml has two elements

The layout file contains a reference to a string, not the string itself

Let’s look in the strings.xml file

Take the app for a test drive

Your Android Toolbox

Chapter 2
Building Interactive Apps: Apps That Do Something

You’re going to build a Beer Adviser app

Here’s what you need to do

Create the project

We’ve created a default activity and layout

Adding components with the design editor

activity_find_beer.xml has a new button

A closer look at the layout code

Changes to the XML...

...are reflected in the design editor

Use string resources rather than hardcoding the text

Change the layout to use the string resources

Let’s take the app for a test drive

Add values to the spinner

Get the spinner to reference a string-array

Test drive the spinner

We need to make the button do something

Make the button call a method

What activity code looks like

Add an onClickFindBeer() method to the activity

onClickFindBeer() needs to do something

Once you have a View, you can access its methods

Update the activity code

The first version of the activity

What the code does

Test drive the changes

Building the custom Java class

Enhance the activity to call the custom Java class so that we can get REAL advice

Activity code version 2

What happens when you run the code

Test drive your app

Your Android Toolbox

Chapter 3
Multiple Activities and Intents: State Your Intent

Apps can contain more than one activity

Here’s the app structure

Create the project

Update the layout

Update strings.xml...

Create the second activity and layout

What just happened?

Welcome to the Android manifest file

Every activity needs to be declared

An intent is a type of message

Use an intent to start the second activity

What happens when you run the app

The story continues...

Test drive the app

Pass text to a second activity

Update the text view properties

putExtra() puts extra information in an intent

Update the CreateMessageActivity code

Get ReceiveMessageActivity to use the information in the intent

What happens when the user clicks the Send Message button

Test drive the app

We can change the app to send messages to other people

How Android apps work

But we don’t know what apps are on the device

Create an intent that specifies an action

Change the intent to use an action

What happens when the code runs

The story continues...

The intent filter tells Android which activities can handle which actions

How Android uses the intent filter

You need to run your app on a REAL device

Test drive the app

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

What happens when you call createChooser()

The story continues...

Change the code to create a chooser

Test drive the app

If you have NO matching activities

Your Android Toolbox

Chapter 4
The Activity Lifecycle: Being an Activity

How do activities really work?

The Stopwatch app

The stopwatch layout code

How the activity code will work

Add code for the buttons

The runTimer() method

Handlers allow you to schedule code

The full runTimer() code

The full StopwatchActivity code

What happens when you run the app

The story continues

Test drive the app

What just happened?

Rotating the screen changes the device configuration

From birth to death: the states of an activity

The activity lifecycle: from create to destroy

Your activity inherits the lifecycle methods

How do we deal with configuration changes?

Or save the current state...

...then restore the state in onCreate()

What happens when you run the app

The story continues

Test drive the app

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

The activity lifecycle: the visible lifetime

We need to implement two more lifecycle methods

The updated StopwatchActivity code

What happens when you run the app

Test drive the app

But what if an app is only partially visible?

The activity lifecycle: the foreground lifetime

Stop the stopwatch if the activity’s paused

What happens when you run the app

Test drive the app

The complete activity code

Your handy guide to the lifecycle methods

Your Android Toolbox

Chapter 5
The User Interface: Enjoy the View

Your user interface is made up of layouts and GUI components

Three key layouts: relative, linear, and grid

RelativeLayout displays views in relative positions

Adding padding

Positioning views relative to the parent layout

Attributes for positioning views relative to the parent layout

Positioning views relative to other views

Attributes for positioning views relative to other views

Use margins to add distance between views

RelativeLayout: a summary

LinearLayout displays views in a single row or column

A linear layout displays views in the order they appear in the layout XML