If you’re using the Android Emulator, you can skip this section. Otherwise, you need to set up your device as follows.
Note: Your device must be running Android 4.1 or higher.
Enable Developer Options on your device.
Set the environment variable ANDROID_HVPROTO=ddm on your development machine.
This variable tells Hierarchy Viewer to connect to the device using the ddm protocol, which is the same as the DDMS protocol. The caveat is that there can be only one process on the host that connects to the device, so you must kill any other DDMS session to run Hierarchy Viewer.
Start Hierarchy Viewer
Figure 1. Android Device Monitor
Connect your device to your computer. If prompted by a dialog on the device that asks, Allow USB debugging?, tap OK.
Open your project in Android Studio, build and run it on your device.
Select Tools > Android > Android Device Monitor. Android Studio might show a Disable adb integration dialog because only one process can connect to the device via adb at once, and Android Device Monitor is requesting a connection. So click Yes.
Figure 1 illustrates what initially appears in the Android Device Monitor.
In the menu bar, select Window > Open Perspective, and then click Hierarchy View.
You should see an arrangement similar to what’s shown in figure 2. If not, select Window > Reset Perspective to return to the default layout.
Double-click your app’s package name in the Windows tab on the left. This populates the panes with the view hierarchy of your app.
Get familiar with the tools
Figure 2. Hierarchy Viewer panes.
The Hierarchy Viewer provides the following panes (as shown in figure 2):
Tree View (center): Shows a tree view of your view hierarchy. You can drag and zoom the tree using your mouse and the zoom control at the bottom. Each node indicates it’s View class name and ID name.
Tree Overview (top right): Gives you a bird’s-eye view of your app’s complete view hierarchy. Move the grey rectangle to change the viewport that’s visible in the Tree View.
Layout View (bottom right): Shows a wireframe view of your layout. The outline of the currently selected view is red, and its parent view is light red.
Clicking a view here also selects it in the Tree View, and vice versa.