ScrollView Must Have Exactly One Child

ID

java.android_scrollview_count

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

medium

Resource

Reliability

Language

Java

Tags

android, best-practice, reliability

Description

Reports ScrollView, HorizontalScrollView, or NestedScrollView elements that contain more than one direct child. The Android framework only renders the first direct child of a scroll container; additional children are silently ignored.

Rationale

A ScrollView is designed to host exactly one direct child that is typically a LinearLayout or another ViewGroup wrapping the scrollable content. When multiple direct children are placed inside a scroll container, only the first child participates in scrolling. The remaining children are either invisible or rendered in an undefined position, producing a broken layout.

<!-- Bad: multiple direct children -->
<ScrollView ...>
    <TextView android:text="First" />
    <TextView android:text="Second" />
</ScrollView>

Remediation

Wrap all content in a single ViewGroup such as LinearLayout.

<!-- Good: single child wrapping content -->
<ScrollView ...>
    <LinearLayout android:orientation="vertical" ...>
        <TextView android:text="First" />
        <TextView android:text="Second" />
    </LinearLayout>
</ScrollView>