172 CHAPTER 6: Android Screen Design: Writing to the Display Using Activity and View
It is important to note that even though a ViewGroup subclass must be used as the “parent” object
in your XML UI layout design, ViewGroup subclass objects can also be “nested” underneath this
parent ViewGroup object. We’ll actually be nesting ViewGroup subclasses using a LinearLayout
ViewGroup subclass later in the chapter. ViewGroup subclass objects can be both parent and child
objects, whereas View widgets are usually just child objects.
Hands-on experience is the best way to show you all of this UI design theory, so let’s integrate a UI
design with a Galaxy object created using the Galaxy( ) constructor you built in the previous chapter.
Let’s use the Galaxy class you created in Chapter 5 to create a Galaxy object and utilize the .set( )
methods you created to establish its properties. Then we’ll develop a UI design using the popular
LinearLayout ViewGroup subclass that displays galaxy information on your Activity screen. Finally,
we will “wire together” your UI design and the Galaxy object, so that the UI design displays the
variable data in the Galaxy object’s data fields.
Customizing Your UI Design: Galaxy Screen
Next, let’s get right into learning how the Activity subclass, ViewGroup layout container, and View
widgets are used, within the context of your current Hello Universe Android application. To do this,
we’ll continue building upon your Java programming work that you did in Chapter 5, and continue
using your Galaxy class, to create a new Galaxy object in your MainActivity.java Activity subclass’s
.onCreate( ) method. Then, we’ll create the UI design for this Galaxy Info Screen, which will display
your Galaxy object’s characteristics on the Hello Universe app screen.
During the next several sections of this chapter, we will make the following Hello Universe
application additions and UI design modifications:
Review the Eclipse Project ➤ Clean Project menu sequence, and learn the
work process to rebuild a project. This is in case you ever open up Eclipse
Android Development Tools (ADT) and find that your project is filled with errors
that should not be there (that is, were not there the last time that you closed
and exited Eclipse). This just happened to me, as I was writing the code for this
chapter, so I am going to include this topic here so you know how to deal with
it in case this ever happens to you. Opening up what you know is a “clean”
project and seeing it filled with “red ink,” or error highlighting everywhere, can be
extremely unnerving to any Android developer.
Review and enhance the .onCreate( ) method in your MainActivity.java class,
to create a new Galaxy object, so that you have some Galaxy data values to
display in the revised UI design that we’ll be creating by using one of the most
commonly used ViewGroup subclasses (LinearLayout).
Edit and reconfigure the activity_main.xml UI layout definition, to use a more
basic and memory efficient LinearLayout ViewGroup subclass to create a
Galaxy Information Screen. We will also be using the most commonly used
View subclass UI widget, the TextView, and we will take a look at how to “nest”
LinearLayout ViewGroup objects, in order to create more complex UI structures.