Android - how to make a scrollable constraintlayout?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ConstraintLayout does not scroll by itself. If the content can grow beyond the screen, the usual solution is to place the ConstraintLayout inside a ScrollView or NestedScrollView, make the scrolling container the direct parent, and let the constrained content use wrap_content height.
The Correct Structure
The common pattern is:
This works because:
- the scroll container provides vertical scrolling
- the
ConstraintLayoutmeasures to the full height of its content - '
fillViewport="true"helps short content still fill the screen nicely'
If you skip the scroll container, the layout will simply clip or compress content instead of scrolling.
Why wrap_content Matters
The inner ConstraintLayout should normally use android:layout_height="wrap_content". If you give it match_parent, the layout may only occupy the viewport height, which defeats the point of making the content area expandable.
A working example:
Why NestedScrollView Is Often Better
ScrollView can work for simple cases, but NestedScrollView is usually safer in modern Android projects because it cooperates better with other nested scrolling components and coordinator-style layouts.
If your screen includes app bars, collapsing toolbars, or nested containers, NestedScrollView tends to integrate more cleanly.
Constraints Still Need to Be Valid
Placing ConstraintLayout inside a scrolling parent does not change how constraints work. Every child still needs valid vertical constraints so the layout can compute its total height.
Typical mistakes include:
- child views constrained only horizontally
- a final view that is not constrained downward from the previous content
- using
0dpheights without the proper top and bottom constraints
If the content height is ambiguous, the scroll container cannot measure the full scrollable area correctly.
When Not to Use This Pattern
If the screen is essentially a long list of repeated rows, RecyclerView is usually better than one giant scrolling ConstraintLayout. A scrollable ConstraintLayout is appropriate for:
- forms
- profile screens
- settings screens
- mixed static content
It is not ideal for hundreds of repeated items because the whole view tree is still inflated at once.
Programmatic Setup
If you build the layout in code, the principle is the same:
The scroll container wraps the content container. ConstraintLayout is still the content layout, not the scrolling mechanism.
Common Pitfalls
- Expecting
ConstraintLayoutitself to scroll without wrapping it inScrollVieworNestedScrollView. - Giving the inner
ConstraintLayouta height ofmatch_parentinstead ofwrap_content, which prevents it from expanding with content. - Forgetting
fillViewport="true"and then getting awkward empty space behavior on short screens. - Leaving child views under-constrained so the layout cannot calculate its full content height correctly.
- Using one huge scrollable layout for data that really belongs in a
RecyclerView.
Summary
- '
ConstraintLayoutbecomes scrollable by placing it insideScrollViewor, more commonly,NestedScrollView.' - The inner
ConstraintLayoutshould usually havewrap_contentheight. - '
fillViewport="true"improves behavior when the content is shorter than the screen.' - Valid child constraints are still required so the scrollable content height can be measured correctly.
- For repeated large datasets, use
RecyclerViewinstead of one oversized scrollingConstraintLayout.

