Erroneous Layout Attribute for Parent Type

ID

kotlin.android_erroneous_layout_attribute

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

android, layout

Description

Reports layout attributes that only have an effect when used inside a specific parent ViewGroup. For example, android:layout_below is a RelativeLayout.LayoutParams attribute and is silently ignored when the parent is a LinearLayout or FrameLayout. Similarly, app:layout_constraint* attributes only apply inside a ConstraintLayout.

Rationale

LayoutParams are owned by the parent ViewGroup subclass. An attribute that is not understood by the parent is parsed without error and then discarded, so the resulting on-screen layout never reflects what the source XML describes. The bug is usually discovered only at runtime — and even then only because the layout looks visually wrong.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Bad: layout_below only works inside RelativeLayout -->
    <TextView android:layout_below="@id/title" />
</LinearLayout>

Remediation

Either change the parent ViewGroup to one that supports the attribute, or replace the attribute with one that the current parent understands (android:layout_weight for LinearLayout, app:layout_constraint* for ConstraintLayout, etc.).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Good: layout_below works here -->
    <TextView android:layout_below="@id/title" />
</RelativeLayout>