I have been recently hired to maintain a very large program (only two Activities, about a hundred Fragments, and several hundred layouts). Furthermore, most of the content of the layouts (images and text) as well as the order that the layouts appear is dynamically determined via the company's Web API.
Unfortunately there is no documentation. No map, nada. The company hired a 3rd party to make this app long before they even had Android programmers. And the quality of the code is poor at best (even variable names are confusing and contradictory).
Consequently, I spend about 70% - 90% of my time simply searching for layouts and code just to change the background of Button.
Is there a tool that one can run (perhaps in Android Studio's debugger?) that can somehow spit the names of the layout files that are currently displayed?
My superiors often say something like, "Change that background texture from black to light gray." And I'm thinking: the change is trivial, but finding the xml file could take an hour.
The project that generated this question is no longer under my control, so this question has become moot. But it does seem to be a common issue, so I'm leaving this question open. Perhaps in the future some kind of tool/solution will be arise. I hope this thread is useful to other programmers in that situation.
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources).
Layout Validation is a visual tool for simultaneously previewing layouts for different devices and display configurations, helping you catch problems in your layouts earlier in the process.
There is also the Developer Assistant app for Android which can inspect a view hierarchy at runtime and then display the most probable layout names visible on the screen. The heuristic is involved so it won't work 100% accurate, but still can be helpful and works completely offline (a disclosure: I made this app).
An example:
In the Android Device Monitor, there is a button you can click on called "Dump View Hierarchy for UI Automator".
This will open a UI Hierarchy viewer in the Device Monitor, which you can use to see the resource IDs of each of your views.
While this does not actually give you the name of the inflated XML file, it could be very useful. If you are lucky, you can even grep for the resource IDs to narrow down the search for which XML files to look at.
And if you hover over the title of the viewer window, it will show you the path to the actual XML file that it created. This is a way to find all the resource IDs in one place.
Some more info on Hierarchy Viewer in case it helps:
The Hierarchy Viewer allows you to debug and optimize your user interface. It provides a visual representation of the layout's View hierarchy (the Layout View) and a magnified inspector of the display (the Pixel Perfect View).
Here's a blog post that contains something interesting - a custom layout inflater that intercepts all view inflation:
It may be possible to use that sample code to intercept the inflate calls, and receive the resource ID of the XML file:
LayoutInflator.inflate()
on Android Developer docsAt this point, you will need to turn the resource id into a useful name. Quoting from this question (How to get Resource Name from Resource id), you can use:
getResources().getResourceEntryName(int resid);
Add some logging and this may give you each XML file as it is being inflated.
This could be quite a nice way to document your entire project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With