Android ScrollView Examples

If you have so many widgets in a layout hierarchy that they can’t fit or be displayed on the screen, you should use ScrollView. ScrollView allows users to scroll view hierarchy allowing users to see all widgets in a view hierarchy when view hierarchy is longer than the physical display.

ScrollView is a subclass of frame layout. As frame layout allows only one child, you can place only one child view in scroll view. Child of scroll view can contain entire view hierarchy.

For just TextView and ListView, you don’t need to use scroll view as scrolling is taken care of by those views themselves.

Vertical scrolling can be created using ScrollView, but to create horizontal scrolling you need to use HorizontalScrollView.

To create nested scrolling, meaning parent view and child view can have separate scrolling in a layout, you need to use NestedScrollView.

ScrollView and ConstraintLayout Example

Below layout shows scroll view with constraint layout. To know more about constraint layout, you can read constraint layout article.

scroll view constraint layout example



ScrollView fillViewport Property

ScrollView occupies required space on the screen to display its child views; it doesn’t consider entire physical screen or view port when view hierarchy is smaller than the physical display. To change this behavior, you need to set fillViewport property of scrollview to true.

Below screen shows how scroll view displays when fillViewport set to true and false.

scroll view fillviewport example



HorizontalScrollView with ConstraintLayout Example

If you want to enable horizontal scrolling in your layout, you need to use HorizontalScrollView. Below layout shows how to use horizontalscrollview with constraintlayout. I first added two buttons to layout with required constraints using layout editor then added additional buttons with same type of constraint by editing layout xml.

horizontal scroll view constraint layout example


NestedScrollView Example

To enable scrolling of view hierarchy within the scrolling of parent view, you need to use NestedScrollView. Below example layout shows how to use NestedScrollView.

Below are parent scrolling and child view scrolling screens.

nestedscrollview parent scrolling examplenestedscrollview child scrolling example