Location as an Edit Text View

Why are we using an EditText View for the location field in the ReviewCriteria Activity when Android includes technology that could be used to derive this value from the current physical location of the device (or allow the user to select it using a Map, rather than type it in)? Good eye, but we are doing this intentionally here. We want this early example to be complete and nontrivial but not too complicated. You will learn more about using the location support Android provides and MapViews in later chapters.

After an Activity, complete with necessary views, is started, the lifecycle takes over and the onCreate() method is invoked G. This is one of a series of important lifecycle methods the Activity class provides. Every Activity will override onCreate(), where component initialization steps are invoked, though not every Activity will need to override other lifecycle methods. The Activity lifecycle is worthy of an in-depth discussion of its own, and for that reason we will explore these methods further, in section 3.1.2.

Once inside the onCreate() method, the setContentView() method is where you will normally associate an XML layout file Q. We say normally, because you do not have to use an XML file at all; you can instead define all of your layout and View configuration in code, as Java objects. Nevertheless, it is often easier, and better practice by decoupling, to use an XML layout resource for each Activity. An XML layout file defines View objects, which are laid out in a tree, and can then be set into the Activity for use.

Layout and view details, defined in XML or in code, are also topics we will address in later sections of this chapter. Here we simply need to stress that views are typically defined in XML and then are set into the Activity and "inflated." Views that need some runtime manipulation, such as binding to data, can then be referenced in code and cast to their respective subtypes Q. Views that are static, those you don't need to interact with or update at runtime, like labels, do not need to be referenced in code (they show up on the screen, because they are part of the View tree as defined in the XML, but need no explicit setup in code). Going back to the screen shots in figure 3.1, you will notice that the ReviewCriteria screen has two labels as well as the three inputs we have already discussed. These labels are not present in the code; they are defined in the review_criteria.xml file that you will see when we discuss XML-defined resources.

The next area of our ReviewCriteria Activity is where we bind data to our select list views, the Spinner objects. Android employs a handy "adapter" concept to link views that contain collections with data. Basically an Adapter is a collection handler that returns each item in the collection as a View. Android provides many basic adapters: ListAdapter, ArrayAdapter, GalleryAdapter, CursorAdapter, and more. You can also easily create your own Adapter, a technique we will use when we discuss creating custom views in section 3.2. Here we are using an ArrayAdapter that is populated with our Context (this), a View element defined in an XML resource file, and an array representing the data (also defined as a resource in XML—which you will learn more about in section 3.3) ©. When we create the ArrayAdapter we define the View to be used for the element shown in the Spinner before it is selected; after it is selected it uses the View defined in the drop-down ©. Once our Adapter and its View elements are defined, we set it into the Spinner object ©.

The last thing this initial Activity demonstrates is our first explicit use of event handling. UI elements in general support many types of events, which you will learn more about in section 3.2.7. In this case we are using an OnClickListener with our Button, in order to respond when the button is clicked ©.

After the onCreate() method is complete, with the binding of data to our Spinner views, we have menu buttons (which are different than on-screen Button views, as you shall see) and associated actions. We show how these are implemented in the last part of ReviewCriteria in listing 3.2.

0 0

Post a comment