Intrduction

Learning Android is like moving to a foreign city. Even if you speak the language, it will not feel like home at first. Everyone around you seems to understand things that you are missing. Things you already knew turn out to be dead wrong in this new context.

Android has a culture. That culture speaks Java, but knowing Java is not enough. Getting your head around Android requires learning many new ideas and techniques. It helps to have a guide through unfamiliar territory.

As an Android programmer, you must:

  • write Android applications
  • understand what you are writing

This guide will lead you through writing several Android applications, introducing concepts and techniques as needed. When there are rough spots, when some things are tricky or obscure, you will face them head on, and we will do our best to explain why things are the way they are.

Prerequisites

You need to be familiar with Java, including classes and objects, interfaces, listeners, packages, inner classes, anonymous inner classes, and generic classes.

If these ideas do not ring a bell, you will be in the weeds by page 2. Start instead with an introductory Java book and return afterwards.

If you are comfortable with object-oriented programming concepts, but your Java is a little rusty, you will probably be OK. We will provide some brief reminders about Java specifics (like interfaces and anonymous inner classes). Keep a Java reference handy in case you need more support as you go through the book.

How to Use This Book

  • This book is not a reference book. Rather, the goal is to get you over the initial hump to where you can get the most out of reference and recipe sites and books.
  • It is based on a five-day class. As such, it is meant to be worked through from the beginning. Chapters build on each other and skipping around is unproductive.
  • As a reader, arrange the best learning environment possible:
    • Get a good night’s rest
    • Find a quiet place to work
    • Arrange to have blocks of focused time to work on chapters
    • Start a reading group with your friends or coworkers
    • Find someone who knows Android to help you out

How This Book is Organized

As you work through this book, you will write eight Android apps. A couple are simple and take only a chapter to create. Others are more complex. The longest app spans 11 chapters. All are designed to teach you important concepts and techniques and give you direct experience using them:

  • GeoQuiz
  • CriminalIntent
  • BeatBox
  • NerdLauncher
  • PhotoGallery
  • DragAndDraw
  • Sunset
  • Locatr

Challenges

Most chapters have a section at the end with exercises for you to work through. This is your opportunity to apply what you learned, explore documentations, and do some problem solving on your own.

We strongly recommend that you do the challenges. Find your own way, solidify your learning and get confidence in your new skills.

Are you more curious?

There are also sections at the ends of chapters labeled “For the More Curious.” These sections offer deeper explanations or additional information about topics presented in the chapter. The information in these sections is not absolutely essential, but we hope you will find it interesting and useful.

Code Style

There are two areas where our choices differ from what you might see elsewhere in the Android community:

We use anonymous inner classes for listeners.

This is mostly a matter of opinion. We find it makes for cleaner code in the applications in this book because it puts the listener’s method implementations right where you want to see them. In high-performance contexts or large applications, anonymous inner classes may cause problems, but for most circumstances they work fine.

After we introduce fragments in [Chapter 7], we use them for all user interfaces.

Fragments are not an absolutely necessary tool but we find that, when used correctly, they are a valuable tool in any Android developer’s toolkit. Once you get comfortable with fragments, they are not that difficult to work with. Fragments have clear advantages over activities that make them worth the effort, including flexibility in building and presenting your user interfaces. There are people however who advocate against using fragments and we'll cover that.

Android Fragmentation

http://developer.android.com/about/dashboards/index.html

http://opensignal.com/reports/2015/08/android-fragmentation/

As you see, while there is a small amount of market-share on older versions of Android, we find that the effort required to support those versions (i.e. poor people) is not worth the reward.

As Android releases new versions, the techniques you learn here will continue to work thanks to Android’s backwards compatibility support (see Chapter 6 for details).

Downloading and Installing Android Studio

Downloading Earlier SDK Versions

To test your apps on earlier versions of Android, get the components for each platform using the Android SDK Manager:

  • If you have a project open in Android Studio, select ToolsAndroidSDK Manager
  • If you have not created a project yet, you can access the SDK Manager from the Android Setup Wizard screen. Under the Quick Start section, select ConfigureSDK Manager

Select and install each version of Android that you want to test with the latest to start with. Note that downloading may take a while.

The Android SDK Manager is also how to get Android’s latest releases, like a new platform or an update of the tools.

A Hardware Device

The emulator and Genymotion are useful for testing apps. However, they are no substitute for an actual Android device when measuring performance. If you have a hardware device, we recommend using that device at times when working through this book.