Table Of ContentAndroid Application Development
A Beginner’s Tutorial
Budi Kurniawan
Android Application Development: A Beginner’s Tutorial
First Edition: February 2015
All rights reserved. No part of this book may be reproduced or transmitted in any
form or by any means, electronic or mechanical, including photocopying, recording,
or by any information storage and retrieval system, without written permission from
the publisher, except for the inclusion of brief quotations in a review.
ISBN: 9780992133016
Indexer: Chris Mayle
Trademarks
Oracle and Java are registered trademarks of Oracle and/or it’s affiliates
UNIX is a registered trademark of the Open Group
Apache is a trademark of The Apache Software Foundation.
Firefox is a registered trademark of the Mozilla Foundation.
Google is a trademark of Google, Inc.
Throughout this book the printing of trademarked names without the trademark
symbol is for editorial purpose only. We have no intention of infringement of the
trademark.
Warning and Disclaimer
Every effort has been made to make this book as accurate as possible. The author and
the publisher shall have neither liability nor responsibility to any person or entity
with respect to any loss or damages arising from the information in this book.
About the Author
Budi Kurniawan is known for his clear writing style. A consultant at Brainy Software
Corp., he has written software licensed by Fortune 100 companies and architected and
developed large scale applications for various organizations around the world. He has also
published more than 100 articles in prestigious publications. His other books include the
popular “How Tomcat Works” and “Servlet and JSP: A Tutorial.”
Table of Contents
Introduction
Overview
Application Development in Brief
Android Versions
Online Reference
Which Java Versions Can I Use?
About This Book
Code Download
Chapter 1: Getting Started
Downloading and Installing Android Studio
Creating An Application
Running the Application on the Emulator
The Application Structure
Debuging Your Application
The Android SDK Manager
Creating An Android Virtual Device
Running An Application on A Physical Device
Opening A Project in Android Studio
Using Java 8
Getting Rid of the Support Library
Summary
Chapter 2: Activities
The Activity Lifecycle
ActivityDemo Example
Changing the Application Icon
Using Android Resources
Starting Another Activity
Activity-Related Intents
Summary
Chapter 3: UI Components
Overview
Using the Android Studio UI Tool
Using Basic Components
Toast
AlertDialog
Notifications
Summary
Chapter 4: Layouts
Overview
LinearLayout
RelativeLayout
FrameLayout
TableLayout
Grid Layout
Creating A Layout Programmatically
Summary
Chapter 5: Listeners
Overview
Using the onClick Attribute
Implementing A Listener
Summary
Chapter 6: The Action Bar
Overview
Adding Action Items
Adding Dropdown Navigation
Going Back Up
Summary
Chapter 7: Menus
Overview
The Menu File
The Options Menu
The Context Menu
The Popup Menu
Summary
Chapter 8: ListView
Overview
Creating A ListAdapter
Using A ListView
Extending ListActivity and Writing A Custom Adapter
Styling the Selected Item
Summary
Chapter 9: GridView
Overview
Using the GridView
Summary
Chapter 10: Styles and Themes
Overview
Using Styles
Using Themes
Summary
Chapter 11: Bitmap Processing
Overview
Bitmap Processing
Summary
Chapter 12: Graphics and Custom Views
Overview
Hardware Acceleration
Creating A Custom View
Drawing Basic Shapes
Drawing Text
Transparency
Shaders
Clipping
Using Paths
The CanvasDemo Application
Summary
Chapter 13: Fragments
The Fragment Lifecycle
Fragment Management
Using A Fragment
Extending ListFragment and Using FragmentManager
Summary
Chapter 14: Multi-Pane Layouts
Overview
A Multi-Pane Example
Summary
Chapter 15: Animation
Overview
Property Animation
An Animation Project
Summary
Chapter 16: Preferences
SharedPreferences
The Preference API
Using Preferences
Summary
Chapter 17: Working with Files
Overview
Creating a Notes Application
Accessing the Public Storage
Summary
Chapter 18: Working with the Database
Overview
The Database API
Example
Summary
Chapter 19: Taking Pictures
Overview
Using Camera
The Camera API
Using the Camera API
Summary
Chapter 20: Making Videos
Using the Built-in Intent
MediaRecorder
Using MediaRecorder
Summary
Chapter 21: The Sound Recorder
The MediaRecorder Class
Example
Summary
Chapter 22: Handling the Handler
Overview
Example
Summary
Chapter 23: Asynchronous Tasks
Overview
Example
Summary
Chapter 24: Services
Overview
The Service API
Declaring A Service
A Service Example
Summary
Chapter 25: Broadcast Receivers
Overview
BroadcastReceiver-based Clock
Canceling A Notification
Summary
Chapter 26: The Alarm Service
Overview
Example
Summary
Chapter 27: Content Providers
Overview
The ContentProvider Class
Creating A Content Provider
Consuming A Content Provider
Summary
Appendix A: Installing the JDK
Downloading and Installing the JDK
Appendix B: Using the ADT Bundle
Installing the ADT
Creating An Application
Running An Application on An Emulator
Logging
Debugging An Application
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