Android UI Design Via XML, Page 2
Declaring Android UI Layout with XML
As already mentioned, the graphical layout tool writes a corresponding XML layout resource file while you're visually creating your UI. You can dip into the XML and edit it directly at any point during the development process.
To access the XML file:
- Ensure the 'Java' perspective is open.
- In the Package Explorer tab, open 'res' then 'layout.'
- Open the 'main.xml' folder and open the 'main.xml' file.
Click here for larger image
In the main.xml file, you can create your UI components by entering XML code. Here is an example of a very simple XML layout resource:
Your XML resource file should begin with the following :
<?xml version="1.0" encoding="utf-8"?>
It will then be followed by the root element of the layout, usually a container view such as a layout (in this example, it's LinearLayout.)
This example contains some additional elements:
- TextView -- a text editor. The actual text is specified using the following:
android:text = "your text"
- CheckBox -- two checkboxes
Again, the text displayed alongside the checkboxes is specified with:
android:text = "button text"
Tip:The integer ID associated with view objects is essential for identifying the particular view within the tree. In the layout XML, this ID is usually assigned as a string, in the id attribute. The basic ID syntax is:
For example, the UI element name could be "textbox" (or "textbox1" if your UI contains more than one textbox.)
But how does this XML translate to the mobile device? Launch the Android emulator and take a look!
The ADT plugin's graphical WYSIWYG editor has been greatly improved in recent releases of ADT, so there's never been a better time to try it out! In this Android UI tutorial, we provided an intro to the graphical layout editor, before exploring its powerful functionality in greater depth. We also briefly covered getting started with defining your layout in XML -- but there's still much more to cover. Interested in learning more? The official Android docs are a great source of additional information on using XML in Android.
Page 2 of 2