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.

This tends to trip people up when it comes to seemingly common elements like a button with both image and text centered.

{% img /images/posts/centered-button.png %}

I’ve seen a lot of ridiculous solutions to this, including this one, which involve invisible button overlays and complex layouts used in high profile apps.  This can greatly affect performance by added layout complexity and make view management and maintenance extremely difficult.

Exploring the Button

The Button class is nothing more than a TextView, really.  Go ahead, check the source.  What many people don’t notice is that the Button’s click handling is all covered at the View level.  If you require this type of layout, or any fancy button layout, in fact, there is no need for all of this complexity and magic.

{% img /images/posts/three-buttons.png %}

All three buttons above look and function the same, except that they are laid out slightly differently.

{% codeblock lang:xml %}