David Caunt

Mobile Software Engineer

Subscribe to my RSS feed

Archive for the ‘Android’ Category

Android Gotcha – Action Bar Home Icon Doesn’t Work

January 21st, 2012

Hopefully by now you’re using the Action Bar in your applications – it’s pretty much mandatory unless you’re making a game or something unusual. I recommend using the excellent ActionBarSherlock to deal with pre-Honeycomb platforms.

Jumping straight in with ActionBarSherlock can cause you to skip over the official ActionBar docs, leading to confusion over implementation details. On older devices the ActionBar home icon works fine and yet, on ICS, the button doesn’t work!

For packages targetting API level 14 onwards, you need to explicitly enable the home button by calling setHomeButtonEnabled()

In your onCreate, add the following:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    getActionBar().setHomeButtonEnabled(true);
}

The moral of the story is to always read the official docs and be aware of the differences between a native and compatibility library ActionBar implementation.

 

Galaxy Nexus – Android 4 ICS Easter Egg

January 8th, 2012

On your Galaxy Nexus, go to Settings -> About phone and tap on the item which displays the version ‘Android version 4.0.1′ three times.

The settings screen and status bar will disappear and you’ll see this splash. Long press on the Android to enter a Nyandroid scene!

Android Gotcha – Unable to instantiate service

October 21st, 2011

If you’re using Eclipse and create an IntentService class using the IDE’s correction features you’ll create a class like this:

public class MyService extends IntentService {
 
    public MyService(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub        
        Log.d("MyService", "onHandleIntent (intent=" + intent + ")");
    }
}

I’ve added a Log call so I can see my service receive intents.

When you trigger the service in your application, you’ll immediately see this cryptic force close error in your logcat:

E/AndroidRuntime( 7758): FATAL EXCEPTION: main
E/AndroidRuntime( 7758): java.lang.RuntimeException: Unable to instantiate service com.example.brokenservice.service.MyService: java.lang.InstantiationException: com.example.brokenservice.service.MyService
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:2177)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.access$2500(ActivityThread.java:132)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1102)
E/AndroidRuntime( 7758): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 7758): 	at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.main(ActivityThread.java:4263)
E/AndroidRuntime( 7758): 	at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 7758): 	at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 7758): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 7758): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 7758): 	at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 7758): Caused by: java.lang.InstantiationException: com.example.brokenservice.service.MyService
E/AndroidRuntime( 7758): 	at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 7758): 	at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:2174)
E/AndroidRuntime( 7758): 	... 10 more

What gives?

The clue is in the java.lang.InstantiationException, which should be the first thing you search for. This suggests an issue with the constructor for MyService.

The problem is that the OS is looking for a constructor with no arguments, while the definition of IntentService has a constructor which takes a String name. This constructor is overridden by Eclipse. The correct definition of MyService looks like this:

public class MyService extends IntentService {
 
    public MyService() {
        super("MyService");
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub      
        Log.d("MyService", "onHandleIntent (intent=" + intent + ")");
    }
}

Thanks to ddewaele on Stack Overflow for the correct answer.

Beyond the SDK – Improving the Android Development Workflow

April 7th, 2011

So you’ve got the Android SDK installed and Eclipse doesn’t seem as bad as you remember, but what else is worth setting up before you get stuck into a project? Below are extensions, tools and tips to improve your workflow.

Local documentation

If you have browsed your local Android sdk folder you may have noticed that there is a docs folder. This folder contains everything you can browse online at developer.android.com and can be accessed locally to avoid network latency and server/network downtime.

Open ‘file:///path/to/android-sdk/docs/reference/packages.html’ in your browser, replacing /path/to/android-sdk with your actual SDK path.

Note: I found that the API level filter doesn’t work in Google Chrome, probably due to local script execution. Firefox doesn’t have this problem, but if you will use Google Chrome, creating an Apache vhost for your docs is one workaround. If you know how to fix this, please leave a comment below.

Android SDK Reference search (Omnibox)

Android developer Roman Nurik has created a Google Chrome extension which allows fast Android SDK Reference search including both classes and XML elements. By simple typing ‘ad’ followed by your query, e.g. ‘ad ViewG’ you’ll see all classes beginning with ‘ViewG’.

This extension also adds links on class reference pages linking to the source on android.git.kernel.org.

Google Chrome Omnibox featuring Android SDK search

Source code for this extension is available at code.google.com and this extension could easily be customised to link to your local documentation (see above).

Android sources

Eclipse displays API documentation and autocompletes code but you can’t view the source of Android platform code within the IDE. Fortunately there is a plugin which will download and install the sources for you. You can then ctrl/cmd click on any line of code to see how it is actually implemented.  I often find this useful when documentation isn’t clear, and it’s a great way to improve your own code.

Coloured logcat output

Jeff Sharkey has written a great python script for colouring logcat output – colouredlogcat.py. This script simply colours lines from different programs in different colours, making logs more readable. I recommend putting this one on your PATH.

Sample output from colouredlogcat.py

Android Asset Studio

Don’t have an awesome in-house designer? Again Roman Nurik has your back with his Android Asset Studio, a set of web-based tools for generating image assets for your project. These are useful for prototyping but can generate production quality assets without launching Photoshop.

Themes

Roman Nurik’s Android themes won’t increase your productivity but they will make your browser more awesome! There’s a honeycomb theme and a traditional Android version.

WebView gradient backgrounds

April 5th, 2011

One of those easy questions shot up on Stack Overflow where a few people quickly jump in with an answer. How do you set a gradient background on a view?

Three answers later and the question poster refines his question: “What about a WebView?” While we all assumed the answer to be the same, WebViews ignore any background drawable used while a background colour is set. The default background colour for a WebView is white, so you need to make it transparent.

Simply specify 0 as your colour and add a drawable in code or via layout XML.

WebView webview = (WebView)findViewById(R.id.wv);
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.gradient);

You can view the original discussion on Stack Overflow.

Stack Overflow

David Caunt's Stack Overflow badge

Zend Certified Engineer - PHP5 & Zend Framework

Zend PHP5 Certified Engineer Zend Framework Certified Engineer

Top