Android lets you specify screen layouts using XML elements inside an XML file, similar to designing screen layout for a webpage in an HTML file. Each file contains a whole screen or a part of a screen, and is compiled into a View resource that can be passed in to Activity.setContentView or used as a reference by other layout resource elements. Files are saved in the res/layout/ folder of your project, and compiled by the Android resource compiler, aapt.
Every layout XML file must evaluate to a single root element. First we'll describe how to use the standard XML tags understood by Android as it is shipped, and then we'll give a little information on how you can define your own custom XML elements for custom View objects.
For a complete discussion on creating layouts, see the User Interface topic.
Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root element of one of the supported XML layout elements.
Resource file location: res/layout/some_file.xml.
Compiled resource datatype: Resource pointer to a View (or subclass) resource. Resource reference name:
<ViewGroupClass xmlns:android="http://schemas.android.com/apk/res/android" id="@+id/string_name" (attributes)> <widget or other nested ViewGroupClass>+
<requestFocus/>(0 or 1 per layout file, assigned to any element) </ ViewGroupClass>
The file must have a single root element. This can be a ViewGroup class that contains other elements, or a widget (or custom item) if it's only one object. By default, you can use any (case-sensitive) Android widget or ViewGroup class name as an element. These elements support attributes that apply to the underlying class, but the naming is not as clear. How to discover what attributes are supported for what tags is discussed below. You should not assume that any nesting is valid (for example you cannot enclose <TextView> elements inside a <ListLayout>).
If a class derives from another class, the XML element inherits all the attributes from the element that it "derives" from. So, for example, <EditText> is the corresponding XML element for the EditText class. It exposes its own unique attributes (EditText_numeric), as well as all attributes supported by <TextView> and <View>. For the id attribute of a tag in XML, you should use a special syntax: "@+id/somestringvalue". The "@+" syntax creates a resource number in the R.id class, if one doesn't exist, or uses it, if it does exist. When declaring an ID value for an XML tag, use this syntax. Example: <TextView id="@+id/nameTextbox"/>, and refer to it this way in Java: findViewById(R.id.nameTextbox). All elements support the following values:
• id - An ID value used to access this element in Java. Typically you will use the syntax @+id/string_name to generate an ID for you in the id.xml file if you haven't created one yourself.
• xmlns:android="http://schemas.android.com/apk/res/android" - Required for the root element only.
Any element representing a View object can include this empty element, which gives it's parent tag initial focus on the screen. You can have only one of these elements per file.
Was this article helpful?