Android
ScrollView
Android Development
Mobile App Development
User Interface Design

How to use ScrollView in Android?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

ScrollView is the standard Android container for vertically scrolling content that is larger than the screen. The important rule is that a ScrollView can have only one direct child, so the usual pattern is to place a layout such as LinearLayout inside it and put the real content inside that child.

Start with the Correct Layout Structure

Here is the basic XML pattern:

xml
1<ScrollView
2    xmlns:android="http://schemas.android.com/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent"
5    android:fillViewport="true">
6
7    <LinearLayout
8        android:layout_width="match_parent"
9        android:layout_height="wrap_content"
10        android:orientation="vertical"
11        android:padding="16dp">
12
13        <TextView
14            android:layout_width="match_parent"
15            android:layout_height="wrap_content"
16            android:text="Long content goes here" />
17
18        <Button
19            android:layout_width="wrap_content"
20            android:layout_height="wrap_content"
21            android:text="Save" />
22
23    </LinearLayout>
24</ScrollView>

The LinearLayout is the single direct child. That child can then contain as many views as needed.

Know When fillViewport Helps

android:fillViewport="true" tells the ScrollView to stretch its child to at least the height of the viewport. That is useful when the content is sometimes short and you still want child layouts such as match_parent or gravity-based positioning to behave nicely.

Without it, short content may collapse to its natural height instead of filling the screen.

Access and Control the Scroll Position

In code, you can scroll programmatically:

kotlin
1val scrollView = findViewById<ScrollView>(R.id.scrollView)
2
3scrollView.post {
4    scrollView.fullScroll(View.FOCUS_DOWN)
5}

Using post ensures the layout has already been measured before you try to scroll.

You can also scroll to a specific child view:

kotlin
1val target = findViewById<View>(R.id.submitButton)
2scrollView.post {
3    scrollView.smoothScrollTo(0, target.top)
4}

That is useful on forms where validation should move the user to the next field or to the first error.

Use the Right Container for the Right Job

ScrollView is good for forms, settings screens, static text, and small groups of views. It is not the right tool for long or dynamic lists. For lists of many repeated items, use RecyclerView instead of placing dozens or hundreds of child views inside a ScrollView.

That difference matters for performance and memory use.

There is also HorizontalScrollView for horizontal scrolling, but the same "single direct child" rule still applies there.

In Jetpack-based screens, the same layout principles still apply even if the view is hosted inside a fragment rather than an activity. The scrolling container should own the vertical scroll behavior, and child content should describe its height with wrap_content where appropriate.

If the screen includes validation errors or dynamically expanded sections, test those states too. Scroll behavior that looks fine with static sample content can become awkward once the real form grows.

That extra testing usually reveals focus and keyboard issues early.

It also improves overall form usability.

Common Pitfalls

The biggest mistake is putting multiple direct children under ScrollView. Android allows only one direct child view.

Another common issue is nesting other scrolling containers inside it without understanding touch and layout consequences. A RecyclerView inside a ScrollView is often a sign that the screen structure should be redesigned.

People also use match_parent for the child height when wrap_content is the real intention. That can cause confusing layout results.

Finally, if the screen uses keyboard input, test how focus and scrolling behave when the soft keyboard appears. Scrollable forms often need extra attention there.

Summary

  • 'ScrollView provides vertical scrolling for content larger than the screen.'
  • It must have exactly one direct child, usually a layout container such as LinearLayout.
  • 'fillViewport is useful when shorter content should still fill the visible area.'
  • Use programmatic scrolling only after layout is complete.
  • Prefer RecyclerView over ScrollView for long or dynamic lists.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.