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