326 CHAPTER 9: Android Graphic Design: Making Your UI Designs Visual
The ImageButton Class: Multi-state Buttons
Android’s ImageButton class is a direct subclass of the ImageView class, which is itself a subclass
of the View superclass, which, as you learned in Chapter 6, is a subclass of the java.lang.Object
master class, and is primarily utilized to create UI widgets and layout containers (via the ViewGroup
superclass). The Android class hierarchy for the ImageButton class would thus be structured as
follows:
java.lang.Object
android.view.View
android.widget.ImageView
android.widget.ImageButton
The ImageButton class, like its parent class ImageView, is stored in a separate Android package
for UI widgets, which is called the android.widget package. This is because the ImageButton is
an often-used UI widget which can be leveraged to create custom button UI widgets which can be
“skinned” using Android Drawable objects.
This makes the ImageButton class extremely powerful, as Android Drawable objects can be
BitmapDrawable (images), NinePatchDrawable (asymetric tiling imagery), AnimationDrawable
(animation), ShapeDrawable (illustrations), and any of the other Android Drawable subclasses. I wish
I could cover all of these Android Drawable subclasses within this book; however, if you wanted to
explore Android Drawables further, you should check out the Apress Pro Android Graphics title.
The ImageButton UI widget would be used when developers need to create a custom button
UI element, which will display the button using an image instead of using a standard text label
on a square gray background, as the standard UI Button element would do. We have already
implemented both Button and ImageButton elements in previous chapters that covered user
interface layout design classes, including the LinearLayout, RelativeLayout, GridLayout, and
SlidingPaneLayout.
Just like the Android Button class UI widget, an ImageButton UI widget can be pressed, using a
click or a touch event by the user, and can have button focus characteristics defined as well by
implementing multi-state images.
As you saw in the previous chapter, if you don’t utilize any of its custom parameters, your
ImageButton widget will have a visual appearance of a standard Android Button UI object with a
gray button background which changes color to blue when the button is pressed. The real power of
the ImageButton class comes when you use it with alpha channel capable images, in conjunction
with multiple image states, both of which you will be learning about in detail during this chapter.
The ImageButton UI widget can define up to four different ImageButton “states” which are defined
using XML markup, which we will be doing a bit later on in this chapter. We will cover these
ImageButton states in detail in the next section of this chapter, after I cover a couple of the key XML
parameters and Java methods here in this section on the ImageButton class member methods and
features.