LinearLayout
ScrollView
Android Development
UI Layout
Layout Issue

LinearLayout not expanding inside a ScrollView

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

When a LinearLayout sits inside a ScrollView, developers often expect it to expand like a normal full-height parent. That expectation fails because ScrollView measures its child differently from a regular layout. The usual fix is to give the hierarchy the right width and height rules and, when you want the child to fill at least the visible screen, enable fillViewport.

Remember the ScrollView Rules

ScrollView has two layout rules that matter immediately:

  1. it can have only one direct child
  2. it measures that child with scrolling behavior in mind

A common XML structure looks like this:

xml
1<ScrollView
2    android:layout_width="match_parent"
3    android:layout_height="match_parent"
4    android:fillViewport="true">
5
6    <LinearLayout
7        android:layout_width="match_parent"
8        android:layout_height="wrap_content"
9        android:orientation="vertical">
10
11        <!-- child views -->
12
13    </LinearLayout>
14</ScrollView>

This is the normal starting point. match_parent on width is important, and fillViewport="true" helps when the content should stretch to fill the visible screen even if it is short.

Why fillViewport Often Solves the Problem

Without fillViewport, the child inside a ScrollView is allowed to be only as tall as its content. If the content is short, the inner LinearLayout may not expand to occupy the viewport, which makes weights and alignment behave differently than expected.

Setting:

xml
android:fillViewport="true"

tells the ScrollView to size its child to at least the height of the viewport. This is the fix people most often need when they say the inner layout is not expanding.

Be Careful with layout_weight

Weights inside a LinearLayout under a ScrollView are another common source of confusion. If the vertical space is effectively unbounded, layout_weight does not behave like it does in a normal fixed-height parent.

For example, this can be misleading:

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical">
5
6    <View
7        android:layout_width="match_parent"
8        android:layout_height="0dp"
9        android:layout_weight="1" />
10
11</LinearLayout>

If your intention is "take the remaining screen height", that works much better once the ScrollView is using fillViewport="true".

Width Problems Can Look Like Height Problems

Developers sometimes focus on height and miss the real issue: the child layout does not have match_parent width, so descendants measure strangely and the overall layout feels collapsed.

A safer inner child configuration is:

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical" />

That gives the inner layout the full horizontal space available from the scroll view.

When to Consider NestedScrollView

If the screen uses modern AndroidX layouts with AppBar behavior or other nested scrolling interactions, NestedScrollView may be a better fit than the platform ScrollView. The expansion rules are similar, but nested scrolling integrates better with the rest of the support library stack.

Still, the core fix remains the same:

  • one direct child
  • 'match_parent width'
  • 'fillViewport="true" when full-height behavior is desired'

Common Pitfalls

The biggest mistake is expecting the inner LinearLayout to behave like a normal full-height parent without enabling fillViewport. Inside a scroll container, the measuring rules are different.

Another issue is adding multiple direct children to the ScrollView. That is invalid for ScrollView and often leads to confusing layout results or runtime problems.

Weights are another trap. They are not meaningless inside a ScrollView, but they depend on the parent having a meaningful viewport size to distribute against.

Finally, avoid forcing arbitrary fixed heights just to make the layout "look right". That usually hides the measurement problem instead of solving it.

Summary

  • A ScrollView should contain one direct child, often a LinearLayout.
  • Use match_parent width on the inner layout so descendants measure correctly.
  • Turn on android:fillViewport="true" when the child should expand to at least the visible screen height.
  • Be careful with layout_weight because scrolling parents change how height is measured.
  • Fix the measurement rules instead of patching the UI with hard-coded heights.

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.