1 page.title=Debugging and Profiling User Interfaces
8 <h2>In this document</h2>
12 <a href="#HierarchyViewer">
13 Debugging and Optimizing User Interfaces with Hierarchy Viewer
16 <li><a href="#runhv">Running Hierarchy Viewer and choosing a window</a></li>
17 <li><a href="#viewhierarchy">About the View Hierarchy window</a></li>
18 <li><a href="#indiView">Working with an individual View in Tree View</a></li>
19 <li><a href="#hvdebugging">Debugging with View Hierarchy</a></li>
20 <li><a href="#hvoptimize">Optimizing with View Hierarchy</a></li>
24 <a href="#pixelperfect">
25 Examining and Designing User Interfaces with Pixel Perfect
28 <li><a href="#aboutpixelperfect">About the Pixel Perfect window</a></li>
29 <li><a href="#overlays">Working with Pixel Perfect overlays</a></li>
32 <li><a href="#layoutopt">Optimizing Layouts with <code>layoutopt</code></a></li>
37 <p>Sometimes your application's layout can slow down your application.
38 To help debug issues in your layout, the Android SDK provides the Hierarchy Viewer and
39 <code>layoutopt</code> tools.
42 <p>The Hierarchy Viewer application allows you to debug and optimize your user interface. It
43 provides a visual representation of the layout's View hierarchy (the View Hierarchy window)
44 and a magnified view of the display (the Pixel Perfect window).</p>
46 <p><code>layoutopt</code> is a command-line tool that helps you optimize the layouts and layout
47 hierarchies of your applications. You can run it against your layout files or resource
48 directories to quickly check for inefficiencies or other types of problems that could be
49 affecting the performance of your application.</p>
51 <h2 id="HierarchyViewer">Debugging and Optimizing User Interfaces with Hierarchy Viewer</h2>
53 <h3 id="runhv">Running Hierarchy Viewer and choosing a window</h3>
55 To run Hierarchy Viewer, follow these steps:</p>
58 Connect your device or launch an emulator.
60 To preserve security, Hierarchy Viewer can only connect to devices running a
61 developer version of the Android system.
65 If you have not done so already, install the application you want to work with.
68 Run the application, and ensure that its UI is visible.
71 From a terminal, launch <code>hierarchyviewer</code> from the
72 <code><sdk>/tools/</code>
76 The first window you see displays a list of devices and emulators. To expand the list
77 of Activity objects for a device or emulator, click the arrow on the left. This displays a
78 list of the Activity objects whose UI is currently visible on the device or emulator. The
79 objects are listed by their Android component name. The list includes both your application
80 Activity and system Activity objects. A screenshot of this window appears in
84 Select the name of your Activity from the list. You can now look at its view
85 hierarchy using the View Hierarchy window, or look at a magnified image of the UI using
86 the Pixel Perfect window.
90 To learn how to use the View Hierarchy window, go to
91 <a href="#viewhierarchy">About the View Hierarchy window</a>. To learn how to use the
92 Pixel Perfect window, go to <a href="#pixelperfect">About the Pixel Perfect window</a>.
94 <img id="Fig1" src="{@docRoot}images/developing/hv_device_window.png" alt="" height="600"/>
95 <p class="img-caption"><strong>Figure 1.</strong> Hierarchy Viewer device window</p>
96 <h3 id="viewhierarchy">About the View Hierarchy window</h3>
98 The View Hierarchy window displays the View objects that form the UI of the
99 Activity that is running on your device or emulator. You use it to look at individual
100 View objects within the context of the entire View tree. For each View object, the View
101 Hierarchy window also displays rendering performance data.
104 To see the View Hierarchy window, run Hierarchy Viewer as described in
105 the section <a href="#runhv">Running Hierarchy Viewer and choosing a window</a>. Next, click
106 <strong>View Hierarchy</strong> at the top of the device window.
109 You should see four panes:
113 <strong>Tree View</strong>: The left-hand pane displays the Tree View,
114 a diagram of the Activity object's hierarchy of views. Use Tree View to examine individual
115 View objects and see the relationships between View objects in your UI.
117 To zoom in on the pane, use the slider at the bottom of the pane, or use your mouse
118 scroll wheel. To move around in the pane or reveal View objects that are not currently
119 visible, click and drag the pane.
122 To highlight the nodes in the tree whose class or ID match a search string, enter the
123 string in the <strong>Filter by class or id:</strong> edit box at the bottom of the
124 window. The background of nodes that match the search string will change from gray to
128 To save a screenshot of Tree View to a PNG file, click <strong>Save As PNG</strong> at
129 the top of the View Hierarchy window. This displays a dialog in which you can choose
130 a directory and file name.
133 To save a layered screenshot of your device or emulator to an Adobe Photoshop (PSD)
134 file, click <strong>Capture Layers</strong> at the top of the View Hierarchy window.
135 This displays a dialog in which you can choose a directory or file name.
136 Each View in the UI is saved as a separate Photoshop layer.
139 In Photoshop (or similar program that accepts .psd files), you can hide, show or edit a
140 layer independently of others. When you save a layered screenshot, you can examine and
141 modify the image of an individual View object. This helps you experiment with design
146 The upper right-hand pane displays the <strong>Tree Overview</strong>, a smaller map
147 representation of the entire Tree View window. Use Tree Overview to identify the part of the
148 view tree that is being displayed in Tree View.
150 You can also use Tree Overview to move around in the Tree View pane. Click and drag
151 the shaded rectangle over an area to reveal it in Tree View.
155 The middle right-hand pane displays the <strong>Properties View</strong>,
156 a list of the properties for a selected View object. With Properties View, you can
157 examine all the properties without having to look at your application source.
159 The properties are organized by category. To find an individual property, expand
160 a category name by clicking the arrow on its left. This reveals all the properties
165 The lower right-hand pane displays the <strong>Layout View</strong>,
166 a block representation of the UI. Layout View is another way to navigate through your UI.
167 When you click on a View object in Tree View, its position in the UI is highlighted.
168 Conversely, when you click in an area of Layout View, the View object for that area is
169 highlighted in Tree View.
171 The outline colors of blocks in Layout View provide additional information:
175 Bold red: The block represents the the View that is currently selected in
179 Light red: The block represents the parent of the block outlined in bold red.
182 White: The block represents a visible View that is not a parent or child of the
183 View that is currently selected in Tree View.
189 When the UI of the current Activity changes, the View Hierarchy window is not automatically
190 updated. To update it, click <strong>Load View Hierarchy</strong> at the top of the window.
193 Also, the window is not updated if you switch to a new Activity. To update it, start by
194 clicking the window selection icon in the bottom left-hand corner of the window. This
195 navigates back to the Window Selection window. From this window, click the Android
196 component name of the new Activity and then click <strong>Load View Hierarchy</strong>
197 at the top of the window.
200 A screenshot of the View Hierarchy window appears in figure 2.
202 <img id="Fig2" src="{@docRoot}images/developing/hv_view_hierarchy_window.png" alt="" height="600"/>
203 <p class="img-caption"><strong>Figure 2.</strong> The View Hierarchy window</p>
204 <h3 id="indiView">Working with an individual View in Tree View</h3>
206 Each node in Tree View represents a single View. Some information is always visible. Starting
207 at the top of the node, you see the following:
211 View class: The View object's class.
214 View object address: A pointer to View object.
217 View object ID: The value of the
218 <code><a href="{@docRoot}guide/topics/resources/layout-resource.html#idvalue">android:id</a>
222 Performance indicators: A set of three colored dots that indicate the rendering
223 speed of this View relative to other View objects in the tree. The three dots
224 represent (from left to right) the measure, layout, and draw times of the rendering.
226 The colors indicate the following relative performance:
230 Green: For this part of the render time, this View is in the faster 50% of all
231 the View objects in the tree. For example, a green dot for the measure time means
232 that this View has a faster measure time than 50% of the View objects in the tree.
235 Yellow: For this part of the render time, this View is in the slower 50% of all
236 the View objects in the tree. For example, a yellow dot for the layout time means
237 that this View has a slower layout time than 50% of the View objects in the tree.
240 Red: For this part of the render time, this View is the slowest one in the tree.
241 For example, a red dot for the draw time means that this View takes the most
242 time to draw of all the View objects in the tree.
247 View index: The zero-based index of the View in its parent View. If it is the only child,
252 When you select a node, additional information for the View appears in a small window above
253 the node. When you click one of the nodes, you see the following:
257 Image: The actual image of the View, as it would appear in the emulator. If the View has
258 children, these are also displayed.
261 View count: The number of View objects represented by this node. This includes the View
262 itself and a count of its children. For example, this value is 4 for a View that has 3
266 Render times: The actual measure, layout, and draw times for the View rendering, in
267 milliseconds. These represent the same values as the performance indicators mentioned in
268 the preceding section.
272 An annotated screenshot of an individual node in the Tree View window appears in figure 3.
274 <img id="Fig3" src="{@docRoot}images/developing/hv_treeview_screenshot.png" alt="" height="600"/>
275 <p class="img-caption"><strong>Figure 3.</strong> An annotated node in Tree View</p>
276 <h3 id="hvdebugging">Debugging with View Hierarchy</h3>
278 The View Hierarchy window helps you debug an application by providing a static display
279 of the UI. The display starts with your application's opening screen. As you step through
280 your application, the display remains unchanged until you redraw it by invalidating and
281 then requesting layout for a View.
284 To redraw a View in the display:
288 Select a View in Tree View. As you move up towards the root of the tree (to the
289 left in the Tree View), you see the highest-level View objects. Redrawing a high-level
290 object usually forces the lower-level objects to redraw as well.
293 Click <strong>Invalidate</strong> at the top of the window. This marks the View as
294 invalid, and schedules it for a redraw at the next point that a layout is requested.
297 Click <strong>Request Layout</strong> to request a layout. The View and its children
298 are redrawn, as well as any other View objects that need to be redrawn.
302 Manually redrawing a View allows you to watch the View object tree and examine the properties of
303 individual View objects one step at a time as you go through breakpoints in your code.
305 <h3 id="hvoptimize">Optimizing with View Hierarchy</h3>
307 View Hierarchy also helps you identify slow render performance. You start by looking at the
308 View nodes with red or yellow performance indicators to identify the slower View objects. As you
309 step through your application, you can judge if a View is consistently slow or slow only in
310 certain circumstances.
313 Remember that slow performance is not necessarily evidence of a problem, especially for
314 ViewGroup objects. View objects that have more children and more complex View objects render
318 The View Hierarchy window also helps you find performance issues. Just by looking at the
319 performance indicators (the dots) for each View node, you can see which View objects are the
320 slowest to measure, layout, and draw. From that, you can quickly identify the problems you
321 should look at first.
323 <h2 id="pixelperfect">Examining and Designing User Interfaces with Pixel Perfect</h2>
325 Pixel Perfect is a tool for examining pixel properties and laying out UIs from a design drawing.
327 <h3 id="aboutpixelperfect">About the Pixel Perfect window</h3>
329 The Pixel Perfect window displays a magnified image of the screen that is currently
330 visible on the emulator or device. In it, you can examine the properties
331 of individual pixels in the screen image. You can also use the Pixel Perfect window
332 to help you lay out your application UI based on a bitmap design.
335 To see the Pixel Perfect window, run Hierarchy Viewer, as described in
336 the section <a href="#runhv">Running Hierarchy Viewer and choosing a window</a>. Next, click
337 <strong>Inspect Screenshot</strong> at the top of the device window. The Pixel Perfect window
341 In it, you see three panes:
345 View Object pane: This is a hierarchical list of the View objects that are currently
346 visible on the device or emulator screen, including both the ones in your application and
347 the ones generated by the system. The objects are listed by their View class.
348 To see the class names of a View object's children, expand the View by clicking the
349 arrow to its left. When you click a View, its position is highlighted in the Pixel Perfect
353 Pixel Perfect Loupe pane: This is the magnified screen image. It is overlaid by a grid in
354 which each square represents one pixel. To look at the information for a pixel, click in its
355 square. Its color and X,Y coordinates appear at the bottom of the pane.
357 The magenta crosshair in the pane corresponds to the positioning
358 crosshair in the next pane. It only moves when you move the crosshair in the next pane.
361 To zoom in or out on the image, use the <strong>Zoom</strong> slider at the bottom of
362 the pane, or use your mouse's scroll wheel.
365 When you select a pixel in the Loupe pane, you see the following information at the
370 Pixel swatch: A rectangle filled with the same color as the pixel.
373 HTML color code: The hexadecimal RGB code corresponding to the pixel color
376 RGB color values: A list of the (R), green (G), and blue (B) color values of the
377 pixel color. Each value is in the range 0-255.
380 X and Y coordinates: The pixel's coordinates, in device-specific pixel units.
381 The values are 0-based, with X=0 at the left of the screen and Y=0 at the top.
386 Pixel Perfect pane: This displays the currently visible screen as it would appear in the
389 You use the cyan crosshair to do coarse positioning. Drag the crosshair in the image,
390 and the Loupe crosshair will move accordingly. You can also click on a point in the
391 Pixel Perfect pane, and the crosshair will move to that point.
394 The image corresponding to the View object selected in the View Object pane is
395 outlined in a box that indicates the View object's position on the screen. For the
396 selected object, the box is bold red. Sibling and parent View objects have a light
397 red box. View objects that are neither parents nor siblings are in white.
400 The layout box may have other rectangles either inside or outside it, each of which
401 indicates part of the View. A purple or green rectangle indicates the View bounding box.
402 A white or black box inside the layout box represents the <strong>padding</strong>, the
403 defined distance between the View object's content and its bounding box. An outer white
404 or black rectangle represents the <strong>margins</strong>, the distance between the
405 View bounding box and adjacent View objects. The padding and margin boxes are white if
406 the layout background is black, and vice versa.
409 You can save the screen image being displayed in the Pixel Perfect pane as a PNG file.
410 This produces a screenshot of the current screen. To do this, click
411 <strong>Save as PNG</strong> at the top of the window. This displays a dialog,
412 in which you can choose a directory and filename for the file.
417 The panes are not automatically refreshed when you change one of the View objects or go to
418 another Activity. To refresh the Pixel Perfect pane and the Loupe pane, click
419 <strong>Refresh Screenshot</strong> at the top of the window. This will change the panes
420 to reflect the current screen image. You still may need to refresh the View Object pane;
421 to do this, click <strong>Refresh Tree</strong> at the top of the window.
424 To automatically refresh the panes while you are debugging, set
425 <strong>Auto Refresh</strong> at the top of the window, and then set a refresh rate
426 with the <strong>Refresh Rate</strong> slider at the bottom of the Loupe pane.
428 <h3 id="overlays">Working with Pixel Perfect overlays</h3>
430 You often construct a UI based on a design done as a bitmap image. The Pixel Perfect window
431 helps you match up your View layout to a bitmap image by allowing you to load the bitmap as an
432 <strong>overlay</strong> on the screen image.
435 To use a bitmap image as an overlay:
439 Start your application in a device or emulator and navigate to the Activity whose UI you
443 Start Hierarchy Viewer and navigate to the Pixel Perfect window.
446 At the top of the window, click <strong>Load Overlay</strong>. A dialog opens, prompting
447 for the image file to load. Load the image file.
450 Pixel Perfect displays the overlay over the screen image in the Pixel Perfect pane. The
451 lower left corner of the bitmap image (X=0, Y=<em>max value</em>) is anchored on the lower
452 leftmost pixel (X=0, Y=<em>max screen</em>) of the screen.
454 By default, the overlay has a 50% transparency, which allows you to see the screen
455 image underneath. You can adjust this with the <strong>Overlay:</strong> slider at the
456 bottom of the Loupe pane.
459 Also by default, the overlay is not displayed in the Loupe pane. To display it,
460 set <strong>Show in Loupe</strong> at the top of the window.
465 The overlay is not saved as part of the screenshot when you save the screen image as a PNG
469 A screenshot of the Pixel Perfect window appears in figure 4.
471 <img id="Fig4" src="{@docRoot}images/developing/hv_pixelperfect.png"
474 <p class="img-caption"><strong>Figure 4.</strong> The Pixel Perfect window</p>
475 <h2 id="layoutopt">Optimizing layouts with layoutopt</h2>
477 The <code>layoutopt</code> tool lets you analyze the XML files that define your
478 application's UI to find inefficiencies in the view hierarchy.</p>
481 To run the tool, open a terminal and launch <code>layoutopt <xmlfiles></code>
482 from your SDK <code>tools/</code> directory. The <xmlfiles> argument is a space-
483 delimited list of resources you want to analyze, either uncompiled resource xml files or
484 directories of such files.
487 The tool loads the specified XML files and analyzes their definitions and
488 hierarchies according to a set of predefined rules. For every issue it detects, it
489 displays the following information:
493 The filename in which the issue was detected.
496 The line number for the issue.
499 A description of the issue, and for some types of issues it also suggests a resolution.
502 <p>The following is a sample of the output from the tool:</p>
506 7:23 The root-level <FrameLayout/> can be replaced with <merge/>
507 11:21 This LinearLayout layout or its FrameLayout parent is useless
509 7:7 The root-level <FrameLayout/> can be replaced with <merge/>
511 -1:-1 This layout has too many nested layouts: 13 levels, it should have <= 10!
512 20:81 This LinearLayout layout or its LinearLayout parent is useless
513 24:79 This LinearLayout layout or its LinearLayout parent is useless
514 28:77 This LinearLayout layout or its LinearLayout parent is useless
515 32:75 This LinearLayout layout or its LinearLayout parent is useless
516 36:73 This LinearLayout layout or its LinearLayout parent is useless
517 40:71 This LinearLayout layout or its LinearLayout parent is useless
518 44:69 This LinearLayout layout or its LinearLayout parent is useless
519 48:67 This LinearLayout layout or its LinearLayout parent is useless
520 52:65 This LinearLayout layout or its LinearLayout parent is useless
521 56:63 This LinearLayout layout or its LinearLayout parent is useless
523 7:413 The root-level <FrameLayout/> can be replaced with <merge/>
524 -1:-1 This layout has too many views: 81 views, it should have <= 80!
526 7:19 The root-level <FrameLayout/> can be replaced with <merge/>
527 11:17 This LinearLayout layout or its FrameLayout parent is useless