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.
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.
The important pieces are:
- '
ScrollViewis the parent' - it has one direct child
- the inner
LinearLayoutuseswrap_contentfor 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:
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:
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:
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:
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
ScrollViewinstead of wrapping them in one container. - Setting the inner
LinearLayoutheight tomatch_parentwhenwrap_contentis needed for scrolling content. - Using
ScrollViewfor huge repeated lists instead ofRecyclerView. - Forgetting
fillViewportand getting an undersized layout on short screens. - Mixing weight-based height logic into a scrolling layout and expecting fixed-sibling behavior.
Summary
- '
LinearLayoutis not scrollable by itself; wrap it inScrollVieworNestedScrollView.' - A
ScrollViewcan have only one direct child, usually theLinearLayout. - Use
wrap_contenton the inner layout so it can grow beyond the screen height. - Use
HorizontalScrollViewfor sideways scrolling. - For large repeated lists, prefer
RecyclerViewinstead of a giant scrollableLinearLayout.
Related reading
- How do you move a CALayer instantly without animation
- How do you obscure text in a password field in an iPhone Application?
- How do you perform wireless debugging in Xcode 9 with iOS 11, Apple TV 4K, etc?
- How do you run a task in the background in flutter?
- How do you save/store objects in SharedPreferences on Android?
- How do you set EditText to only accept numeric values in Android?
- How do you share data between view controllers and other objects in Swift?
- How do you synchronise projects to GitHub with Android Studio?
.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.