blob: eba5bf10604a6503cc9a38ad78420eef78f6ea9b [file] [log] [blame]
Robert Lyce4d2292010-12-16 17:26:11 -08001page.title=Debugging and Profiling User Interfaces
2@jd:body
3
4 <div id="qv-wrapper">
5 <div id="qv">
6 <h2>In this document</h2>
7
8 <ol>
9 <li>
10 <a href="#hierarchyViewer">Debugging and Optimizing User Interfaces with Hierarchy
11 Viewer</a>
12 <ul>
13 <li><a href="#layoutview">Layout View</a></li>
14
15 </li>
16
17 <li><a href="#layoutopt">Optimizing Layouts with <code>layoutopt</code></a></li>
18 </ol>
19 </div>
20 </div>
21
22 <p>Sometimes your application's layout can slow down your application.
23 To help debug issues in your layout, the Android SDK provides the Hierarchy Viewer and
24 <code>layoutopt</code> tools.
25 </p>
26
27 <p>The Hierarchy Viewer application allows you to debug and optimize your user interface. It
28 provides a visual representation of the layout's View hierarchy (the Layout View) and a magnified
29 inspector of the display (the Pixel Perfect View).</p>
30
31 <p><code>layoutopt</code> is a command-line tool that helps you optimize the layouts and layout
32 hierarchies of your applications. You can run it against your layout files or resource
33 directories to quickly check for inefficiencies or other types of problems that could be
34 affecting the performance of your application.</p>
35
36 <h2 id="hierarchyViewer">Debugging and Optimizing User Interfaces with Hierarchy Viewer</h2>
37
38 <p>To get the Hierarchy Viewer started:</p>
39
40 <ol>
41 <li>Connect your device or launch an emulator.</li>
42
43 <li>From a terminal, launch <code>hierarchyviewer</code> from the <code>&lt;sdk&gt;/tools/</code>
44 directory.</li>
45
46 <li>In the window that opens, you'll see a list of <strong>Devices</strong>. When a device is
47 selected, a list of currently active <strong>Windows</strong> is displayed on the right. The
48 <em>&lt;Focused Window&gt;</em> is the window currently in the foreground, and also the default
49 window loaded if you do not select another.</li>
50
51 <li>Select the window that you'd like to inspect and click <strong>Load View
52 Hierarchy</strong>. The Layout View will be loaded. You can then load the Pixel Perfect View by
53 clicking the second icon at the bottom-left of the window.</li>
54 </ol>
55
56 <p>If you've navigated to a different window on the device, press <strong>Refresh
57 Windows</strong> to refresh the list of available windows on the right.</p>
58
59
60 <h3 id="layoutview">Layout View</h3>
61
62 <p>The Layout View offers a look at the View layout and properties. It has three views:</p>
63
64 <ul>
65 <li>Tree View: a hierarchy diagram of the Views, on the left.</li>
66
67 <li>Properties View: a list of the selected View's properties, on the top-right.</li>
68
69 <li>Wire-frame View: a wire-frame drawing of the layout, on the bottom-right.</li>
70 </ul><br />
71 <img src="{@docRoot}images/hierarchyviewer-layout.png"
72 alt=""
73 height="509"
74 width="700" />
75 <p class="img-caption"><strong>Figure 1.</strong> Screenshot of Hierarchy Viewer</p>
76
77 <p>Select a node in the Tree View to display the properties of that element in the Properties
78 View. When a node is selected, the Wire-frame View also indicates the bounds of the element with
79 a red rectangle. Double click a node in the tree (or select it, and click <strong>Display
80 View</strong>) to open a new window with a rendering of that element.</p>
81
82 <p>The Layout View includes a couple other helpful features for debugging your layout:
83 <strong>Invalidate</strong> and <strong>Request Layout</strong>. These buttons execute the
84 respective View calls, {@link android.view.View#invalidate()} and {@link
85 android.view.View#requestLayout()}, on the View element currently selected in the tree. Calling
86 these methods on any View can be very useful when simultaneously running a debugger on your
87 application.</p>
88
89 <p>The Tree View can be resized by adjusting the zoom slider, below the diagram. The number of
90 View elements in the window is also given here. You should look for ways to minimize the number
91 of Views. The fewer View elements there are in a window, the faster it will perform.</p>
92
93 <p>If you interact with the device and change the focused View, the diagram will not
94 automatically refresh. You must reload the Layout View by clicking <strong>Load View
95 Hierarchy</strong>.</p>
96
97 <h3>Pixel Perfect View</h3>
98
99 <p>The Pixel Perfect View provides a magnified look at the current device window. It helps you
100 design your UI better by giving you a closer look at your UI's image quality, alignment, and other
101 aesthetic qualities. It has three views:</p>
102
103 <ul>
104 <li>Explorer View: shows the View hierarchy as a list, on the left.</li>
105
106 <li>Normal View: a normal view of the device window, in the middle.</li>
107
108 <li>Loupe View: a magnified, pixel-grid view of the device window, on the right.</li>
109 </ul><br />
110 <img src="{@docRoot}images/hierarchyviewer-pixelperfect.png"
111 alt=""
112 height="509"
113 width="700" />
114
115 <p>Click on an element in the Explorer View and a "layout box" will be drawn in the Normal View
116 to indicate the layout position of that element. The layout box uses multiple rectangles, to
117 indicate the normal bounds, the padding and the margin (as needed). The purple or green rectangle
118 indicates the normal bounds of the element (the height and width). The inner white or black
119 rectangle indicates the content bounds, when padding is present. A black or white rectangle
120 outside the normal purple/green rectangle indicates any present margins. (There are two colors
121 for each rectangle, in order to provide the best contrast based on the colors currently in the
122 background.)</p>
123
124 <p>A very handy feature for designing your UI is the ability to overlay an image in the Normal
125 and Loupe Views. For example, you might have a mock-up image of how you'd like to layout your
126 interface. By selecting <strong>Load...</strong> from the controls in the Normal View, you can
127 choose the image from your computer and it will be placed atop the preview. Your chosen image
128 will anchor at the bottom left corner of the screen. You can then adjust the opacity of the
129 overlay and begin fine-tuning your layout to match the mock-up.</p>
130
131 <p>The Normal View and Loupe View refresh at regular intervals (5 seconds by default), but the
132 Explorer View does not. If you navigate away and focus on a different View, then you should
133 refresh the Explorer's hierarchy by clicking <strong>Load View Hierarchy</strong>. This is even
134 true when you're working in a window that holds multiple Views that are not always visible. If
135 you do not, although the previews will refresh, clicking a View in the Explorer will not provide
136 the proper layout box in the Normal View, because the hierarchy believes you are still focused on
137 the prior View.</p>
138
139 <p>Optional controls include:</p>
140
141 <ul>
142 <li><strong>Overlay</strong>: Load an overlay image onto the view and adjust its opacity.</li>
143
144 <li><strong>Refresh Rate</strong>: Adjust how often the Normal and Loupe View refresh their
145 display.</li>
146
147 <li><strong>Zoom</strong>: Adjust the zoom level of the Loupe View.</li>
148 </ul>
149
150 <h2 id="layoutopt">Optimizing layouts with layoutopt</h2>
151 <p>The <code>layoutopt</code> tool lets you analyze the XML files that represent your application's layout
152 and finds ineffiencies in the view hierarchy.</p>
153
154 <p>To run the tool, open a terminal and launch <code>layoutopt &lt;resources&gt;</code> from your
155 SDK <code>tools/</code> directory. In the command, supply a list of uncompiled resource xml files
156 or directories that you want to analyze.</p>
157
158 <p>When run, the tool loads the specified XML files and analyzes their layout structures and
159 hierarchies according to a set of predefined rules. If it detects issues, it outputs information
160 about the issues, giving filename, line numbers, description of issue, and for some types of
161 issues a suggested resolution.</p>
162
163 <p>Here's an example of the output:</p>
164 <pre>
165$ layoutopt samples/
166samples/compound.xml
167 7:23 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
168 11:21 This LinearLayout layout or its FrameLayout parent is useless
169samples/simple.xml
170 7:7 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
171samples/too_deep.xml
172 -1:-1 This layout has too many nested layouts: 13 levels, it should have &lt;= 10!
173 20:81 This LinearLayout layout or its LinearLayout parent is useless
174 24:79 This LinearLayout layout or its LinearLayout parent is useless
175 28:77 This LinearLayout layout or its LinearLayout parent is useless
176 32:75 This LinearLayout layout or its LinearLayout parent is useless
177 36:73 This LinearLayout layout or its LinearLayout parent is useless
178 40:71 This LinearLayout layout or its LinearLayout parent is useless
179 44:69 This LinearLayout layout or its LinearLayout parent is useless
180 48:67 This LinearLayout layout or its LinearLayout parent is useless
181 52:65 This LinearLayout layout or its LinearLayout parent is useless
182 56:63 This LinearLayout layout or its LinearLayout parent is useless
183samples/too_many.xml
184 7:413 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
185 -1:-1 This layout has too many views: 81 views, it should have &lt;= 80!
186samples/useless.xml
187 7:19 The root-level &lt;FrameLayout/&gt; can be replaced with &lt;merge/&gt;
188 11:17 This LinearLayout layout or its FrameLayout parent is useless
189</pre>
190
191<p>
192For more information on running the tool, see the
193<a href="${@docRoot}guide/developing/tools/layoutopt.html">layoutopt</a> reference.</p>
194
195
196
197
198
199
200
201