Home | History | Annotate | Download | only in views
      1 page.title=Declaring a UI in XML
      2 parent.title=Views and Layout
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <p>You can create your application's user interface in two ways: 
      7 <ul>
      8 <li>You can declare UI elements statically, in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. </li>
      9 <li>You can instantiate screen elements dynamically, at runtime, through code in your application. Your application can refer to or create View or other class objects and manipulate their properties programmatically. </li>
     10 </ul>
     11 
     12 <p>One advantage of declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls it's behavior. Your UI description is external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile. For example, you can create XML layouts for different screen orientations and for a variety of device screen sizes or languages. Additionally, declaring in XML makes it easier to see the elements and structure of your UI, so it's easier to debug problems. </p>
     13 
     14 <p>The Android framework gives you the flexibility to use either or both of these ways of declaring and managing your application's UI. For example, you could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties. You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time. </p>
     15 
     16 <p>You build your application's UI in approximately the same way, whether you are declaring it in XML or programmatically. In both cases, your UI will be a tree structure that may include multiple View or Viewgroup subclasses. <p>
     17 
     18 <p>In general, the XML vocabulary for declaring UI elements closely follows the structure and naming of the framework's UI-related classes and methods, where element names correspond to class names and attribute names correspond to methods. In fact, the correspondence is often so direct that you can guess what XML attribute corresponds to a class method, or guess what class corresponds to a given xml element. </p>
     19 
     20 <p>However, note that the XML vocabulary for defining UI is not entirely identical to the framework's classes and methods. In some cases, there are slight naming differences. For
     21 example, the EditText element has a <code>text</code> attribute that corresponds to
     22 EditText.setText. </p>
     23 
     24 <div class="sidebox-wrapper">
     25 <div class="sidebox">
     26 <p>For your convenience, the API reference documentation for UI related classes
     27 lists the available XML attributes that correspond to the class methods, including inherited
     28 attributes.</p>
     29 
     30 <p>To learn more about the available XML elements and attributes, as well as the format of the XML file, see <a
     31 href="{@docRoot}reference/available-resources.html#layoutresources">Layout Resources</a>.</p>
     32 </div>
     33 </div>
     34 
     35 <p>Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create HTML files &mdash; as a series of nested tags. </p>
     36 
     37 <p>Each layout file must contain exactly one root element, and the root element must be a View or ViewGroup object. Once you've defined the root element, you can add additional layout objects or controls as child elements of the root element, if needed. In the example below, the tree of XML elements evaluates to the outermost LinearLayout object.
     38 
     39 <p>After you've declared your layout in XML, you must save the file, with the <code>.xml</code> extension, in the proper location, so that it will be compiled correctly. The proper location for storing layout files is in your application's <code>res/layout/</code> directory. </p>
     40 
     41 <p>When you compile your application, each XML layout file is compiled into an
     42 android.view.View resource. You can then load the layout resource from your application code, by calling <code>setContentView(R.layout.<em>layout_file_name</em>)</code> in your {@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()} 
     43 implementation.</p>
     44 
     45 <p>When you load a layout resource, the Android system initializes run-time objects corresponding to the elements in your layout. It parses the elements of your layout in-order (depth-first), instantiating the Views and adding them to their parent(s). </p>
     46 
     47 <p>Attributes named <code>layout_<em>something</em></code> apply to that
     48 object's LayoutParams member. <a href="{@docRoot}reference/available-resources.html#layoutresources">Layout
     49 Resources</a> also describes how to learn the syntax for specifying
     50 LayoutParams properties. </p>
     51 
     52 <p>Also note that Android draws elements in the order in which they
     53 appear in the XML. Therefore, if elements overlap, the last one in the XML
     54 file will probably be drawn on top of any previously listed elements in that
     55 same space.</p>
     56 
     57 <p>The following values are supported for dimensions (described in {@link
     58 android.util.TypedValue TypedValue}):</p>
     59 
     60 <ul>
     61     <li>px (pixels) </li>
     62     <li>dip (device independent pixels) </li>
     63     <li>sp (scaled pixels &mdash; best for text size) </li>
     64     <li>pt (points) </li>
     65     <li>in (inches) </li>
     66     <li>mm (millimeters) </li>
     67 </ul>
     68 
     69 <p>Example: <code>android:layout_width=&quot;25px&quot;</code> </p>
     70 
     71 <p>For more information about these dimensions, see <a href="{@docRoot}reference/available-resources.html#dimension">Dimension Values</a>.</p>
     72 
     73 <p>The example below shows an XML file and the resulting screen in the UI. Note that the text on the
     74 top of the screen was set by calling {@link
     75 android.app.Activity#setTitle(java.lang.CharSequence) Activity.setTitle}. Note
     76 that the attributes that refer to relative elements (i.e., layout_toLeft)
     77 refer to the ID using the syntax of a relative resource
     78 (@id/<em>id_number</em>). </p>
     79 
     80 <table border="1">
     81     <tr>
     82         <td>
     83         <pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
     84 &lt;!-- Demonstrates using a relative layout to create a form --&gt;
     85 &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android
     86                 android:layout_width=&quot;fill_parent&quot; 
     87                 android:layout_height=&quot;wrap_content&quot;
     88                 android:background=&quot;@drawable/blue&quot;
     89                 android:padding=&quot;10px&quot;&gt;
     90 
     91     &lt;TextView id=&quot;@+id/label&quot; 
     92               android:layout_width=&quot;fill_parent&quot; 
     93               android:layout_height=&quot;wrap_content&quot; 
     94               android:text=&quot;Type here:&quot;/&gt;
     95 
     96     &lt;EditText id=&quot;@+id/entry&quot; 
     97               android:layout_width=&quot;fill_parent&quot; 
     98               android:layout_height=&quot;wrap_content&quot; 
     99               android:background=&quot;@android:drawable/editbox_background&quot;
    100               android:layout_below=&quot;@id/label&quot;/&gt;
    101   
    102     &lt;Button id=&quot;@+id/ok&quot; 
    103             android:layout_width=&quot;wrap_content&quot; 
    104             android:layout_height=&quot;wrap_content&quot; 
    105             android:layout_below=&quot;@id/entry&quot;
    106             android:layout_alignParentRight=&quot;true&quot;
    107             android:layout_marginLeft=&quot;10px&quot;
    108             android:text=&quot;OK&quot; /&gt;
    109 
    110     &lt;Button android:layout_width=&quot;wrap_content&quot; 
    111             android:layout_height=&quot;wrap_content&quot;
    112             android:layout_toLeftOf=&quot;@id/ok&quot;
    113             android:layout_alignTop=&quot;@id/ok&quot;
    114             android:text=&quot;Cancel&quot; /&gt;
    115 &lt;/RelativeLayout&gt;</pre></td>
    116         <td><img src="{@docRoot}images/designing_ui_layout_example.png" alt="Screen shot showing how this layout XML file is rendered."  /></td>
    117     </tr>
    118 </table>
    119 
    120 <h3>Loading the XML Resource </h3>
    121 
    122 <p>Loading the compiled layout resource is very easy, and done with a single
    123 call in the activity's onCreate() method, as shown here:</p>
    124 
    125 <pre>
    126 protected void onCreate(Bundle savedValues)
    127 {
    128    // Be sure to call the super class.
    129    super.onCreate(savedValues);
    130 
    131    // Load the compiled layout resource into the window's
    132    // default ViewGroup.
    133    // The source file is res/layout/hello_activity.xml
    134     setContentView(R.layout.hello_activity);
    135   
    136    // Retrieve any important stored values.
    137    restoreValues(savedValues);
    138 } </pre>
    139