Customer Service 1-800-221-5528

Murach’s Android Programming (2nd Edition)

by Joel Murach
18 chapters, 686 pages, 289 illustrations
Published December 2015
ISBN 978-1-890774-93-6
List price: $57.50

This is a great book for a first course in Android programming. To start, it shows how to install and configure Android Studio and the Android SDK. Then, it presents a series of complete Android apps that illustrate key skills…working with widgets, layouts, event handling, fragments, services, broadcast receivers, SQLite databases, content providers, Google Maps, and much more!

The only prerequisite for this book is a basic set of Java skills equivalent to chapters 1-18 of Murach's Beginning Java with NetBeans or Murach's Beginning Java with Eclipse.

Buy this book

You are currently on the Murach site for instructors. To buy this book, please visit our retail site.

I like the fact that the book approaches the subject by using example apps as the theme of study, and working through the skills incrementally from easy to difficult. These case-study apps are complete apps by themselves and are worthy of being in the Android app stores.”

Jason Ong, ASP.NET World

  • About this Book
  • Table of Contents
  • Courseware
  • FAQs
  • Corrections

Book description

This is a great book for a first course in Android programming. By using realistic Android apps, it engages students, shows them how everything they’re learning can be applied in the real world, and gives them ideas for their own apps.

Here’s a brief rundown on each section of this book.

Section 1: Get started fast

In just 4 chapters, your students will quickly master the basics of Android programming. That includes:

  • essential concepts and terms for Android programming
  • how to use Android Studio to develop their first Android app, a simple but complete Tip Calculator app
  • how to use Android Studio to thoroughly test and debug apps

Section 2: The essential Android skills

Next, your students will learn the Android essentials as they enhance the Tip Calculator app that they developed in section 1. That includes:

  • how to use layouts and widgets to develop a sophisticated user interface
  • how to handle high- and low-level events
  • how to use themes and styles to format an app in a way that’s consistent and easy to maintain
  • how to use menus and preferences
  • how to use fragments to take advantage of the large screens that are available on devices like tablets

Section 3: The News Reader app

Here, your students will learn how to develop a News Reader app that displays current news to the user. This will help you teach them:

  • how to read an RSS feed from the Internet, save that data in a file, and display it on the user interface
  • how to use a service to download data for an app even when the app isn’t running
  • how to notify a user that new data is available even when the app isn’t the current app
  • how to respond to actions that are broadcast by the Android operating system or by other apps

Section 4: The Task List app

In this section, you can teach your students how to develop a Task List app that stores one or more to-do lists. Along the way, they’ll learn:

  • how to create a database and use it to store data
  • how to use tabs and a custom adapter to display data on the user interface
  • how to use a content provider to allow other apps to work with the same data as this app
  • how to create an app widget that can display some of this app’s data on a device’s Home screen

Section 5: Advanced Android skills

Using this section, you can cover some advanced skills for developing and deploying apps, including:

  • how to create a Run Tracker app that tracks the location of a device on a Google Map
  • how to deploy apps to the Google Play store

The prerequisites for the course

The only prerequisite is basic understanding of Java, equivalent to chapters 1-18 of Murach's Beginning Java with NetBeans or Murach's Beginning Java with Eclipse.

What software your students need

The software that your students need for this book can be downloaded for free from the Internet:

  • the Java SDK (Software Development Kit)
  • Android Studio, which includes the Android SDK

Appendixes A, B, and C in this book show an easy way to install and configure this software on Windows, Mac OS X, and Linux systems.

What hardware we recommend

If necessary, your students can use an emulator to run and test Android apps; this book shows how. Unfortunately, emulators tend to run extremely slowly on most computers. As a result, we recommend that your students use an actual Android device such as a smartphone or tablet whenever possible.

What software this book supports

The Android releases

Over 95% of all Android devices use Android 4.0 (API 15) or later, and this book reflects that. The only app that won’t work with Android 4.0 is the News Reader app, which is designed to work with Android 4.1 (API 16) and later.

At the time this book was written, the most recent release was Android 6.0 (API 23). But since Android is backwards-compatible, all of the apps will continue to work under subsequent releases, too.

The operating systems

The software you need for developing Android apps is available for Windows, Mac OS X, and Linux. The appendixes in this book show how to download and install this software on all three of these operating systems.

The IDE

This book shows you how to use Android Studio to code, test, and debug applications. Android Studio is the official IDE for Android development, so it’s what the professionals use. It’s also available for free, and it runs on all operating systems.

Related books for related courses

For beginning programming or core Java courses, we offer two editions of our Beginning Java text: Murach's Beginning Java with NetBeans and Murach's Beginning Java with Eclipse. As an alternative for core Java courses where students already have some programming experience, we also offer Murach’s Java Programming (4th Edition).

For Java web programming courses, we offer Murach’s Java Servlets and JSP (3rd Edition). Like our Android text, it interfaces easily with any of our core Java texts.

What people say about this book

“Want to get into Android programming fast and seamlessly? This book is all you need to create an application from scratch and publish it successfully to Google Play Market.”
- Igor Rolinsky, Chicago Java User Group

“As someone who first learnt Android programming from a confusing jumble of resources strewn across the Internet - from Google's tutorials to StackOverflow questions to any number of random blog posts that search threw up - I can honestly say I wish I had read this book first, because it would have saved me an awful amount of time and confusion.
     “The book takes the reader along a gentle basics-to-advanced learning path that quite accurately addresses at every step what a new developer should learn next, both on the front end and back end.”
- Karthik Shiraly, CodeRanch

“This book is geared towards folks looking to get a handle on a complex topic. Walking through the chapters, it really presents material in a great fashion. The exercise and homework questions are on point, the writing clear, and the example applications germane and useful. A perfect book for someone who is stuck on the hurdle of ‘where do I start.’”
- Jeremy Johnson, Dream in Code

“Developing Android apps can be really challenging even to seasoned programmers. Much of the challenge has to do with having to make an app work in a very fragmented Android platform. I'm glad that the book makes great effort in highlighting the crucial differences between the API versions and providing best-practice guidance on implementing back-compatibility support.
     “Of note is that the book doesn't just leave readers will the essential programming skills but also takes readers through the important app publishing process on Google Play store. This is really end-to-end stuff.
     “Taking into consideration the well-structured content, clear and concise writing style, practical code samples and exercises, I think this book serves as an excellent learning tool to those who are getting their feet wet in the world of Android programming or who just want to update their core Android programming skills.”
- Jason Ong, ASPNETWorld

To view the table of contents for this book in a PDF: Table of Contents

Click on any chapter title to display or hide its content.

Section 1 Get started fast with Android

Chapter 1 An introduction to Android and Android Studio

An overview of Android

Types of devices

Types of apps

A brief history

Versions

System architecture

How apps are compiled and run

An introduction to Android Studio

How to work with the Welcome page

How to open an existing project

How to view the user interface for an app

How to view the code for an app

How to run an app on a physical device

How to run an app on an emulator

The user interface

Chapter 2 How to start your first Android app

The Tip Calculator app

The user interface

How to create a new project

How to develop the user interface

How to work with a layout

How to add widgets to a layout

How to set the display text

How to work with resource files

How to set properties

Common properties

The XML for the user interface

Chapter 3 How to finish your first Android app

How to write the Java code

How to work with an activity

How to remove the v7 appcompat support library

How to get references to widgets

How to handle the EditorAction event

How to get and set the text for widgets

The lifecycle of an activity

How to save and restore values

The Java code for the app

More skills for finishing an app

How to work with the Gradle build script

How to work with dependencies

How to work with the Android manifest

How to set the launcher icon for an app

How to use the documentation for the Android API

Chapter 4 How to test and debug an Android app

Basic skills for testing and debugging

Typical test phases

How to check the layout

How to handle runtime errors

How to trace code execution

How to use LogCat

How to use toasts

How to use the debugger

How to set and remove breakpoints

How to step through code

How to inspect variables

How to inspect the stack trace

How to create emulators

How to create an emulator for a tablet

How to create an emulator for a phone with a hard keyboard and DPad

Section 2 Essential Android skills

Chapter 5 How to work with layouts and widgets

An introduction to layouts and widgets

A summary of layouts

A summary of widgets

The View hierarchy

How to work with layouts

How to use a linear layout

How to use a table layout

How to use a frame layout

How to nest layouts

How to provide a landscape layout

How to work with widgets

How to use editable text views

How to use check boxes

How to use radio buttons

How to use spinners

How to use seek bars

How to display images

How to show and hide widgets

How to add scroll bars

How to display web content

Chapter 6 How to handle events

A summary of listeners

High-level events

Low-level events

Four techniques for handling events

How to use the current class as the listener

How to use a named class as the listener

How to use an anonymous class as the listener

How to use an anonymous inner class as the listener

When to use each technique

How to handle high-level events

How to handle events for check boxes and radio buttons

How to handle events for radio groups

How to handle events for spinners

How to handle events for seek bars

How to handle low-level events

How to handle Key events

How to handle Touch events

The Tip Calculator app

The user interface

The Java code for the activity

Chapter 7  How to work with themes and styles

An introduction to themes and styles

Three common themes

How to supply different themes for different APIs

How to convert an existing project to the AppCompat theme

How to work with styles

How to define a style

How to apply a style

How to create a style sheet

How to work with themes

How to modify a built-in theme

How to modify the AppCompat theme

How to modify a theme depending on the API

How to modify the text appearance for a theme

A summary of built-in themes

How to apply themes

How to work with colors

How to define colors

How to apply colors

Chapter 8  How to work with menus and preferences

How to work with menus

An introduction to menus

How to define a menu

How to display an options menu

How to handle option menu events

How to start a new activity

How to work with preferences

An introduction to preferences

How to define preferences

How to display preferences

How to get preferences

How to use preferences

More skills for working with preferences

How to group preferences

How to enable and disable preferences

How to use Java to work with preferences

Chapter 9 How to work with fragments

An introduction to fragments

Single-pane and multi-pane layouts

The lifecycle methods of a fragment

How to use single-pane layouts for small screens

How to create the layout for a fragment

How to create the class for a fragment

How to display a fragment in an activity

How to create a preference fragment

How to display a preference fragment in an activity

How to use multi-pane layouts for large screens

How to add multiple fragments to a layout

How to detect screen width

How to control the soft keyboard

Other skills for working with fragments

How to get a reference to a fragment

How to replace one fragment with another

Section 3 The News Reader app

Chapter 10 How to work with threads, files, adapters, and intents

An introduction to the News Reader app

The user interface

The XML for an RSS feed

How to work with threads

How threads work

How to execute asynchronous tasks

How to execute timed tasks

How to update the UI thread

How to work with files

How to download a file from the Internet

How to parse an XML file

The RSSFeedHandler class

The RSSFeed class

The RSSItem class

How to work with adapters

How to create the layout for a list view

How to use an adapter to display data in a list view

How to handle events for an adapter

How to work with intents

How to pass data between activities

How to view a URL in a web browser

How to dial or call a phone number

The News Reader app

The activity_items layout

The ItemsActivity class

The FileIO class

The activity_item layout

The ItemActivity class

Chapter 11 How to work with services and notifications

How to work with the Application object

How to define the Application object

How to register the Application object

How to use the Application object

How to work with services

The lifecycle of a service

How to create a service

How to register a service

How to start and stop a service

How to use threads with services

How to test a service

How to view all services

How to work with notifications

How notifications work

How to create a pending intent

How to create a notification

How to work with system services

How to display or remove a notification

How to check if a network connection is available

The News Reader app

The NewsReaderService class

The ItemsActivity class

The FileIO class

Chapter 12 How to work with broadcast receivers

How to work with system broadcasts

A summary of the system broadcasts

How to code a receiver for the boot completed broadcast

How to code a receiver for the connectivity changed broadcast

How to work with custom broadcasts

How to create and send a custom broadcast

How to code a receiver for a custom broadcast

Section 4 The Task List app

Chapter 13 How to work with SQLite databases

An introduction to databases

The user interface for the Task List app

An introduction to SQLite

An introduction to the Task List database

The business objects for the Task List app

How to create a database class

How to define the constants for a database

How to define the SQL statements that create a database

How to create or upgrade a database

How to open and close a database connection

How to add public methods to a database class

How to retrieve multiple rows from a table

How to retrieve a single row from a table

How to get data from a cursor

How to insert, update, and delete rows

How to test the database

How to test the database class

How to clear test data from a device

How to work with the database file

How to use DB Browser for SQLite

Chapter 14 How to work with tabs and custom adapters

How to use tabs

How to add the TabManager class to your project

The layout for an activity that displays tabs

The class for an activity that displays tabs

The class for a fragment that displays tab content

How to use a custom adapter

A layout for a list view item

A class that extends the layout for a list view item

A class for a custom adapter

A class for a fragment that uses a custom adapter

The Task List app

The user interface

The activity_task_list menu

The TaskListActivity class

The activity_add_edit and spinner_list layout

The activity_add_edit menu

The AddEditActivity class

Chapter 15 How to work with content providers

An introduction to content providers

URIs for content providers

MIME types for content providers

How to add supporting methods to the database class

How to create a content provider

How to start a content provider class

How to provide for querying

How to provide for inserting rows

How to provide for updating rows

How to provide for deleting rows

How to register a content provider

How to use a content provider

How to use a custom content provider

How to use a built-in content provider

How to work with a dialog box

How to import the dialog class and interface

How to build and show the dialog box

The Task History app

The user interface

The XML for the layouts

The Java code for the activity

Chapter 16 How to work with app widgets

An introduction to app widgets

A Home screen that has app widgets

How to remove app widgets

How to add app widgets

How to create app widgets

How to create the layout

How to modify the database class

How to create the provider class

How to configure an app widget

How to register an app widget

How to test an app widget

Section 5 Advanced Android skills

Chapter 17 How to deploy an app

An introduction to distribution and monetization

How distribution works

How monetization works

How to create a release build

How to prepare an app for release

How to create the signed APK file

How to distribute directly to the user

How to distribute via a website

How distribute via email

How to publish on Google Play

How to set up a publisher account

How to use the Developer Console to publish an app

How to view the listing for an app

Chapter 18 How to work with locations and maps

An introduction to locations and maps

The user interface for the Run Tracker app

An introduction to determining location

An introduction to maps

An introduction to the Google Maps Android API

How to configure the Google Maps Android API v2

How to create a new Google Maps project

How to add Google Play services to an existing project

How to get a Google Maps API key

How to set permissions and features

How to work with locations

How to connect to Google Play services

How to get the current location

How to handle a failed connection

How to get location updates

How to make sure GPS is enabled

How to work with Google Maps

How to add a map fragment to a layout

How to display a map

How to zoom in on a location

How to add markers

How to add lines

The Run Tracker app

The activity_run_map layout

The RunMapActivity class

The RunTrackerService class

If you aren’t already familiar with the supporting courseware that we provide for a book, please go to About our Courseware. As you will see, our courseware consists of the end-of-chapter activities in the book, the files in the student download at our retail site, and the instructor’s materials. These components provide everything that other publishers provide, but in a way that delivers better results.

If you are familiar with our courseware, here’s a quick summary of the courseware for this book. For a detailed description in PDF format, please read the Instructor’s Summary.

End-of-chapter activities in the book

  • Term lists
  • Chapter summaries
  • Practice exercises

Student download at our retail site

  • Source code for the apps in the book
  • Starting source code for the book exercises
  • Solutions to the book exercises

In the book, the appendixes give your students complete instructions for downloading and installing these items.

Instructor’s materials

  • Instructional objectives by chapter
  • PowerPoint slides for classroom presentations
  • Test banks in multiple formats
  • Additional exercises that aren’t in the book, plus their starting points and solutions
  • Projects that the students start from scratch and their solutions
  • The files that students can download at our retail site: (1) the book apps, (2) starting points for the exercises in the book, and (3) solutions to the exercises in the book

Have there been recent changes to Android Studio?

Since Murach’s Android Programming (2nd Edition) was published in 2015, there have been some significant improvements to Android Studio that affect chapters 1 and 2. So we’ve created a FREE PDF that shows how to take advantage of these improvements. You can incorporate this material into your course after you’ve covered pages 1-15 of chapter 1. 

Your students can access this download by going to the FREE Downloads tab on the book page at our retail site. And you can download this PDF right now.

When I create a new project, it uses the ConstraintLayout, not the RelativeLayout described in chapter 2. How do you get the ConstraintLayout to work for the Tip Calculator app?

Android Studio 2.3 and later use the ConstraintLayout as the default layout for a new project and provide integrated tools for working with this layout. The following document describes how to use this layout to create the user interface for the Tip Calculator app:
Android Studio Update for 2017

When I run the News Reader app from chapter 10, it crashes. How can I fix this?

The RSS feed from the CNN website has changed significantly since this book was published. In particular, the title element is often empty, which causes the description to be stored in the title field of the RSSItem object. Also, many items no longer have a pubDate element that stores the publication date. This missing publication date causes the app to crash. You can prevent the app from crashing by modifying the getPubDateFormatted method of the RSSItem class to handle a null publication date like this:

public String getPubDateFormatted() {
    try {
        if (pubDate != null) {
            Date date = dateInFormat.parse(pubDate.trim());
            String pubDateFormatted = dateOutFormat.format(date);
            return pubDateFormatted;
        }
        else {
            return "No date in RSS feed";
        }

    }
    catch (ParseException e) {
        throw new RuntimeException(e);
    }
}

However, a better solution is to modify the RSSFeedHandler and RSSItem classes so that they do a better job of parsing the RSS feed. New and improved versions of these classes are now included in the download for this book here:

\book_apps\ch10_NewsReader_Fixed

These classes substitute a truncated description for the title if the title is missing. In addition, they parse a publication date from the URL for the original link to the item. This publication date no longer includes a time component, but it works.

If you want, you can fix the News Reader apps for the exercises and other chapters by using the code for the new RSSFeedHandler and RSSItem classes instead of the old code that’s described in the book.

To keep the News Reader apps for chapters 11 and 12 working, the download for this book now uses the fixed RSSFeedHandler and RSSItem classes for these apps. Similarly, it uses these fixed classes for all exercise starts and solutions that are based on the News Reader app.

When I run the Run Tracker app from chapter 18, it crashes when I click on the Start button. How can I fix this?

Android 6.0 (API 23) and later and later request permissions at runtime when the user needs them instead of requesting them when they install the app. Since the Run Tracker app presented in Murach’s Android Programming does not request these permissions at runtime, it crashes on API 23 and later when you click the Start button of the Stopwatch activity. To fix this issue, you can add the following code to the StopwatchActivity class:

// add this constant to the StopwatchActivity class
private final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;

// add this code to the onStart method of the StopwatchActivity class
if (Build.VERSION.SDK_INT > 22) {
    if (checkSelfPermission(
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
    }
}

For more details about how this code works, you can view the last figure of the following document:
Android Studio Update for 2017

To view the corrections for this book in a PDF, just click on this link: View the corrections

Then, if you find any other errors, please email us so we can correct them in the next printing of the book. Thank you!

Murach college books and courseware since 1974