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.
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:
- it can have only one direct child
- it measures that child with scrolling behavior in mind
A common XML structure looks like this:
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:
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:
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:
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_parentwidth' - '
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
ScrollViewshould contain one direct child, often aLinearLayout. - Use
match_parentwidth 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_weightbecause scrolling parents change how height is measured. - Fix the measurement rules instead of patching the UI with hard-coded heights.
Related reading
- Linker Command failed with exit code 1 use -v to see invocation, Xcode 8, Swift 3
- Listing all extras of an Intent
- LLDB Swift Casting Raw Address into Usable Type
- LLDB Swift Casting Raw Address into Usable Type
- Load a UIView from nib in Swift
- Load dimension value from res/values/dimension.xml from source code
- Load HTML file into WebView
- Load local html into UIWebView using swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.