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.
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:
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:
Using post ensures the layout has already been measured before you try to scroll.
You can also scroll to a specific child view:
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
- '
ScrollViewprovides vertical scrolling for content larger than the screen.' - It must have exactly one direct child, usually a layout container such as
LinearLayout. - '
fillViewportis useful when shorter content should still fill the visible area.' - Use programmatic scrolling only after layout is complete.
- Prefer
RecyclerViewoverScrollViewfor long or dynamic lists.
Related reading
- How to use SharedPreferences in Android to store, fetch and edit values
- How to use Swift autoclosure
- How to use Swift documentation comments
- How to use Swift struct in Objective-C
- How to use the background thread in Objective-C?
- How to use ThreeTenABP in Android Project
- How to use UIScrollView in Storyboard
- How to use UIVisualEffectView to Blur Image?
.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.