Android Dev-Tip #6
Layout comments
Tip:
If some part of your layout xml is not clear right off the bat (e.g. the existence of seemingly unnecessary View or weird parameter), mark your intentions with a comment (in this xml).
Explanation:
You can treat this tip as a paraphrase of Explanation of Intent section (chapter: Comments) from the:
With the specifics of Android SDK this tip gets a special meaning if you consider layout xmls.
The things you can set in the layout xml can sometimes be just not enough for your case. E.g. you might need to introduce a seemingly unnecessary View
to make your layout
work as expected or tweak some parameters of your Views
from code to make them look exactly right.
Adding a simple comment (in the xml) to a particular View
is a simple solution that can be priceless for the future You or some other fellow developer that will have to work with your code. It can save hours of trying to understand a seemingly broken layout by just reading the tip.
Example 1:
Assume you’ve cannot depend just on using dimens
to calculate the value of View’s
padding
. You need to utilise few lines of your code to do that.
Add a simple comment to your layout xml, so when you get back to this part of your app, you don’t need to spend any time figuring out the source of the padding
in this FrameLayout
.
Example 2:
See Saul’s response to the post.