About this Book - 50 Android Hacks (2013)

50 Android Hacks (2013)

About this Book

Android is a project with a lot of momentum. The first Android release happened on September 23, 2008, and by the end of 2010 it had become the leading smartphone platform.

Every time there’s a new release, a new set of APIs and possibilities show up. While Android version 1.5 (Donut) only worked in the HTC Dream, right now Android runs in many devices from cellphone to TVs, and on different sizes of tablets and laptops.

This causes two big problems when developing for Android. First, you have to deal with different types of supported devices. While there are lots of ways of dealing with different screen sizes and screen density, you need to create an app that works, and looks great, in every device. Also, targeting every possible Android-powered device might result in different user experiences. The user won’t interact in the same way with a cellphone as with a TV.

The second problem is how long the Android versions stay alive. The story is always the same: with a new Android version, we get new APIs. A new API would be an excellent addition to your app, but as a developer you still need to support older versions, because not everyone will get the update and also because it may take a lot of time to reach your main target audience.

You’ll need to choose if you want to add the new API functionality and release an app just for people using the newest Android version, or go with a hybrid approach where some functionalities are only available in newer versions.

I’ve created this book to help you out, because when you’re developing for Android, all the decisions are in your hands. 50 Android Hacks offers a problem/solution approach to tasks you might encounter while developing, but also ways to enhance what’s already there.

What is Android?

Android is an open source operating system based on Linux. In the beginning, it was just for cell phones, but now it works on tablets, TVs, computers, and even car stereos. It has been gaining a lot of momentum in the mobile scene and is now used in more than 50% of mobile devices.

The apps that run on an Android-powered device are usually coded in Java and it has a powerful SDK that allows the developer to create different types of applications. Android allows developers to customize almost everything. For example, you can create custom wallpapers, custom keyboards, and custom home screens, things you wouldn’t imagine doing in other platforms.

Who should read this book?

This book is intended for people who are already developing with Android. I assume you know how to program in Java and the basic concepts of the Android platform.

There are hacks intended for people taking their first steps with the Android platform, and there are hacks for advanced developers. If you’re developing an Android app, skim through the book; I’m sure you’ll find something that will help you.

To find out if this book is for you, consider these questions:

· Are you developing for Android?

· Have you found yourself scratching your head, trying to think of better solutions to your problems?

· Are you looking for new ways of addressing your programming issues?

· Do you want to find out how other people are handling similar problems?

How to use this book

My recommendation is that, before you read about a hack, you first compile and run the sample code. That will give you a better understanding of what we’ll do in each example. Apart from that, the book doesn’t need to be read in any particular order. Feel free to start reading any section that interests you.


While the book is flexible enough to let you go forward and backward between hacks without an issue, you can also read it sequentially.

· Chapter 1, “Working your way around layouts,” has four hacks that offer you different layout tips.

· The four hacks in chapter 2, “Creating cool animations,” describe different tips for dealing with animations.

· Chapter 3, “View tips and tricks,” has nine hacks covering every tip related to views.

· The two hacks in chapter 4, “Tools,” give you an overview of available tools apart from the IDE.

· Chapter 5, “Patterns,” offers pattern examples in its four hacks that are applicable for Android.

· Chapter 6, “Working with lists and adapters,” groups tips about the ListView and Adapter classes in its seven hacks.

· Two hacks in chapter 7, “Useful libraries,” explain how to use third-party libraries in your apps.

· Chapter 8, “Interacting with other languages,” shows some examples of coding for Android in programming languages other than Java in one hack focused on Objective-C and one hack discussing Scala.

· Chapter 9, “Ready-to-use snippets,” offers six hacks that provide copy-and-paste code snippets.

· The three hacks in chapter 10, “Beyond database basics,” state some advanced tips about database usage.

· Chapter 11, “Avoiding fragmentation,” includes four hacks that show how to make your app work in different Android versions.

· The final three hacks presented in chapter 12, “Building tools,” include tips on how to build your app.

Code conventions and downloads

All the code in the examples used in this book is presented in a monospace font like this. Annotations accompany many of the code listings and numbered cueballs are used if longer explanations are needed.

The source code for all of the examples in the book is available for download from the publisher’s website at www.manning.com/50AndroidHacks. You can also download the source code from the Google code project. How to get the latest code is explained in the appendix. The sample code is hosted at GitHub. You can download the code here: https://github.com/Macarse/50AH-code.

To run the book samples, you’ll need to install

· Eclipse

· Android SDK

· Eclipse Android plugin

If you don’t know where to start, I recommend visiting http://developer.android.com/sdk/installing/index.html, where there’s an easy step-by-step guide to configuration.