A (175)

(Tuis.) #1

268 CHAPTER 8: Android UI Design: Using Advanced ViewGroup Layout Containers


We will be using the true setting, so that margins are automatically set for us by the GridLayout;
although, if we decide to use margin parameters, these will be “respected” by the GridLayout
algorithm as well, as you will see.


Converting a RelativeLayout UI to a GridLayout


To get the best overview or perspective on how the two most often used layout containers,
RelativeLayout and GridLayout, differ, let’s convert the RelativeLayout that you created for your
EditGalaxy.java Activity subclass to use a GridLayout parent layout container tag instead. In this
way, we can build on the same Java code that we used in the previous chapter, allowing us to focus
primarily on XML UI design in this chapter.


Open your activity_edit.xml file in the central editing pane of Eclipse, if it is not there as a tab
already, and edit the opening and closing tag, changing the work Relative to Grid,
so that it now reads .


After you do this, you need to have Eclipse “evaluate” the markup, and in the XML editor, this
evaluation is invoked by using the CTRL-S (File Save) keystroke (hold down the Ctrl key on the
keyboard while typing in an “s”).


As you can see in Figure 8-1, Eclipse will place its wavy yellow warning highlighting under the
RelativeLayout child widget tags that use relative layout parameters, which you will have to replace
to utilize GridLayout parameters instead. There is also one wavy red error highlight under the


parent tag, and if you mouse-over this you will see that, as I mentioned previously in
the chapter, GridLayout is an Android 4.0 class, and requires the use of API Level 14 or later. For this
reason, you will have to edit your AndroidManifest.xml file in order to eliminate this wavy red error
highlighting from your GridLayout parent tag.

Figure 8-1. Change the container parent tag to a tag, and review error and warnings


It is important to note that Eclipse’s Java editor does not require this CTRL-S work process to
get Eclipse ADT to evaluate your Java code, as Eclipse will do this in “real-time,” as you type. In
contrast, the XML markup editor does require that you Save (CTRL-S) your markup before it will

Free download pdf