EOverride
87Using resources
In the ReviewDetail class we are first defining View components that we will later ref- erence from resources
B
. From there we see a Handler that is used to perform a net- work call to populate an ImageView based on a URL. This doesn’t relate to resources but is included here for completeness. Don’t worry too much about the details of this here, as it will be covered more when we specifically discuss networking in chapter 5C
. After the Handler, we set the layout and View tree using setContentView(R.layout.review_ detail)D
. This maps to an XML layout file at src/res/layout/review_detail.xml. Next we also reference some of the View objects in the layout file directly through resources and corresponding IDsE
.Views that are defined in XML are inflated by parsing the XML and injecting the corresponding code to create the objects for you. This is handled automatically by the platform. All of the View and LayoutParams methods we have discussed previously have counterpart attributes in the XML format. This inflation approach is one of the most important aspects of View-related resources, and it makes them very convenient to use and reuse. We will examine the layout file we are referring to here and the spe- cific views it contains more closely in the next section.
You reference resources in code, such as we are here, through the automatically generated R class. The R class is made up of static inner classes (one for each resource type) that hold references to all of your resources in the form of an int value. This value is a constant pointer to an object file through a resource table (which is con- tained in a special file the aapt tool creates and the R file utilizes).
The last reference to resources in listing 3.6 is for the creation of our menu items
F
. For each of these we are referencing a String for text from our own local resources, and we are also assigning an icon from the android.R.drawable resources namespace. You can qualify resources in this way and reuse the platform drawables: icons, images, bor- ders, backgrounds, and so on. You will likely want to customize much of your own appli- cations and provide your own drawable resources, which you can do, but the platform resources are also available if you need them (and they are arguably the better choice in terms of consistency for the user, if you are calling out to well-defined actions as we are here: map, phone call, and web page).We will cover how all the different resource types are handled and where they are placed in source in the next several sections. The first types of resources we will look at more closely are those of layouts and views.
3.3.3 Defining views and layouts through XML resources
As we have noted in several earlier sections, views and layout can be, and often are, defined in XML rather than in Java code. Defining views and layout as resources in this way makes them easier to work with, decoupled from the code, and in some cases reusable in different contexts.
View resource files are placed in the res/layout source directory. The root of these XML files is usually one of the ViewGroup layout subclasses we have already discussed:
RelativeLayout, LinearLayout, FrameLayout, and so on. Within these root elements are child XML elements that represent the view/layout tree.
An important thing to understand here is that resources in the res/layout direc- tory don’t have to be layouts. You can define a single TextView in a layout file the same way you might define an entire tree starting from an AbsoluteLayout. Yes, this makes the layout name and path potentially confusing, but that is how it is set up. (It might make more sense to have separate res/layout and res/view directories, but that might be confusing too, so just keep in mind that res/layout is useful for more than layout.)
You can have as many XML layout/view files as needed, all defined in the res/lay- out directory. Each View is then referenced in code based on the type and ID. Our lay- out file for the ReviewDetail screen, review_detail.xml, which is shown in listing 3.7, is referenced in the Activity code as R.layout.review_detail—which is a pointer to the RelativeLayout parent View object in the file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:padding="10px" android.setVerticalScrollBarEnabled="true" >
<ImageView android:id="@+id/review_image" android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="10px"
android:layout_marginBottom="5px" /> <TextView android:id="@+id/name_detail"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/review_image" android:layout_marginLeft="10px" android:layout_marginBottom="5px" style="@style/intro_blurb" /> <TextView android:id="@+id/rating_label_detail"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name_detail" android:layout_marginLeft="10px" android:layout_marginBottom="5px" style="@style/label" android:text="@string/rating_label" /> . . . remainder of file omitted for brevity
</RelativeLayout>
In this file we are using a RelativeLayout
B
. This is the ViewGroup at the root of theView tree. LayoutParams are then also defined in XML using the android: layout_[attribute] convention (where [attribute] refers to a layout attribute)
C
. Along with layout, other View-related attributes can also be defined in XML withListing 3.7 XML layout resource file for review_detail.xml
Define root View element
B
Define LayoutParams
C
D
Define View parameters in XMLE
Include child element with IDReference another resource
F
89