Android
LinearLayout
ScrollView
User Interface
Mobile App Development

How do you make a LinearLayout scrollable?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

LinearLayout itself does not scroll. If the combined size of its children exceeds the available space, you need a scrolling parent such as ScrollView, HorizontalScrollView, or sometimes NestedScrollView. The main rule is simple: the scrolling container wraps the layout, not the other way around.

Use ScrollView for Vertical Content

For a vertically scrolling screen, wrap the layout in ScrollView. The ScrollView can have only one direct child, so that child is usually the LinearLayout that contains the rest of the form or content.

xml
1<?xml version="1.0" encoding="utf-8"?>
2<ScrollView 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="Profile" />
17
18        <EditText
19            android:layout_width="match_parent"
20            android:layout_height="wrap_content"
21            android:hint="Name" />
22
23        <EditText
24            android:layout_width="match_parent"
25            android:layout_height="wrap_content"
26            android:hint="Address" />
27
28    </LinearLayout>
29</ScrollView>

The important pieces are:

  • 'ScrollView is the parent'
  • it has one direct child
  • the inner LinearLayout uses wrap_content for height

Why fillViewport Helps

android:fillViewport="true" is optional but useful. Without it, a small amount of content may collapse to its intrinsic height and not fill the screen. With it, the child can stretch to at least the viewport size, which usually produces better layout behavior for forms and settings screens.

This is especially helpful when you want a button bar or content block to align as if the screen were full height, even before the content becomes long enough to scroll.

Horizontal Scrolling Uses a Different Container

If the layout needs to scroll sideways rather than vertically, use HorizontalScrollView:

xml
1<HorizontalScrollView
2    xmlns:android="http://schemas.android.com/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content">
5
6    <LinearLayout
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:orientation="horizontal">
10
11        <Button
12            android:layout_width="wrap_content"
13            android:layout_height="wrap_content"
14            android:text="One" />
15
16        <Button
17            android:layout_width="wrap_content"
18            android:layout_height="wrap_content"
19            android:text="Two" />
20
21    </LinearLayout>
22</HorizontalScrollView>

The concept is identical, but the scroll direction changes.

Common Layout Mistakes

The most common mistake is putting multiple direct children inside ScrollView. That is invalid because ScrollView accepts only one child view. If you need several views, group them inside a single container such as LinearLayout or ConstraintLayout.

Another mistake is using layout_height="match_parent" on the inner LinearLayout when the intent is a naturally expanding content column. wrap_content is usually the right choice for the scrolling child because it allows the content to grow taller than the screen.

NestedScrollView for Modern Screens

In AndroidX projects, NestedScrollView is often preferable when your layout is part of a more complex coordinated screen. It cooperates better with nested scrolling behavior used by components such as AppBarLayout.

Example:

xml
1<androidx.core.widget.NestedScrollView
2    xmlns:android="http://schemas.android.com/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent">
5
6    <LinearLayout
7        android:layout_width="match_parent"
8        android:layout_height="wrap_content"
9        android:orientation="vertical">
10
11        <!-- content -->
12
13    </LinearLayout>
14</androidx.core.widget.NestedScrollView>

If you are building a simple standalone screen, plain ScrollView is usually enough. If the screen participates in nested scroll behavior, NestedScrollView is a better fit.

When Not to Use LinearLayout Inside a Scroll Container

If you have a very long or dynamic list of repeated items, do not build it as hundreds of child views inside a LinearLayout. That approach inflates all views at once and performs poorly.

For repeated scrolling content, use RecyclerView:

kotlin
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = UserAdapter(users)

RecyclerView recycles off-screen rows, which is far more efficient than a giant LinearLayout in a ScrollView.

Weight and Measurement Issues

Developers often combine layout_weight with a scroll container and then get surprising results. Inside a ScrollView, height weights are usually not the right tool because the child is being measured in a scrolling context rather than in a fixed-height sibling layout.

If a child view needs a minimum size, use explicit dimensions or minHeight instead of relying on weighted height distribution inside the scrollable content column.

Programmatic Setup

You can also build the hierarchy in code:

kotlin
1val scrollView = ScrollView(this)
2val container = LinearLayout(this).apply {
3    orientation = LinearLayout.VERTICAL
4}
5
6scrollView.addView(container)
7setContentView(scrollView)

The same rule still applies: the scroll container owns one child, and that child holds the real content.

Common Pitfalls

  • Putting several direct children inside ScrollView instead of wrapping them in one container.
  • Setting the inner LinearLayout height to match_parent when wrap_content is needed for scrolling content.
  • Using ScrollView for huge repeated lists instead of RecyclerView.
  • Forgetting fillViewport and getting an undersized layout on short screens.
  • Mixing weight-based height logic into a scrolling layout and expecting fixed-sibling behavior.

Summary

  • 'LinearLayout is not scrollable by itself; wrap it in ScrollView or NestedScrollView.'
  • A ScrollView can have only one direct child, usually the LinearLayout.
  • Use wrap_content on the inner layout so it can grow beyond the screen height.
  • Use HorizontalScrollView for sideways scrolling.
  • For large repeated lists, prefer RecyclerView instead of a giant scrollable LinearLayout.

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.