Android Tips: Making a Custom Button out of Anything

The Android APIs are filled with things that don’t quite seem right.  I’ve always been annoyed by the lack of relationship between interface elements.  For instance, isn’t a ListView really just a special case of a GridView with only one column?  Yet GridView and ListView aren’t directly related, so we get oddities where I can add header and footer views to a ListView, but not a GridView.

One thing that seems to trip people up is the Button class.  Android has a Button class for text (and images if your not really concerned with formatting them) and an ImageButton class for images.  Like the ListView/GridView situation mentioned earlier, these two buttons are not, in fact, related.  Button inherits from TextView and ImageButton inherits from ImageView.

Read More →

The case for Fragment Callbacks

Fragment are a great addition to Android.  They allow reusability of sections of your views at the controller layer, and they also provide better encapsulation of your app’s logic.  This can really help developers speed up code development and also keep their code clean, which makes it easier to maintain.  All of these benefits are available if you follow a few guidelines when developing your code.

One of the biggest mistakes that I see developers make, particularly those coming from other platforms, is casting the parent activity.  The Fragment API provides methods for communicating with the parent activity because there are many things, like navigation, that should not be handled by the Fragment.  This requires a mechanism for the Fragment to notify the parent Activity when certain activities occur.

Read More →