Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

50 Android Hacks
50 Android Hacks
50 Android Hacks
Ebook331 pages2 hours

50 Android Hacks

Rating: 4 out of 5 stars

4/5

()

Read preview

About this ebook

Summary

The best programming techniques are often the shortest and simplest—the hacks. In this compact and infinitely useful book, Android expert Carlos Sessa delivers 50 hacks that will save you time, stretch your skills, and maybe even make you smile.

About this Book

Hacks. Clever programming techniques to solve thorny little problems. Ten lines of code that save you two days of work. The little gems you learn from the old guy in the next cube or from the geniuses on Stack Overflow. That's just what you'll find in this compact and useful book.

The name 50 Android Hacks says it all. Ranging from the mundane to the spectacular, each self-contained, fully illustrated hack is just a couple of pages long and includes annotated source code. These practical techniques are organized into twelve collections covering layout, animations, patterns, and more.

What's Inside
  • Hack 3 Creating a custom ViewGroup
  • Hack 8 Slideshow using the Ken Burns effect
  • Hack 20 The Model-View-Presenter pattern
  • Hack 23 The SyncAdapter pattern
  • Hack 31 Aspect-oriented programming in Android
  • Hack 34 Using Scala inside Android
  • Hack 43 Batching database operations
  • Plus 43 more hacks!

Most hacks work with Android 2.x and greater. Version-specific hacks are clearly marked.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Author
Carlos Sessa is a passionate professional Android developer. He's active on Stack Overflow and is an avid hack collector.

Table of Contents
  1. Working your way around layouts
  2. Creating cool animations
  3. View tips and tricks
  4. Tools
  5. Patterns
  6. Working with lists and adapters
  7. Useful libraries
  8. Interacting with other languages
  9. Ready-to-use snippets
  10. Beyond database basics
  11. Avoiding fragmentation
  12. Building tools
LanguageEnglish
PublisherManning
Release dateJun 2, 2013
ISBN9781638352839
50 Android Hacks
Author

Carlos Sessa

Carlos Sessa is a passionate full-time Android developer. He's active on StackOverflow and is an avid hack collector.

Related to 50 Android Hacks

Related ebooks

Programming For You

View More

Reviews for 50 Android Hacks

Rating: 4 out of 5 stars
4/5

2 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    50 Android Hacks - Carlos Sessa

    Copyright

    For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact

         Special Sales Department

         Manning Publications Co.

         20 Baldwin Road

         PO Box 261

         Shelter Island, NY 11964.

    ©2013 by Manning Publications Co. All rights reserved.

    No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.

    Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.

    Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.

    ISBN 9781617290565

    Printed in the United States of America

    1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13

    Dedication

    Al milagro que hizo esto posible

    (To the miracle that made this possible)

    Brief Table of Contents

    Copyright

    Brief Table of Contents

    Table of Contents

    Foreword

    Preface

    Acknowledgments

    About this Book

    About the Cover Illustration

    Chapter 1. Working your way around layouts

    Chapter 2. Creating cool animations

    Chapter 3. View tips and tricks

    Chapter 4. Tools

    Chapter 5. Patterns

    Chapter 6. Working with lists and adapters

    Chapter 7. Useful libraries

    Chapter 8. Interacting with other languages

    Chapter 9. Ready-to-use snippets

    Chapter 10. Beyond database basics

    Chapter 11. Avoiding fragmentation

    Chapter 12. Building tools

    Index

    List of Figures

    Table of Contents

    Copyright

    Brief Table of Contents

    Table of Contents

    Foreword

    Preface

    Acknowledgments

    About this Book

    About the Cover Illustration

    Chapter 1. Working your way around layouts

    Hack 1 Centering views using weights: Android v1.6+

    1.1. Combining weightSum and layout_weight

    1.2. The bottom line

    1.3. External links

    Hack 2 Using lazy loading and avoiding replication: Android v1.6+

    2.1. Avoid replication using the tag

    2.2. Lazy loading views with the ViewStub class

    2.3. The bottom line

    2.4. External links

    Hack 3 Creating a custom ViewGroup: Android v1.6+

    3.1. Understanding how Android draws views

    3.2. Creating the CascadeLayout

    3.3. Adding custom attributes to the children

    3.4. The bottom line

    3.5. External links

    Hack 4 Preferences hacks: Android v1.6+

    4.1. The bottom line

    4.2. External links

    Chapter 2. Creating cool animations

    Hack 5 Snappy transitions with TextSwitcher and ImageSwitcher: Android v1.6+

    5.1. The bottom line

    5.2. External links

    Hack 6 Adding eye candy to your ViewGroup’s children: Android v1.6+

    6.1. The bottom line

    6.2. External links

    Hack 7 Doing animations over the Canvas: Android v1.6+

    7.1. The bottom line

    7.2. External links

    Hack 8 Slideshow using the Ken Burns effect: Android v1.6+

    8.1. The bottom line

    8.2. External links

    Chapter 3. View tips and tricks

    Hack 9 Avoiding date validations with an EditText for dates: Android v1.6+

    9.1. The bottom line

    9.2. External links

    Hack 10 Formatting a TextView’s text: Android v1.6+

    10.1. The bottom line

    10.2. External links

    Hack 11 Adding text glowing effects: Android v1.6+

    11.1. The bottom line

    11.2. External links

    Hack 12 Rounded borders for backgrounds: Android v1.6+

    12.1. The bottom line

    12.2. External links

    Hack 13 Getting the view’s width and height in the onCreate() method: Android v1.6+

    13.1. The bottom line

    13.2. External links

    Hack 14 VideoViews and orientation changes: Android v1.6+

    14.1. The bottom line

    14.2. External links

    Hack 15 Removing the background to improve your Activity startup time: Android v1.6+

    15.1. The bottom line

    15.2. External links

    Hack 16 Toast’s position hack: Android v1.6+

    16.1. The bottom line

    16.2. External links

    Hack 17 Creating a wizard form using a Gallery: Android v2.1+

    17.1. The bottom line

    17.2. External links

    Chapter 4. Tools

    Hack 18 Removing log statements before releasing: Android v1.6+

    18.1. The bottom line

    18.2. External links

    Hack 19 Using the Hierarchy Viewer tool to remove unnecessary views: Android v1.6+

    19.1. The bottom line

    19.2. External links

    Chapter 5. Patterns

    Hack 20 The Model-View-Presenter pattern: Android v1.6+

    20.1. The bottom line

    20.2. External links

    Hack 21 BroadcastReceiver following Activity’s lifecycle: Android v1.6+

    21.1. The bottom line

    21.2. External links

    Hack 22 Architecture pattern using Android libraries: Android v1.6+

    22.1. Back-end logic and model

    22.2. Android library

    22.3. Android application

    22.4. The bottom line

    22.5. External links

    Hack 23 The SyncAdapter pattern: Android v2.2+

    23.1. Common approaches

    23.2. What we’ll create

    23.3. The bottom line

    23.4. External links

    Chapter 6. Working with lists and adapters

    Hack 24 Handling empty lists: Android v1.6+

    24.1. The bottom line

    24.2. External links

    Hack 25 Creating fast adapters with a ViewHolder: Android v1.6+

    25.1. The bottom line

    25.2. External links

    Hack 26 Adding section headers to a ListView: Android v1.6+ Contributed by Chris King

    26.1. Creating list layouts

    26.2. Providing visible section headers

    26.3. Wrapping up

    26.4. The bottom line

    26.5. External links

    Hack 27 Communicating with an Adapter using an Activity and a delegate: Android v1.6+

    27.1. The bottom line

    27.2. External links

    Hack 28 Taking advantage of ListView’s header: Android v1.6+

    28.1. The bottom line

    28.2. External links

    Hack 29 Handling orientation changes inside a ViewPager: Android v1.6+

    29.1. The bottom line

    29.2. External links

    Hack 30 ListView’s choiceMode: Android v1.6+

    30.1. The bottom line

    30.2. External links

    Chapter 7. Useful libraries

    Hack 31 Aspect-oriented programming in Android: Android v1.6+

    31.1. The bottom line

    31.2. External links

    Hack 32 Empowering your application using Cocos2d-x: Android v2.2+

    32.1. What is Cocos2d-x?

    32.2. Using Cocos2d-x

    32.3. The bottom line

    32.4. External links

    Chapter 8. Interacting with other languages

    Hack 33 Running Objective-C in Android: Android v1.6+

    33.1. Downloading and compiling Itoa

    33.2. Creating the modules

    33.3. Setting up the Java part

    33.4. The bottom line

    33.5. External links

    Hack 34 Using Scala inside Android: Android v1.6+

    34.1. The bottom line

    34.2. External links

    Chapter 9. Ready-to-use snippets

    Hack 35 Firing up multiple intents: Android v2.1+

    35.1. Taking a picture

    35.2. Picking a picture from the gallery

    35.3. Mixing both intents

    35.4. The bottom line

    35.5. External links

    Hack 36 Getting user information when receiving feedback: Android v1.6+

    36.1. The bottom line

    36.2. External links

    Hack 37 Adding an MP3 to the media ContentProvider: Android v1.6+

    37.1. Adding the MP3 using content values

    37.2. Adding the MP3 using the media scanner

    37.3. The bottom line

    37.4. External links

    Hack 38 Adding a refresh action to the action bar: Android v2.1+

    38.1. The bottom line

    38.2. External links

    Hack 39 Getting dependencies from the market: Android v1.6+

    39.1. The bottom line

    39.2. External links

    Hack 40 Last-in-first-out image loading: Android v2.1+ Contributed by William Sanville

    40.1. Starting point: Android sample application

    40.2. Introducing executors

    40.3. UI thread—leaving and returning seamlessly

    40.4. Considerations

    40.5. The bottom line

    40.6. External links

    Chapter 10. Beyond database basics

    Hack 41 Building databases with ORMLite: Android v2.2+ Contributed by William Sanville

    41.1. A simple data model

    41.2. Getting started

    41.3. Rock-solid database schema

    41.4. SQLiteOpenHelper—your gateway to the database

    41.5. Singleton pattern for database access

    41.6. CRUD operations made easy

    41.7. Query builders

    41.8. Data types and tricky foreign types

    41.9. Raw SQL queries

    41.10. Transactions

    41.11. The bottom line

    41.12. External links

    Hack 42 Creating custom functions in SQLite: Android v1.6+

    42.1. Java code

    42.2. Native code

    42.3. The bottom line

    42.4. External links

    Hack 43 Batching database operations: Android v2.1+

    43.1. No batch

    43.2. Using batch operations

    43.3. Applying batch using SQLiteContentProvider

    43.4. The bottom line

    43.5. External links

    Chapter 11. Avoiding fragmentation

    Hack 44 Handling lights-out mode: Android v1.6+

    44.1. Android 2.x

    44.2. Android 3.x

    44.3. Merging both worlds in a single Activity

    44.4. The bottom line

    44.5. External links

    Hack 45 Using new APIs in older devices: Android v1.6+

    45.1. Using apply() instead of commit()

    45.2. Storing the app on the SD card

    45.3. The bottom line

    45.4. External links

    Hack 46 Backward-compatible notifications: Android v1.6+

    46.1. The bottom line

    46.2. External links

    Hack 47 Creating tabs with fragments: Android v1.6+

    47.1. Creating our tab UI

    47.2. Placing the tabs in an Activity

    47.3. The bottom line

    47.4. External links

    Chapter 12. Building tools

    Hack 48 Handling dependencies with Apache Maven: Android v1.6+

    48.1. The bottom line

    48.2. External links

    Hack 49 Installing dependencies in a rooted device: Android v1.6+

    49.1. Predexing

    49.2. Creating the permissions XML

    49.3. Modifying AndroidManifest.xml

    49.4. The bottom line

    49.5. External links

    Hack 50 Using Jenkins to deal with device diversity: Android v1.6+ Contributed by Christopher Orr

    50.1. Creating a Jenkins job

    50.2. Running the job

    50.3. The bottom line

    50.4. External links

    Index

    List of Figures

    Foreword

    Android as an ecosystem is expanding rapidly in all directions. Every day manufacturers introduce new devices and form factors, consumers purchase and activate over one million devices, and users download and try new apps. It’s the job of developers (yourself included, hopefully) to fill this ecosystem with beautiful, engaging, and deeply fulfilling applications through which users can better interpret and interact with their world.

    As a platform, Android was birthed in late 2003 by former employees of Danger (the company behind the popular Sidekick phones). In 2005 the company driving Android was acquired by Google, and three years later the HTC Dream (G1) was released as the first consumer device running Android. Over the next three years the hardware and platform were heavily iterated, but Android remained solely a phone operating system.

    In 2011 Google introduced two new form factors for the Android: tablets and TV. This represented the first official deviation from phones as the device of choice and sparked manufacturer interest in other devices. Android now runs on laptops, wrist-watches, video game consoles, and car stereos. It can only be expected that in the future the number of devices supporting Android will continue to grow.

    As application developers, it’s extremely important that you understand the diversity of the platform and the direction in which it’s heading. Creating content on Android is no longer as simple as designing for a phone-sized screen held in portrait orientation. While this does mean more work for the developer creating apps, the end result is a vastly more pleasant experience for the user, regardless of which device your content is consumed on.

    In developing applications there are three major things that you’ll need aside from your own creativity and desire to develop: the platform documentation, the open source community, and glue to hold everything together. The platform documentation is easy, since the latest version is always hosted at http://developer.android.com. The open source community is spread across GitHub, Google Code, Stack Overflow, and the like, providing libraries, code snippets, and design patterns for simplifying development. You still need something to tie these disjointed pieces together as one cohesive app. If it were as simple as arranging a few building blocks, everyone would be developing applications. This book is that glue.

    Contained in the book are examples of how to solve common problems that arise in Android development. Some are relatively trivial and some quite complex. What they share, however, is being loosely or sparsely documented facets of app development which often cause developers pain. 50 Android Hacks is not meant as a sole resource for learning or mastering Android development, but rather exists to fill in the cracks.

    It’s a great task to craft an app that’s dynamic enough to support Android’s growing device diversity. With the knowledge provided by this book, accompanied by that of similar print and online sources, it’s my hope that you’re more empowered to develop and publish apps. Beyond this, while I am a developer just like you, I am also an avid Android user and patiently await that next great application. Perhaps you will be the one to write it.

    JAKE WHARTON

    ANDROID ENGINEER

    Preface

    I started learning about Android back in 2009. Android version 1.5 had just been released, and it showed a lot of potential.

    In July 2009, thanks to a friend living in Australia, I got my first Android-powered device, an HTC Magic with Android version 1.5. To be honest, it processed more slowly than I expected, but I started testing the APIs and creating apps that I wanted to have on my cell phone. I sensed that Android would get a lot of attention and I knew that if I managed to create an application, it would be available to a lot of people.

    I was proved right—not long afterward, there was a kick-off for Android development, which soon grew bigger and bigger. Suddenly a lot of tools and third-party libraries supporting the Android platform emerged—everything from game frameworks, like cocos2d-x, to build systems, like Apache Maven.

    In November 2010 I was asked to review a book from Manning Publications called Android in Practice (www.manning.com/collins/). Delving deep into Manning’s work, it occurred to me that I could write a book about Android development using a different approach. I wanted to imitate Joshua Bloch’s Effective Java (www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683), providing tips and patterns I had learned over all my years of developing for the Android platform.

    Essentially, I wanted to gather together in one book every Android tip I have learned and provide some degree of documentation for it. That’s what 50 Android Hacks is all about: a collection of tips gathered in the process of developing different Android applications.

    Something I enjoyed about Effective Java was that the book doesn’t have any particular order and I could read various sections, learning something different from each of them. After some time, I would go back to the book and find a different application for the project I was working on. I kept that in mind while writing this book. I imagine the reader investigating a hack while going to work or before going to sleep, getting new ideas for the project they’re working on.

    I’m already using this book on my new projects, copying the sample code for certain tasks and using its examples to explain to my coworkers certain patterns.

    Enjoying the preview?
    Page 1 of 1