Android Programming Tutorials

Developing Mobile Apps in Java

Interested in training from the author of these tutorials? See the upcoming Android training course in Maryland, co-sponsored by Johns Hopkins Engineering for Professionals. Or, contact hall@coreservlets.com for info on customized Android courses at your location.


Following is a series of tutorials on Android programming. Since each section includes exercises and exercise solutions, this can also be viewed as a self-paced Android training course. Click on a section below to expand its content. These tutorials assume that you already know Java; they definitely move too fast for those without at least moderate prior Java experience. If you don't already know the Java language, please see the Java programming tutorial series.

Overview of the Android Tutorials

Although Web applications are important and widely used, there are several reasons why native applications are sometimes preferable for mobile apps.

  • You can use graphics and UI controls that are not available in browsers.
  • You can customize the apps for small screen sizes.
  • You can use resources (location, address book, phone, network, local database) that are unavailable in Web-based applications.

Android is the preferred platform for building corporate apps since you can install Android apps from your company Web site, rather than submitting them to the Apple App Store as required for the iPhone (unless all employees jailbreak their phones).

Click on a topic below to get the detailed tutorial for that topic, download the section's source code as an Eclipse project, see exercises, get the source code for the exercise solutions, or to simply run the sample apps from that section. Practicing is the key to learning, so I strongly recommend that you try out a few of the exercises in each section before you peek at the solutions.

These tutorials are derived from Marty Hall's world-renowned live Android training courses. Customized courses on Android are usually taught on-site at customer locations, but servlet, JSP, Ajax, GWT, JSF 2.0, Spring, Hibernate, RESTful Web Services, Android, and Java 6 training courses at public venues are periodically scheduled for people with too few developers for an onsite course. For descriptions of the various other courses that are available, please see the Java EE and Ajax training course page. To inquire about a customized training course at your location, please contact Marty at hall@coreservlets.com.

If you find these free tutorials helpful, we would appreciate it if you would link to us. Send corrections or feedback on any tutorial to hall@coreservlets.com.

Android Programming: Introduction and Overview

This section gives a high-level introduction to developing for the Android platform.

Android Programming: Getting Started

This section gets you started with the bare-bones basics of Android programming. It covers installing and configuring the necessary software and how to make and test very simple applications.

Android Programming Basics

This section shows you the basic style of Android programming, and illustrates three common variations on the theme. This section assumes that you have already installed and configured the necessary software (as described in the previous tutorial section), and that you know how to make and test applications.

Widget Event Handling

This section shows how to write code that responds to button clicks and other GUI events. It compares and contrasts five standard ways of handling events, giving pros and cons of each approach.

Widgets I: Buttons and Similar Clickable Widgets

This is the first of the sections that give examples of basic Widgets. Here, we look at Button, ImageButton, RadioButton, RadioGroup, CheckBox, and ToggleButton.

Widgets II: Spinners (Combo Boxes)

This section covers the Spinner widget, which is Android-speak for a combo box (a drop down list of choices). It also briefly explains how to switch from one Activity to another at run time, although this technique is covered in much more detail in later section on Intents and Activity switching.

Layouts: Organizing the Screen

This section covers the core Android Layout types and the design strategy of nesting one Layout inside another. It also covers defining colors in XML resource files, and very briefly introduces localization (which will be covered in detail later).

Coding Style: The Official Android Conventions

This section discusses the official Android code-style conventions. It is a good idea to follow the majority of them when writing Android apps, even if a few of the conventions are different than the style you use in your other Java projects.

  • Tutorial section in PDF (best for printing and saving).
  • Tutorial section on SlideShare (preferred by some for online viewing).
  • Topics covered:
    • Why follow conventions?
    • Valuable conventions
      • Ones that are widely considered good practice for any Java project (based on general Java industry consensus)
    • Tolerable conventions
      • Ones that do no harm, but are of questionable value (in Marty’s highly subjective opinion)
    • Dubious conventions
      • Ones that we would have been better off without (in Marty’s highly subjective opinion)
  • Eclipse preferences file. You can import this into Eclipse to get my preference file, which enforces the indentation, brace style, and @Override usage from the official Android coding conventions.

Intents Part I: Invoking Activities by Class Name

This is the first of three relatively short sections on Intents, Intent Filters, and switching from one Activity to another. In this part, we invoke the new Activity by using the specific class name.

Intents Part II: Invoking Activities with URIs

This is the second of three relatively short sections on Intents, Intent Filters, and switching from one Activity to another. In this part, we invoke the new Activity indirectly by using a URI.

Intents Part III: Invoking Activities with Tabbed Windows

This is the third of three relatively short sections on Intents, Intent Filters, and switching from one Activity to another. In this part, we invoke the new Activity by clicking on a tab in a tabbed window.

Localization and Resources

This topic covers how to make applications that adapt to changes in the user's language, screen orientation, display density, and so forth. It also reviews some of the most common resource types.

Handling Screen Rotations and App Restarts

When the user rotates the phone from portrait to horizontal mode, the app is shut down and then restarted. Apps are restarted in a few other situations as well. This topics explains how to preserve data in those situations.

Networking I: General Techniques

This section covers the basics of Android network programming as well as some refreshers on String formatting and parsing, which are particularly important topics when using string-based protocols. Techniques specific to HTTP and JSON are covered in the section after this one.

Networking II: HTTP and JSON Techniques

This section covers networking techniques specific to HTTP servers and JSON data. The basics of Android network programming are covered in the previous section.

  • Tutorial section in PDF (best for printing and saving).
  • Tutorial section on SlideShare (preferred by some for online viewing).
  • Topics covered:
    • HttpURLConnection
    • HttpClient
    • Examples: Searching Web pages
    • Using JSON
    • Example: remote loan calculations
    • Example: Google translation services
  • Sample projects used in this section, bundled as Eclipse projects.
    • Networking.zip This project includes all of the Activities used in both networking sections.
    • NetworkingSupport.zip This is a non-Android project that contains some sample String formatting and parsing programs that you can run from the command line. It also contains the servlet used in the this section.
    • DoubleTranslate.zip The DoubleTranslate app, which uses JSON data from the Google translation Web service. Google has now shut down their translation Web services, probably permanently. So, this app is still possibly educational, but will no longer function.
  • Exercises to reinforce the concepts in this section.
  • Exercise solutions.

Multithreaded Programming Part I: General Techniques

This section covers the basics of multithreaded programming as used generally in the Java programming language. The next section covers the aspects that are specific to Android. If you are already comfortable with ExecutorService, Runnable, and synchronization in "normal" Java, skip this section and move on to the next one.

Multithreaded Programming Part II: Android-Specific Techniques

This section covers aspects of multithreaded programming that are specific to Android. It assumes you are already comfortable with general Java threading as discussed in the previous section.

2D Drawing Part 1: Using onDraw

This section covers the basics of 2D drawing in Android: extending a View, drawing shapes and images, etc. It also briefly contrasts the Android approach with the Swing/AWT approach.

2D Drawing Part 2: Coordinate Transformations and ShapeDrawable

This section covers additional techniques in 2D drawing for the Android. It assumes familiarity with the basic techniques covered in the previous tutorial section.

Custom Components

This section discusses how to make custom components by extending a View, giving it drawing capability, and telling it how to calculate sizes. It assumes familiarity with the drawing techniques covered in the two previous tutorial sections.

Upcoming Topics

Several more sections coming soon. I plan to more sections periodically. Topics on my queue to cover include styles/themes, animation, location handling, Map components, embedding WebKit, playing audio and video, more widgets (menus, list views, fancy textfields), SMS messaging, local databases with SQLite, services, details on the Android Manifest file, and many more. If you have strong opinions on which topics should come next, or if you have suggestions or corrections on any of the existing topics, please email me at hall@coreservlets.com.

Source Code

Source code for all examples in this tutorial as well as the exercise solutions can be downloaded from the Android sample code repository. Code is free for completely unrestricted use. Each section above also has direct links to the code used in that section.

PowerPoint Files for University Faculty

The PDF files in this tutorial contain the complete text of the original PowerPoint files, so if your goal is learning this technology, just stick with this tutorial. However, as a service to instructors teaching full-semester courses at accredited universities, coreservlets.com will release the original PowerPoint files for free. Please see the instructor materials page for details.

More Information

Java

JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts