Introduction - Android Application Development: A Beginner's Tutorial (2015)

Android Application Development: A Beginner's Tutorial (2015)

Introduction

This book is for you if you want to learn Android application development for smart phones and tablets. Android is the most popular mobile platform today and it comes with a comprehensive set of APIs that make it easy for developers to write, test and deploy apps. With these APIs you can easily show user interface (UI) components, play and record audio and video, create games and animation, store and retrieve data, search the Internet, and so on.

The software development kit (SDK) for Android application development is free and includes an emulator, a computer program that can be configured to mimic a hardware device. This means, you can develop, debug and test your applications without physical devices.

This introduction provides an overview of the Android platform and the contents of the book.

Overview

The Android operating system is a multi-user Linux system. Each application runs as a different user in a separate Linux process. As such, an application runs in isolation from other apps.

One of the reasons for Android’s rapid ascent to the top is the fact that it uses Java as its programming language. But, is Android really Java? The answer is yes and no. Yes, Java is the default programming language for Android application development. No, Android applications do not run on a Java Virtual Machine as all Java applications do. Instead, up to Android version 4.4 all Android applications run on a virtual machine called Dalvik. In version 5.0 and later, Android sources are ultimately compiled to machine code and applications run with a new runtime called ART (Android Runtime). Android 4.4 was the turning point and shipped with both Dalvik and ART.

As for the development process, initially code written in Java is compiled to Java bytecode. The bytecode is then cross-compiled to a dex (Dalvik executable) file that contains one or multiple Java classes. The dex file, resource files and other files are then packaged using the apkbuilder tool into an apk file, which is basically a zip file that can be extracted using unzip or Winzip. APK, by the way, stands for application package.

The apk file is how you deploy your app. Anyone who gets a copy of it can install and run it on his or her Android device.

In pre-5.0 versions of Android, the apk file run on Dalvik. In version 5.0 and later, the dex file in the apk is converted into machine code when the application is installed. The machine code is executed when the user runs the application. All of this is transparent to the developer and you do not have to understand intimately the dex format or the internal working of the runtime.

An apk file can run on a physical device or the emulator. Deploying an Android application is easy. You can make the apk file available for download and download it with an Android device to install it. You can also email the apk file to yourself and open the email on an Android device and install it. To publish your application on Google Play, however, you need to sign the apk file using the jarsigner tool. Fortunately, signing an apk is easy with an integrated development environment (IDE), either it is Android Studio or ADT Eclipse.

If you’re interested in learning more about the Android build process, this web page explains the Android build process in detail.

https://developer.android.com/tools/building/index.html

Application Development in Brief

Before you embark on a long journey to becoming a professional Android application developer, you should know what lies ahead.

Before starting a project, you should already have an idea what Android devices will be your target. Most applications will target smart phones and tablets. However, the current Android release also allows you to develop apps for smart TVs and wearables. This book, however, is focused on application development for smart phones and tablets.

Then, you need to decide what versions of Android you want to support. Android was released in 2008, but at the time of writing this book there are already 21 API levels available, level 1 to level 21. Of course, the higher the level, the more features are available. However, many older phones and tablets do not run the latest Android and cannot run applications that target higher API levels than what are installed. For example, if you’re using features in API level 21, your application will not run in Android devices that support API level 20, let alone API level 2. Fortunately, Android is backward-compatible. Applications written for an earlier version will always run on newer versions. In other words, if you write applications using API level 10, your applications will work in devices that support API level 10 and later. Therefore, you would want to aim the lowest API level possible. This topic will be discussed further in the section to come.

Once you decide what Android devices to target and the API level you should write your program in, you can start looking at the API. There are four types of Android application components:

§ Activity: A window that contains user interface components.

§ Service: A long running operation that runs in the background.

§ Broadcast receiver: A listener that responds to a system or application announcement.

§ Content provider: A component that manages a set of data to be shared with other applications.

An application can contain multiple component types, even though a beginner would normally start with an application that has one or two activities. You can think of an activity as a window. You can use Android user interface components or controls to decorate an activity and as a way to interact with the user. If you are using an IDE, you can design an activity by simply dragging and dropping controls around your computer screen.

To encourage code reuse, an application component can be offered to other applications. In fact, you should take advantage of this sharing mechanism to speed up development. For instance, instead of writing your own photo capture component, you can utilize the component of the default Camera application. Instead of writing an email sending component and reinventing the wheel, you can use the system’s email application to send emails from your app.

Another important concept in Android programming is the intent. An intent is a message sent to the system or another application to request that an action be performed. You can do a lot of different things with intents, but generally you use an intent to start an activity, start a service or send a broadcast.

Every application must have a manifest, which describes the application. The manifest takes the form of an XML file and contains one or several of the following:

§ The minimum API level required to run the application.

§ The name of the application. This name will be displayed on the device.

§ The first activity (window) that will be opened when the user touches the application icon on the Home screen of his or her phone or tablet.

§ ? Whether or not you allow your application components be invoked from other applications. To promote code reuse, functionality in an application can be invoked from other applications as long as the author of the application agree to share it. For instance, the default Camera application can be invoked from other applications that need photo or video capture functionality.

§ What set of permissions the user must grant for the application to be installed on the target device. If the user does not grant all the required permissions, the application will not install.

Yes, many things require user permissions. For example, if your application needs to store data in external storage or access the Internet, the application must request the user’s permission before it can be installed. If the application needs to be automatically started when the device boots up, there is a permission for that too. In fact, there are more than 150 permissions that an application may require before it can be installed on an Android device.

Most applications are probably simple enough to only need activities and not other types of application components. Even with only activities, there is a lot to learn: UI controls, events and listeners, fragments, animation, multi-threading, graphic and bitmap processing and so on. Once you master these, you may want to look at services, broadcast receivers and content providers. All are explained in this book.

Android Versions

First released in September 2008, Android is now a stable and mature platform. The current version, version 5.0, is the 21st Android API level ever released. Table I.1 shows the code name, API level and release date of all Android major releases.

Version

Code Name

API Level

Release Date

1.0

1

September 23, 2008

1.1

2

February 9, 2009

1.5

Cupcake

3

April 30, 2009

1.6

Donut

4

September 15, 2009

2.0

Eclair

5

October 26, 2009

2.0.1

Eclair

6

December 3, 2009

2.1

Eclair

7

January 12, 2010

2.2

Froyo

8

May 20, 2010

2.3

Gingerbread

9

December 6, 2010

2.3.3

Gingerbread

10

February 9, 2011

3.0

Honeycomb

11

February 22, 2011

3.1

Honeycomb

12

May 10, 2011

3.2

Honeycomb

13

July 15, 2011

4.0

Ice Cream Sandwich

14

October 19, 2011

4.0.3

Ice Cream Sandwich

15

December 16, 2011

4.1

Jelly Bean

16

July 9, 2012

4.2

Jelly Bean

17

November 13, 2012

4.3

Jelly Bean

18

July 24, 2013

4.4

Kitkat

19

October 31, 2013

4.4w

Kitkat

20

July 22, 2014

5.0

Lollipop

21

November 3, 2014

Table I.1: Android versions

Note

Version 4.4w is the same as 4.4 but with wearable extensions.

With each new version, new features are added. As such, you can use the most features if you target the latest Android release. However, not every Android phone and tablet is running the latest release because Android devices made for older APIs may not support later releases and software upgrade is not always automatic. Table I.2 shows Android versions still in use today.

Version

Codename

API

Distribution

2.2

Froyo

8

0.5%

2.3.3-2.3.7

Gingerbread

10

9.1%

4.0.3-4.0.4

Ice Cream Sandwich

15

7.8%

4.1.x

Jelly Bean

16

21.3%

4.2.x

17

20.4%

4.3

18

7.0%

4.4

KitKat

19

33.9%

Table I.2: Android versions still in use (December 2014)

The data in Table I.2 was taken from this web page:

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

If you distribute your application through Google Play, the most popular marketplace for Android applications, the lowest version of Android that can download your application is 2.2, because versions older than 2.2 cannot access Google Play. In general you would want to reach as wide customer base as possible, which means supporting version 2.2 and up. If you only support version 4.0 and up, for example, you leave out 9.6% of Android devices, which may or may not be okay.

However, the lower the version, the fewer features are supported. Some people risk alienating some customers in order to be able to use the more recent features. To alleviate this problem, Google provides a support library that allows you to use newer features in old devices which otherwise would not be able to enjoy those features. You will learn how to use this support library in this book.

Online Reference

The first challenge facing new Android programmers is understanding the components available in Android. Luckily, documentation is in abundance and it is easy to find help over the Internet. The documentation of all Android classes and interfaces can be found on Android’s official website:

http://developer.android.com/reference/packages.html

Undoubtedly, you will frequent this website as long as you work with Android. If you had a chance to browse the website, you’d have learned that the first batch of types belong to the android package and its subpackages. After them come the java and javax packages that you can use in Android applications. Java packages that cannot be used in Android, such as javax.swing and java.nio.file, are not listed there.

Which Java Versions Can I Use?

To develop Android applications using tools such as Android Studio or ADT Eclipse, you need JDK 6 or later. Support for Java 7 language features was added to Android 4.4 (Kitkat).

At the time of writing there is no official support for Java 8 yet. However, if you are using Android Studio and have JDK 8 installed, you can already use lambda expressions, a new major feature in Java 8.

About This Book

This section outlines the contents of this book.

Chapter 1, “Getting Started” shows how to install the Android SDK and Android Studio and create a simple Android application.

Chapter 2, “Activities” explains the activity and its lifecycle. The activity is one of the most important concepts in Android programming.

Chapter 3, “UI Components” covers the more important UI components, including widgets, Toast, AlertDialog and notifications.

Chapter 4, “Layouts” shows how to lay out UI components in Android applications and use the built-in layouts available in Android.

Chapter 5, “Listeners” talks about creating a listener to handle events.

Chapter 6, “The Action Bar” shows how you can add items to the action bar and use it to drive application navigation.

Menus are a common feature in many graphical user interface (GUI) systems whose primary role is to provide shortcuts to certain actions. Chapter 7, “Menus” looks at Android menus closely.

Chapter 8, “ListView” explains about ListView, a view that shows a scrollable list of items and gets its data source from a list adapter.

Chapter 9, “GridView” covers the GridView widget, a view similar to the ListView. Unlike the ListView, however, the GridView displays its items in a grid.

Chapter 10, “Styles and Themes” discusses the two important topics directly responsible for the look and feel of your apps.

Chapter 11, “Bitmap Processing” teaches you how to manipulate bitmap images. The techniques discussed in this chapter are useful even if you are not writing an image editor application.

The Android SDK comes with a wide range of views that you can use in your applications. If none of these suits your need, you can create a custom view and draw on it. Chapter 12, “Graphics and Custom Views” shows how to create a custom view and draw shapes on a canvas.

Chapter 13, “Fragments” discusses fragments, which are components that can be added to an activity. A fragment has its own lifecycle and has methods that get called when certain phases of its life occur.

Chapter 14, “Multi-Pane Layouts” shows how you can use different layouts for different screen sizes, like that of handsets and that of tablets.

Chapter 15, “Animation” discusses the latest Animation API in Android called property animation. It also provides an example.

Chapter 16, “Preferences” teaches you how to use the Preference API to store application settings and read them back.

Chapter 17, “Working with Files” show how to use the Java File API in an Android application.

Chapter 18, “Working with the Database” discusses the Android Database API, which you can use to connect to an SQLite database. SQLite is the default relational database that comes with every Android device.

Chapter 19, “Taking Pictures” teaches you how to take still images using the built-in Camera application and the Camera API.

Chapter 20, “Making Videos” shows the two methods for providing video-making capability in your application, by using a built-in intent or by using the MediaRecorder class.

Chapter 21, “The Sound Recorder” shows how you can record audio.

Chapter 22, “Handling the Handler” talks about the Handler class, which can be used, among others, to schedule a Runnable at a future time.

Chapter 23, “Asynchronous Tasks” explains how to handle asynchronous tasks in Android.

Chapter 24, “Services” explains how to create background services that will run even after the application that started them was terminated.

Chapter 25, “Broadcast Receivers” discusses another kind of android component for receiving intent broadcasts.

Chapter 26, “The Alarm Manager” shows how you can use the AlarmManager to schedule jobs.

Chapter 27, “Content Providers” explains yet another application component type for encapsulating data that can be shared across applications.

Appendix A, “Installing the JDK” provides instructions on how to install the JDK.

Appendix B, “Using the ADT Bundle” explains how to use the ADT Bundle as an alternative IDE to develop Android apps.