Constraint Layout Tools Attribute Has No Effect
ID |
java.android_constraint_layout_tools |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
android, layout |
Description
Reports tools:context, tools:layout and app:layout_constraint* attributes declared on <merge> or <include> root elements in Android layout XML files. The Android tooling silently ignores these attributes in that position.
Rationale
A <merge> element is collapsed into its parent at inflation time, and an <include> element is replaced with the included layout’s root. In both cases the attributes that describe constraint-layout relationships or design-time preview targets are dropped before they reach the View hierarchy, so they only mislead future readers about the intended structure.
<!-- Bad: tools:context on a <merge> root has no effect -->
<merge xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<!-- Bad: app:layout_constraint... on an <include> is ignored -->
<include layout="@layout/sub"
app:layout_constraintTop_toTopOf="parent" />
</merge>
Remediation
Move the design-time preview attribute (tools:context, tools:layout) to the actual top-level layout file, or to the parent of the <include>. Move app:layout_constraint* attributes to the constraint layout that owns the view, not to its <merge> / <include> wrapper.
<!-- Good: tools:context on a real root layout -->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<include layout="@layout/sub" />
</androidx.constraintlayout.widget.ConstraintLayout>