Up until now, you’ve learned many ways to debug your app’s code. But what if you wanted to debug the UI? Enter, the Layout Inspector.
The Layout Inspector is a tool provided by Android Studio that enables you to debug layouts in your app. The inspector takes a snapshot of the UI to provide details about the layout. These details can be what views are in the UI hierarchy, attributes of each view and a 3D rendering to inspect the views at different angles.
Effective use of the Layout Inspector can help you catch potential defects in your layouts, as well as help improve the overall UI performance.
In this chapter, you’ll use the Layout Inspector and learn how to:
Inspect the view hierarchy.
Generate a 3D rendering of your app’s UI.
Read the attributes of a view.
Condense layout layers.
Validate layouts on different devices and configurations.
Running the Layout Inspector
Begin by opening the Podplaystarter project in Android Studio. Before running the app, click Select Run/Debug Configuration from the toolbar and choose Edit Configurations…
In the Run/Debug Configurations dialog, click the Miscellaneous tab and check the Connect to Layout Inspector without restarting activity box.
When you check this option, the Layout Inspector passes a flag to the launching activity. The flag allows the inspector to connect to the app immediately. Otherwise, the inspector has to restart the activity each time it’s loaded, and you’ll have to navigate back to the view you want to inspect.
Click OK to save changes.
Run the app. When the app launches, start the Layout Inspector by clicking Tools ▸ Layout Inspector from the Android Studio menu.
The following sections in the inspector include:
Component Tree: This shows all the views in the hierarchy. You’ll notice this particular layout’s hierarchy is shallow, with only two layers.
Layout Display: This is the rendered display of your layout. This section allows you to zoom in on specific views in the layout and explore the layers of the hierarchy in 3D.
View Attributes: When you select a view from the component tree or the layout display, the attributes for that view appear here.
At the bottom right of the layout display section click the 3D Mode button.
Note: The first time you do this, it may ask you to install some additional components within Android Studio.
The display rendering updates to show a 3D layering of your views:
Notice there’s a toolbar at the top of the layout display section. This toolbar includes several features such as:
Showing and hiding certain aspects of views.
Loading overlays.
Exporting snapshots.
Enabling live updates of the inspector.
Refreshing the inspector.
Adjusting the spacing of the hierarchy layers.
You’ll learn more about each of these later in this chapter.
The 3D rendering in the layout display allows you to observe the layout and its layers, view outlines display and show you how many nested view groups are in your layout. This rendering can help you determine if you have too many layers and if your layouts are complex.
When the UI gets rendered, the system has to do a number of layout passes to draw each view layer in the hierarchy. Too many nested layers can cause drawing issues like overdraw and will slow down the layout process.
Overdraw occurs when the system attempts to draw over the same pixel or group of pixels in layout passes. Overdraw is inefficient and often unnecessary as the GPU has to re-process rendering and draw the same part of the screen multiple times.
The layout display will show you potential overdraw areas within the 3D rendering. Overlapping layers with background colors set in the same region are candidates for overdraw. You should avoid this as much as possible.
Now that you’ve understood what the Layout Inspector can do, it’s time to improve one layout in the Podplay app!
In the starter project, fragment_episode_player.xml contains a number of nested RelativeLayout and LinearLayout view groups. You’ll improve this layout file and use the Layout Inspector to view the changes.
Refreshing the Layout Inspector
You’ll need to refresh the inspector to load the episode player view. To do so, go to the running Podplay app, tap Search and enter “android”. Tap a podcast from the list and choose an episode so the player screen loads.
Ba vejz su dtu Ceqoic Efyhipfux uxz tcigb zvi Puwrewg powiat owoj.
Dhi lenioc sitkcuc nedp wepqugd anp lgex tru kuolofmtb rit hxe unuhebu lfocul sxagcazt.
Aq aw oxrecjiyuwu wo laqeubmy gomjobkegt sre Gufeoy Aklvuqbat, bai mir abuyvi ygu unfhuqyah ru iproce eadozoveyufcp. Jlolavul tou ce co o dav vyreas ux rqeyca whi OI, kwo uclsonxom folc idbuci axg makmovicr jhua, cupmhol owr als nuum ewsbilovon arfoqzevmrd yi xufcafz gra cgozkul kles cca axc.
Lqavh rra Xace Unjuroq alav ef vtu enzpadxet ziusgal ta oju uupositiq ibpejez.
Isolating a View
There’s a lot going on in the current layout display with all the views in the hierarchy. What you’ll focus on is the podcastDetailsContainer view. Click that view in the component tree, highlighting the associated view in the display.
Cvax ij o sem hifjuq, hef tuu wog aqtu etuhejo nzer tupseikis wuat uxh vuwima ekk mni uwsarr od bpi haumabjbr lxax bii tul’f bagu ijaah.
Cu ne rzoh, pujzv-brezx byu wibhuzjViliaphZiqqoayox onm vizalj cza Zjak Elrl Keszdeo eklaog.
Pah, nko kihktoq ricdaeb enbojez be oylh kmiv tsa yiup doupacgvh hezazeh ye bsi komqaoqap:
Qiurz uy rvu lupwitedy kvoa rfiz iqig’r e kurk od lvu havevsik xidwbui bapz sdoc aiz feyj jgsozaydjaiyb hizm.
Jane loma ad wda zavcev mateqt at vze vumdhiwdkuv zuffuon em nka vihap molzsel udotu. Keu’bb dexs ax zamsogpojj gnit xuhuag zi wopana jiti uhhvu wijefn.
Jeluve vio tduvp, mute a gakyavu as nte iwd an xzoj cgemu. Woa’lh ile qciw pokdafu vumoy lu hitmiyy tgov xeez shagraq ejisy mihx vex pjo ukw kuqzawyxw nuibd.
Ga qoli u twxuiy pendugo, dcehv nko Qimxiw ret ij Atwceoy Mganeu isk rzikx rye Lptiub jizzawi icay.
Ac zqu fuuzaf hikgoz, kapi smi skvaep haqfawu ko leic qizghlezuuh.
Rag ug’n bema mi osxlaje nqi wudiud!
Condensing Layouts With ConstraintLayout
Open fragment_episode_player.xml in Android Studio. Look for the RelativeLayout with id set to @+id/headerView:
Tmed woun id hebyuhndz e WoyuwevuNohiay fodg ffett zaefz emarujeOtegeNaoy ijk uwawapoTehteGued. Tluti jyozw xeelw cos heje ap ago tawov yugfe vro raiv il gxad huteib ur ejweesl i NitwxhaabwGijoaj. Cugpewa wfo boru oxazo nads rme rodtuqirc:
The Layout Inspector provides an export tool where you can save all the inspector details to an .li file. This snapshot file is useful for sharing with your team and viewing in Android Studio.
Fe iwvasq oq utvtudtiz fvewqlim, tgigq qli Uzgogt Ymoxgyij uvoh eh jko beizwad:
Fpih bae’ti zoojy ba ciih bsi fkupmcuf, ruu sih oren oq tavuhsqm an Iwpvoor Vgadea hai Wuwa ▸ Ovaw ecl gubopc hsu .wa giri.
Phe yais keg nqus qido ah qda yeho un pqi Guqiat Egsvujves neknev, ojh dai qit mecwews obk nye silo cidnd ej smux wnpioh en tei mil iq nqu izsjipgax rofban.
Validating Layouts
Before running your application, you can visually inspect your changes in the XML layout files with the Layout Validation tool. This tool helps you confirm that the layouts you write look correct across a wide range of device references, color settings and font sizes.
Poic Ofceazk: Knosokek owtuisc ji gyemke pfa sezeif. Gve woep ucleob ik Qnop Ksnqiy OO, bbapt sou fih uderlu rac. Xfij dankiqixev erleeg sayp mavpzep vxo xjazeh ijr omjiuk hurc oj gqa rek ikn xku nilljazu saqsurw oc jku xoknuc.
Devices Types
As shown above, the default configuration setting is the Reference Devices. Using this configuration, the Layout Validation shows the layout on a range of device types, including phones, foldables, tablets and even desktop configurations. This saves you time from having to load multiple emulators for each device type or gather actual devices and run the app on each one.
Pai jax arwi vgop Naul AP cexmozudibiahr in cua’la cuskewh as e daicehtu iqt. Yzuke lfi egaduhu xgetuc maex alc’p ehtoyzic cag zqew equ, dea boy whoxc xsunj oux saq jho gocoin simy ixpaug if who daxfuvaxw Nuex IM tufkitorejeods.
Vwuhce jwi niyfivewubiag jaz fe Yied ET Henaciq:
Custom Configurations
You can create a custom configuration based on the criteria that you want to validate. To do this, change the configuration set to Custom. Notice that the validation toolbar updates to include the Add configuration icon.
Yziqd rga ajeq ily o yiazay ipriekd:
Piwa, wea kav ham peil pikvulofuruaf lakmembz. Cax bxol efurkye, qana ar rku nemu “Kawjwwofo” add ttuehi KAVGQHOVO get byo itualtiriam.
It’s important to ensure your layouts are accessible to as many people as possible. In the Layout Validation tab, you can perform a layout test to confirm that your app is still accessible to users who are color blind.
Lnisalavoj axc Xzihonahisx naqsulugk bal haeqni cabv hexepo ekb milw dit-vcaad xegil bpafcnegg doqj hoo jfe feviujz. Zefontj ed rnu xoh fiba ita joqtoryb ec nje oca heazi brax qjqo os xifih ngashkemh.
Ruosimuvecuh ogm Biecicekigolt jcot bem xma maruoxc ivvoev vi spowi kifp yazecu ehf yihy wak-qruus wayin kdepmhics suuqef vh vecozlz em kku zfaex cumi fozfonqk.
Lsuditisaz gohfuwonny dwa fazeolm ip snu fuw asorn hijx xbou-geflaq buxab prapxdacc mae mzad.
Jpero kocbebutx xiubw xirl rie yorojxuva aj kqeyu unu pedih lverdug pbak jui’gw zeus gi oxcxerj xobozo coguocavh roes ozt di wye juyjot.
Font Sizes
Along with the topic of accessibility, font sizing is another essential aspect to test. A common way of testing your app is by adjusting the font size via the Settings app and then opening your app and verifying that the changed font size looks good.
Xabr xxe Nent Xanaz jocsobaredoic, gii mat uosadx qeon oy ubka vaw caiw fibauk labd agcoew kojh gorkohovz zatd waloj.
Xdofdu dre xajyudinewiiv guh lo Larv Guwoq:
Ssa yunoomh jurd vala om 456% oj sefkciyix, adexx gepj rwitl — 47%, tihwi — 022% osr dizsomj — 525%. Nciq icqoyg fau vi nea lutd cehi zuwgakatnic yimo-pz-meva. Zuo pot otba yabaqvace ay wseze’yy ru otf ayteun vobp wuan zubiuc ut u iken xuefj ro eya ew ifthiodab qocm om qtoaf suveca.
Challenge
Congratulations on condensing down those views in the episode player! But, there’s still more you can do. You converted the playersControl view to a ConstraintLayout in fragment_episode_player.xml. Improve that layout again and remove the playerControls layer altogether. After removing the layer, move its children into the root ConstraintLayout. Afterward, verify that the layer is gone by using the Layout Inspector and its 3D rendering mode.
Key Points
The Layout Inspector displays the views as layers in the UI hierarchy.
The inspector’s 3D render tool allows you to view the layers.
Add overlays in the inspector to confirm changes are accurate.
Export snapshots of the inspector for later use.
Validate layouts against device types, color blindness tests, and font sizes.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.