Cannot start Android Emulator (initial hax sync failed)

If you see something like this when booting Android Emulator:

It is likely that one of the following app is running:

  1. Virtualbox
  2. Docker
  3. Other virtual machine applications

Stop all of them, and try again.

Using SoundPool in Android

If your app/game need to play a sound when user click certain object, you will quickly found out that using MediaPlayer is a bad idea. This is what SoundPool is for, however, its API is still a bit unfriendly. This is some notes on how to use it and my suggested usage pattern.

Obtaining a SoundPool object

It is recommended to use the Builder, but lower SDK device does not support that, so this Continue reading “Using SoundPool in Android”

Vibrator examples – Android Programming

This article will cover how to use the Vibrator in Android.

Firstly, in your AndroidManifest.xml, you must have this line under the root element, otherwise you will get a Runtime Exception

Quick start

if you just want to vibrate it once, this is the simplest example Continue reading “Vibrator examples – Android Programming”

Solving Genymotion “unable to connect to your virtual device” Error

So I know you are here because you get this error message while starting a Genymotion Device.

genymotion_1

First you should go check your firewall settings as suggested in the Genymotion FAQ. Other than that, if the problem still persist, 9/10 it’s the problem of your Host-only adapter settings. Here’s how to correctly set it. Continue reading “Solving Genymotion “unable to connect to your virtual device” Error”

Android Activity transition slide in/out animation

This is a very common effect, however the Android framework only provides the slide in animation. This is how I implement all the sliding transition animation. There’s in total 4 XMLs to define the animation, and with the use of overridePendingTransition() in appropriate location you can implement this easily.

pull_in_left.xml  Continue reading “Android Activity transition slide in/out animation”

Inspect the view hierarchy (layout) of any Android app

Ever wondering how a particular app structure their hierarchy of views? With the latest Android SDK(I’m using 21.01 currently) and Eclipse ADT plugin, you can finally do that!

1. Connect your phone, Open the app you are curious about. Switch to the DDMS perspective, See the little button over there? click on it.insepct_view_hierarchy_1

2. The following screen will appear, you can now browse the view hierarchy just like a DOM tree which Web developers are familiar with.

insepct_view_hierarchy_2

What is does is basically dump a snapshot of the view in the current screen to the IDE. There’s quite a lot of information you can see in the “Node Detail” section, but you probably cannot reconstruct a layout XML in your own code by these info because It doesn’t show you some attributes like layout_width, layout_height, layout_weight etc. But it definitely tells you what types of views you need to build a particular layout.

Why Learning Android could improve your programming skills

Android is probably the most popular mobile operating system, a lot of programmers join the Android club to be an Android developer and turn their ideas into great apps. While some of you may not be interested in making a mobile app, but I would say you should give it a try. In this article I’ll try to convince you that you can improve your programming skills by studying the Android framework.

It’s simple

By simple, I don’t mean programming in Android is easy. The Google Android team has made it very easy for beginners to get started, just fire up the Eclipse IDE and load the examples and you have your first Android app. Through Android you’ll learn how to organize an app that is so simple to understand, write and deploy. You could apply this in organizing your web application. There’s a lot of good engineering practice in the Android framework. Of course, programming is never easy, if you feel that what you’re doing is easy, you maybe not making something useful!

It’s well documented

Learning Android is a joy. Continue reading “Why Learning Android could improve your programming skills”

Patterns for running Asynchronous code in Android (Part 1)

An important technique in Android Programming is to make potentially long task run asynchronously in your app, avoid blocking the UI thread, so your app feels more responsive. For example, downloading a file from the Internet, loading a media file from the external storage or applying a filter on an image bitmap etc. I will show you how to implement with AsyncTask, simple java threads and java Executor in Part 1.

Let’s say I want to load a bitmap from the internet, and set it to an ImageView , this might be what you’d write at first:

The Java way: simple Threads

You can implement it just like any other Java programs, define a Thread that does the job, and run it. Every Java programmer knows how to do that. Remember that if you want to modify the UI, you need to use runOnUiThread()

Continue reading “Patterns for running Asynchronous code in Android (Part 1)”

[Android] Using LruCache to cache Bitmaps and limit memory usage

Bitmaps are very bulk objects, loading them from the Internet or local storage could be expensive and “choke” the UI. You want to  keep them in memory so that they could be loaded instantly, however you also want to discard them as soon as they are not useful. Here comes the LruCache.

What is LruCache?

LruCache is a util introduced in API 12, which is an implementaion of the Least Recently Used cache. In short, that is a caching strategy which the “oldest” entries are discarded when overflow occur. It is very useful to cache Bitmaps. The default implementation allows you to limits the number of items in the cache. Here’s how to slightly modify the LruCache to limit the memory usage of the cache.

How to use it?

Well, actually it act just like a HashMap. Continue reading “[Android] Using LruCache to cache Bitmaps and limit memory usage”