A (175)

(Tuis.) #1

170 CHAPTER 6: Android Screen Design: Writing to the Display Using Activity and View


Next, we will take a closer look at the relationship between the ViewGroup (layout container) and the
View (UI widget) superclasses in Android, since the ViewGroup superclass is actually a subclass of
the View superclass. It is interesting to note that a superclass can also be a subclass! ViewGroup
inherits characteristics like margin settings, which as you will see, are supported in both layout
containers (ViewGroup subclasses) and UI widgets (View subclasses), thanks to Java inheritance, as
well as to how Android has coded these superclass structures.


The ViewGroup Class: A Subclass of View


Even though the View widgets are nested inside of ViewGroup layout containers, the ViewGroup
superclass is actually underneath (subclassed from) the View superclass in the Java class hierarchy.
Starting with a Java Object master class, the inheritance hierarchy is structured as follows:


java.lang.Object



android.view.View
android.view.ViewGroup



The reason that the Android OS development team structured (coded) the View class hierarchy in
this fashion is because some of the View class attributes, for instance, top, bottom, left, and right
margin attributes (properties, or parameters), will also be available for use in a ViewGroup layout
container, as well as in all View UI widgets.


Thus, the logical Java structure is to subclass ViewGroup from View, so that the ViewGroup
subclasses inherit all of the same attributes and methods that the View subclasses will also feature.
If you want to take a closer look at what this Android View superclass includes, you can see for
yourself, by visiting the following URL:


http://developer.android.com/reference/android/view/View.html


If you look at the View class reference page on the Android developer web site, you will see that
the View class has 11 known direct subclasses, one of which is ViewGroup. Some of the other
common UI widgets such as the TextView, which you have already used in your app, as well as
ImageView and AnalogClock, which we will eventually be using in your application UI design, are
also subclassed directly from the View class.


Note A known direct subclass is a subclass that has been created from the class that is being
documented on that developer web site class reference page. “Known” means it has been officially added
to the Android API. If you subclass your own Android class, it would be called an unknown direct subclass,
because it is “unknown” to the Android API. A known indirect subclass would represent any subclass of a
known direct subclass. It is said to be indirect to the class being documented, because it is more than one
level away from, and therefore not a direct subclass of, the currently documented class.
Free download pdf