Book Image

Android Application Development Cookbook - Second Edition

By : Kyle Mew
Book Image

Android Application Development Cookbook - Second Edition

By: Kyle Mew

Overview of this book

The Android OS has the largest installation base of any operating system in the world; there has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community! This “cookbook” will make it easy for you to jump to a topic of interest and get what you need to implement the feature in your own application. If you are new to Android and learn best by “doing,” then this book will provide many topics of interest. Starting with the basics of Android development, we move on to more advanced concepts, and we’ll guide you through common tasks developers struggle to solve. The first few chapters cover the basics including Activities, Layouts, Widgets, and the Menu. From there, we cover fragments and data storage (including SQLite), device sensors, the camera, and GPS. Then we move on more advanced topics such as graphics and animation (including OpenGL), multi-threading with AsyncTask, and Internet functionality with Volley. We’ll also demonstrate Google Maps and Google Cloud Messaging (also known as Push Notifications) using the Google API Library. Finally, we’ll take a look at several online services designed especially for Android development. Take your application big-time with full Internet web services without having to become a server admin by leveraging the power of Backend as a Service (BaaS) providers.
Table of Contents (23 chapters)
Android Application Development Cookbook Second Edition
Credits
Disclaimer
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

Android was first released in 2007 after being acquired by Google, Inc. Initially, Android was primarily used on a handset. Android 3.0 added features to take advantage of the growing tablet market.

In 2014, Google announced that Android had over 1 billion active users! With over 1 million applications available on Google Play, there's never been a more exciting time to join the Android community!

As we begin 2016, we have the recently released Android 6.0 with exciting new features for both users and developers.

What this book covers

Chapter 1, Activities, discusses Activities, which represent the fundamental building blocks for most applications. See examples of the most common tasks, such as creating an activity and passing control from one activity to another.

Chapter 2, Layouts, talks about Layout options; while Activities are fundamental to the UI, the layout actually defines what the user sees on the screen. Learn the main layout options available and best practices.

Chapter 3, Views, Widgets, and Styles, explores the basic UI object, from which all layouts are built. Widgets include everything from buttons and textboxes to more complicated NumberPicker and Calendar dialogs.

Chapter 4, Menus, teaches you how to use menus in Android. Learn how to create menus and how to control their behavior at runtime.

Chapter 5, Exploring Fragments, AppWidgets, and the System UI, shows how to create more flexible user interfaces by reusing UI components with Fragments. Take advantage of new OS features with translucent system bars or even make the System UI go away completely with Immersive Mode.

Chapter 6, Working with Data, helps you discover multiple methods that Android offers for persisting data, and know when it is the best to use each option. The Loader class example shows an efficient solution to present the data without tying up the UI Thread.

Chapter 7, Alerts and Notifications, shows multiple options for displaying notifications to your users. Options range from alerts in your application, using the system notification, and the Heads Up notification.

Chapter 8, Using the Touchscreen and Sensors, helps you learn the events for handling the standard user interactions, such as button clicks, long presses, and gestures. Access the device hardware sensors to determine orientation changes, device movement, and compass bearing.

Chapter 9, Graphics and Animation, helps you bring your app to life with animations! Take advantage of the many options Android offers for creating animations—from simple bitmaps to custom property animations.

Chapter 10, A First Look at OpenGL ES, discusses the OpenGL; when you need high-performance 2D and 3D graphics, turn to the Open Graphics library. Android supports OpenGL, a cross-platform Graphics API.

Chapter 11, Multimedia, takes advantage of the hardware features for playing audio. Use Android intents to call the default camera application or delve into the camera APIs to control the camera directly.

Chapter 12, Telephony, Networks, and the Web, uses the Telephony functions to initiate a phone call and to listen for incoming phone events. See how to send and receive SMS (text) messages. Use the WebView in your application to display web pages and learn how to use Volley to communicate directly with web services.

Chapter 13, Getting Location and Using Geofencing, shows you how to determine the user's location and the best practices so your app doesn't drain the battery. Use the new Location APIs to receive location updates and create Geofences.

Chapter 14, Getting Your App Ready for the Play Store, helps you polish your app for the Play Store and learn how to implement more advanced features, such as alarms and AsyncTask for background processing. See how to add Google Cloud Messaging (push notification) to your app and take advantage of Google Sign-in.

Chapter 15, The Backend as a Service Options, explores what a Backend as a Service provider can offer your app. Compare several top providers offering native Android support and free subscription options.

What you need for this book

Developing Android applications requires the Android SDK, available on multiple platforms, including Windows, Mac, and Linux.

Though not required, this book uses Android Studio, the official Android IDE. If you are new to Android development, visit the following link to review the current system requirements and download Android Studio with the SDK bundle for your platform:

http://developer.android.com/sdk/index.html

The Android SDK and Android Studio are both free of charge.

Who this book is for

This book assumes basic familiarity with programming concepts and Android fundamentals. Otherwise, if you are new to Android and learn best by jumping into the code, this book provides a wide range of the most common tasks.

As a "cookbook", it's easy to jump to your topic of interest and get the code working in your own application as quickly as possible.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, and user input are shown as follows: "Requesting a JSON response using JsonObjectRequest() basically works the same as StringRequest()."

A block of code is set as follows:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Use the default Phone & Tablet option and select Empty Activity when prompted for Activity Type."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.