The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
One of the classic forms of code reuse is the GUI widget. Since the advent of Microsoft Windows — and, to some extent, even earlier – developers have been creating their own widgets to extend an existing widget set. These range from 16-bit Windows “custom controls” to 32-bit Windows OCX components to the innumerable widgets available for Java Swing and SWT, and beyond. Android lets you craft your own widgets as well, such as extending an existing widget with a new UI or new behaviors.
Understanding this chapter requires that you have read the core chapters of this book.
You have five major options for creating a custom
First, your “custom
View class” might really only be custom
Drawable resources. Many widgets can adopt a radically different
look and feel just with replacement graphics. For example, you might
think that these toggle buttons from the Android 2.1 Google Maps
application are some fancy custom widget:
Figure 617: Google Maps navigation toggle buttons
In reality, those are just radio buttons with replacement images.
Second, your custom
View class might be a simple subclass of an
existing widget, where you override some behaviors or otherwise
inject your own logic. Unfortunately, most of the built-in Android
widgets are not really designed for this sort of simple subclassing,
so you may be disappointed in how well this particular technique
Third, your custom
View class might be a composite widget —
akin to an activity’s contents, complete with layout and such, but
encapsulated in its own class. This allows you to create something
more elaborate than you will just by tweaking resources. We will see
this later in the chapter with
Fourth, you might want to implement your own layout manager, if your
GUI rules do not fit well with
other built-in containers. For example, you might want to create a
layout manager that more closely mirrors the “box model” approach
taken by XUL and Flex, or you might want to create one that mirrors
FlowLayout (laying widgets out horizontally until there is
no more room on the current row, then start a new row).
Finally, you might want to do something totally different, where you
need to draw the widget yourself. For example, the
SeekBar widgets to control the mix of red, blue, and
green. But, you might create a
ColorWheel widget that draws a
spectrum gradient, detects touch events, and lets the user pick a
color that way.
Some of these techniques are fairly simple; others are fairly complex. All share some common traits, such as widget-defined attributes, that we will see throughout the remainder of this chapter.
The preview of this section was fed to a gremlin, after midnight.
The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.
The preview of this section was traded for a bag of magic beans.
The preview of this section is en route to Mars.