50 Android Hacks
Carlos M. Sessa
  • June 2013
  • ISBN 9781617290565
  • 216 pages
  • printed in black & white

How to solve common problems that arise in Android development.

From the Foreword by Jake Wharton, Android Engineer

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.

Table of Contents show full




about this book

about the cover illustration

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

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

2.1. Avoid replication using the <include /> tag

2.2. Lazy loading views with the ViewStub class

2.3. The bottom line

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

Hack 4 Preferences hacks: Android v1.6+

4.1. The bottom line

2. Creating cool animations

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

5.1. The bottom line

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

6.1. The bottom line

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

7.1. The bottom line

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

8.1. The bottom line

3. View tips and tricks

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

9.1. The bottom line

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

10.1. The bottom line

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

11.1. The bottom line

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

12.1. The bottom line

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

13.1. The bottom line

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

14.1. The bottom line

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

15.1. The bottom line

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

16.1. The bottom line

17.1. The bottom line

4. Tools

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

18.1. The bottom line

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

19.1. The bottom line

5. Patterns

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

20.1. The bottom line

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

21.1. The bottom line

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

Hack 23 The SyncAdapter pattern: Android v2.2+

23.1. Common approaches

23.2. What we’ll create

23.3. The bottom line

6. Working with lists and adapters

Hack 24 Handling empty lists: Android v1.6+

24.1. The bottom line

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

25.1. The bottom line

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

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

27.1. The bottom line

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

28.1. The bottom line

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

29.1. The bottom line

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

30.1. The bottom line

7. Useful libraries

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

31.1. The bottom line

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

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

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

34.1. The bottom line

9. Ready-to-use snippets

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

35.1. Taking a picture

35.3. Mixing both intents

35.4. The bottom line

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

36.1. The bottom line

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

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

38.1. The bottom line

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

39.1. The bottom line

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

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

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

42.1. Java code

42.2. Native code

42.3. The bottom line

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

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

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

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

46.1. The bottom line

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

12. Building tools

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

48.1. The bottom line

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

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


About the 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!

About the reader

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

About the author

Carlos Sessa is a passionate professional Android developer. He's active on Stack Overflow and is an avid hack collector.

combo $34.99 pBook + eBook
eBook $27.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

One of the best how-to books I’ve read!

Christian Badenas, Android and .NET Developer

Packed with useful Android development tidbits not found in the official documentation.

Matthias Käppler, SoundCloud

A great resource for creating nontrivial user experiences for the Android platform.

Frank Ableson, Coauthor of Android in Action