Exploring common views

Android provides a healthy set of View objects in the android.view package. These objects range from familiar constructs like the EditText, Spinner, and TextView that we have already seen in action to more specialized widgets such as AnalogClock, Gallery, DatePicker, TimePicker, and VideoView. For a quick glance at some of the more eye-catching views, check out the sample page in the Android documentation: http://code.google.com/android/reference/view-gallery.html.

The class diagram in figure 3.4 provides a high-level snapshot of what the overall View API looks like. This diagram shows how the specializations fan out and includes many, but not all, of the View-derived classes.

AnalogClock

ProgressBar

ImageView

AdapterView

n

Spinner

1

/

Gallery

\

ViewStub

ProgressBar

ImageView

DigitalClock rr

Chronometer

RadioButton

ImageButton VideoView

ViewGroup

MapView

Chronometer

CheckedTextView

AbsoluteLayout

CheckedTextView

AbsoluteLayout

TwoLineListItem

TableLayout

ScaleLayout

TwoLineListItem

TableLayout

TableRow

\

tt

TabWidget

ScaleLayout

ZoomControls

ViewAnimator

Figure 3.4 A class diagram of the Android View API, showing the root View class and specializations from there; notice that ViewGroup classes, such as layouts, are also a type of View.

RadioButton

As is evident from the diagram in figure 3.4 (which is not comprehensive), the View API has quite a few classes. ViewGroup, a special subclass of View related to layout, is a subclass of View, as are other elements such as TextView. Everything is ultimately a View, even the layout classes (which extend ViewGroup).

Of course, everything that extends View has access to the base class methods. These methods allow you to perform important UI-related operations, such as setting the background, setting the minimum height and width, setting padding, setting and enabling events (like clickable and focusable), setting layout parameters, and more. Table 3.2 includes an example of some of the methods available on the root View class.

Table 3.2 A subset of methods in the base Android View API

Method

Purpose

setBackgroundColor(int color)

Set the background color.

setBackgroundDrawable(Drawable d)

Set the background draw-able (image).

setMinimumHeight(int minHeight)

Set the minimum height (parent may override).

setMinimumWidth(int minWidth)

Set the minimum width (parent may override).

setPadding(int left, int right, int top, int bottom)

Set the padding.

setClickable(boolean c)

Set whether or not element is clickable.

setFocusable(boolean f)

Set whether or not element is focusable.

setOnClickListener(OnClickListener l)

Set listener to fire when click event occurs.

setOnFocusChangeListener(OnFocusChangeListener l)

Set listener to fire when focus event occurs.

setLayoutParams(ViewGroup.LayoutParams l)

Set the LayoutParams (position, size, and more).

Beyond the base class, each View subclass typically adds a host of refined methods to further manipulate its respective state, such as what is shown for TextView in table 3.3.

Using the combination of the base class methods with the subtype methods, you can see that you can set layout, padding, focus, events, gravity, height, width, colors, and basically everything you might need. Using these methods in code, or their counterpart attributes in the android: namespace in XML when defining views in XML (something you will see done in section 3.3), is how you manipulate a View element. Each View element you use has its own path through the API and therefore a particular set of methods available; for details on all the methods see the Android Javadocs: http://code.google.com/android/reference/android/view/View.html.

Table 3.3 Further View methods for the TextView subclass

Method

Purpose

setGravity(int gravity) setHeight(int height) setWidth(int width) setTypeFace(TypeFace face) setText(CharSequence text)

Set alignment gravity: top, bottom, left, right, and more.

Set height dimension.

Set width dimension.

Set typeface.

Set text.

When you couple the wide array of classes with the rich set of methods available from the base View class on down, the Android View API can quickly seem intimidating. Thankfully, despite the initial impression, many of the concepts involved quickly become evident, and usage becomes more intuitive as you move from View to View (because they all are specializations on the same object at the core). So even though the "747 cockpit" analogy could be applied, once you start working with Android you should be able to earn your wings fairly quickly.

Though our RestaurantFinder application will not use many of the views listed in our whirlwind tour here, these are still useful to know about, and many of them will be used in later examples throughout the book. The next thing we will focus on is a bit more detail concerning one of the most common nontrivial View elements, specifically the ListView component.

0 0

Post a comment