Wrong Constraint Layout

ID

java.android_wrong_constraint_layout

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

android, reliability

Description

Flags direct children of androidx.constraintlayout.widget.ConstraintLayout (or the legacy support-library android.support.constraint.ConstraintLayout) that declare no app:layout_constraint* attribute.

Rationale

ConstraintLayout positions its children only through constraints. A child without any constraint falls back to coordinate (0,0) and overlaps the upper-left corner of the parent, which is almost always a mistake and produces an unreadable UI on the device.

<!-- Bad: TextView has no constraints, lands at (0,0) -->
<androidx.constraintlayout.widget.ConstraintLayout>
  <TextView android:text="Unconstrained" />
</androidx.constraintlayout.widget.ConstraintLayout>

<include>, <merge> and <requestFocus> children are exempt because their constraints are declared on the <include> tag itself or are not view tags at all.

Remediation

Add at least one horizontal and one vertical constraint to every direct child.

<!-- Good: TextView constrained to parent -->
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto">
  <TextView
      android:text="Constrained"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>