ScrollView Child Must Not Use match_parent

ID

kotlin.android_scrollview_size

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

android, best-practice, reliability

Description

Reports direct children of a ScrollView (or NestedScrollView) that set android:layout_height to match_parent or fill_parent, or direct children of a HorizontalScrollView that set android:layout_width to match_parent or fill_parent. These values defeat the purpose of scrolling because the child never exceeds the container’s bounds.

Rationale

A scroll container works by allowing its child to be larger than the container itself, then scrolling the viewport. When the child’s dimension along the scroll axis is set to match_parent, it is constrained to the container’s own size and can never overflow. The scroll container becomes inert, and the user cannot scroll to content that extends beyond the visible area.

<!-- Bad: child fills entire ScrollView height -->
<ScrollView ...>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</ScrollView>

Remediation

Use wrap_content for the child’s dimension along the scroll axis so that the child can grow beyond the container.

<!-- Good: child wraps content, enabling scroll -->
<ScrollView ...>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</ScrollView>