196 CHAPTER 6: Android Screen Design: Writing to the Display Using Activity and View
The solution to this is to change the “match_parent” constant in both of the nested
containers to use the “wrap_content” constant instead. The wrap_content constant essentially
does the exact opposite of what the match_parent constant does; instead of expanding to fill, or fit,
the screen above it, it contracts instead and “shrink-wraps” the content below (contained inside of)
it. As you can see in Figure 6-26, both of the nested
constants in their parameter settings, and when you use the Run As ➤ Android Application, you
get the result shown in the right-hand panel of the screenshot in Figure 6-20.
Next, let’s add a couple of final android:layout_margin-related “tweaks,” to fine-tune this UI design.
The first thing we need to do is to space the data field TextView UI elements down next to their
corresponding Galaxy attribute label TextView UI elements, using an android:layout_marginTop="33dip"
in the second nested
android:layout_marginLeft="48dip" to the first hello_world
Interfacing with UI Elements: The ID Parameter
There is one last bit of XML markup editing that we will need to do to the seven TextView UI elements
in the second nested LinearLayout container, so they can be “wired into,” or accessed by, the Java
TextView objects, which we are about to create in your MainActivity.java class. In order to be able to
transfer the Galaxy object variable data values into the data field TextView objects we have created
in our UI design to hold and display these values, we need to create these TextView UI elements as
TextView objects in your Java program logic.
The XML parameter you need to be able to reference a UI element (widget) or UI layout in your Java
code is the android:id parameter, which gives an ID to the XML UI element definition, so that you
can instantiate it and subsequently reference and utilize it in your Java code. The XML markup for
this parameter is as follows:
android:id="@+id/name"
Create ID parameters for all seven of your TextViews as @+id/solar, @+id/habit, @+id/colony,
@+id/pop, @+id/fleet and @+id/ships, which are going to display solar, habitable, colonies,
population, fleet, and ships data respectively, as shown in Figure 6-27.