Creating Screen Layouts in Android Using XML
Join the DZone community and get the full member experience.Join For Free
in my last post , i alluded to the fact that you can lay out your visual elements and controls on your android application screens (your activity objects) using xml. in this post, i'm going to give you a peek at what that xml looks like.
here's an example of a super-basic layout xml file:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:id="@+id/exampletext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="push the button" android:background="#ffff00" android:textcolor="#000000" android:textstyle="bold" android:paddingleft="5dip" android:paddingright= "5dip" android:layout_weight="1"/> <button android:id="@+id/examplebutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="the button" android:layout_weight="1" /> </linearlayout>
the first line is a typical xml declaration statement. the next line is the start of a linearlayout view object, which serves as a visual container for other view objects. the first attribute of the linearlayout tag defines the xml namespace (that has to be the first attribute of the first view object of every android layout file you create). this linearlayout container is set to fill the entire width and height of its parent container. in this case, since it is the main container object, it fills the whole screen. the "android:orientation" attribute is set to "vertical" which means that all of the child objects within the linearlayout will be arranged vertically, even if one or more objects could fit together horizontally. seeing the linearlayout for the first time reminded me of the vbox and hbox layout elements in adobe flex, which also uses a domain-specific xml language to define screen layouts.
the second object in the layout (the first within the linearlayout) is a textview object, which as you might guess displays text. the "android:id" attribute assigns an id value of "exampletext" to the textview object. the "android:layout_height" and "android:layout_width" attributes are set to "wrap_content." normally, that means that the object will only be large enough to contain whatever's inside of it plus any padding, but it won't be in this case (which i'll explain shortly). the left and right padding are set to 5dip (density-independent pixels), a pixel scale used by android to accomodate the different device screen sizes. the other attributes are fairly self-explanatory except for "android:layout_weight", which is something more easily explained by the examples coming up shortly.
the third and final object is the button object, and you can guess what it does. note that its "android:layout_width" attribute is set to "fill_parent", so it will be as wide as the screen is, even if the android device is re-oriented from portrait to landscape.
now, here's how the layout looks in the android emulator that comes with the android eclipse plugin:
as i mentioned before, the height and width of the textview object was set to "wrap_content", meaning that the textview should be just as wide and as tall as it needs to be to enclose whatever is inside it plus the padding. the width is just long enough to contain the content horizontally, but why is it so tall? it's because of the "android:layout_weight" attribute. in android, you cannot do percentage-based heights and widths like you can with html elements. instead, you can assign layout weight values to view objects to assign a level of "importance." more important objects take up more of the available layout space, while less important objects take up the remainder.
the default layout weight of all view objects is zero. an "android:layout_weight" value of 1 denotes a more important object, while a value of 2 denotes an even more important object. right now, both the textview and the button have a layout weight of 1, so they take up an equal amount of vertical space (because they are in a vertically-organized linearlayout). if the button's layout weight was changed to 0, however:
...then the button's height gets reduced in deference to the importance of the textview.
personally, i'm still getting the hang of using layout weights. fortunately, i don't have to run the layout through the emulator every time i want to see the results of my latest change to the xml. the android plugin for eclipse lets you view layouts either as raw xml or within a graphical layout view that lets you add view objects to your layout via drag-and-drop and see the results of the property changes you make as you make them:
it's not as nice as the layout designer in flex/flash builder, but it's still nice to have.
in my next android post, i'll show how to attach this layout to an activity object, and make a few changes to the layout object programmatically.
Opinions expressed by DZone contributors are their own.