Android Dev Tip #5
tools:parentTag
Tip:
When using merge as the root element of the layout of your Compound View, tools:parentTag can help you utilise all the features of the Design tool.
Explanation:
Most of the times, when creating a Compound View you want to inflate the layout xml directly into the created ViewGroup
. Without introducing any additional layouts in-between. That’s why you should be really familiar with how merge
tag works in layout xmls. Or at least what’s (probably) the most typical use-case of this tag.
The easiest way to understand it, would be:
- When creating a Compound View:

- With
R.layout.layout_example_compound_view
being:

- When the
ExampleCompoundView
is inflated:
this:

becomes this:

By default your layout design tool will not know what is the eventual type your merge
tag becomes. This can be a bit annoying if:
a) You like to use the design tool to set some of your View’s
parameters.
or
b) You like to peek at the Design visualisation to see if what you’re doing in xml is correct.
Here’s where the parentTag
parameter (of the tools
namespace) comes in handy. It provides you with a way of telling the Design tool, what will be the eventual ViewGroup
of the merge
tag.
tools:parentTag="your.package.ExampleCompoundView"
(You will also need to add android:layout_width
, android:layout_height
to your merge
tag for the Design tool to work properly).
Example:
For the layout:

the design tool looks like this:

It is not aware that the merge
tag represents a ConstraintLayout
, therefore it cannot take into account any of the enclosed ImageView’s
layout parameters (and as a result cannot display anything).
If you add the parentTag
parameter to the layout (+ layout_width
and layout_height
):

The design tool will understand that the merge tag really is a ConstraintLayout
, and it will display the enclosed elements correspondingly:

Remember that it doesn’t have to be exactly ConstraintLayout
. It can be any other ViewGroup
(including any compound view of yours that will extend some ViewGroup
).
If you enjoyed this post, please show your support! Recommend, follow, comment, share.
This really means a lot!
