Added in API level 1

ScrollView


open class ScrollView : FrameLayout
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.FrameLayout
   ↳ android.widget.ScrollView

A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.

Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead.

Never add a androidx.recyclerview.widget.RecyclerView or ListView to a scroll view. Doing so results in poor user interface performance and a poor user experience.

For vertical scrolling, consider androidx.core.widget.NestedScrollView instead of scroll view which offers greater user interface flexibility and support for the material design scrolling patterns.

Material Design offers guidelines on how the appearance of several UI components, including app bars and banners, should respond to gestures.

Summary

XML attributes
android:fillViewport Defines whether the scrollview should stretch its content to fill the viewport.
Inherited XML attributes
Inherited constants
Public constructors
ScrollView(context: Context!)

ScrollView(context: Context!, attrs: AttributeSet!)

ScrollView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

ScrollView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Unit
addView(child: View!)

open Unit
addView(child: View!, index: Int)

open Unit
addView(child: View!, params: ViewGroup.LayoutParams!)

open Unit
addView(child: View!, index: Int, params: ViewGroup.LayoutParams!)

open Boolean
arrowScroll(direction: Int)

Handle scrolling in response to an up or down arrow click.

open Unit

open Boolean

open Unit
draw(canvas: Canvas)

open Boolean

You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.

open Unit
fling(velocityY: Int)

Fling the scroll view

open Boolean
fullScroll(direction: Int)

Handles scrolling in response to a "home/end" shortcut press.

open CharSequence!

open Int

Returns the bottom edge effect color.

open Int

open Int

Returns the top edge effect color.

open Boolean

Indicates whether this ScrollView's content is stretched to fill the viewport.

open Boolean

open Boolean

open Boolean

open Boolean
onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean)

open Unit
onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int)

open Unit
onNestedScrollAccepted(child: View, target: View, axes: Int)

open Boolean
onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int)

open Unit

open Boolean

open Boolean
pageScroll(direction: Int)

Handles scrolling in response to a "page up/down" shortcut press.

open Unit
requestChildFocus(child: View!, focused: View!)

open Boolean
requestChildRectangleOnScreen(child: View, rectangle: Rect!, immediate: Boolean)

open Unit

open Unit

open Unit
scrollTo(x: Int, y: Int)

Set the scrolled position of your view.

open Unit

Scrolls the view to the given child.

open Unit

Sets the bottom edge effect color.

open Unit

Sets the edge effect color for both top and bottom edge effects.

open Unit
setFillViewport(fillViewport: Boolean)

Indicates this ScrollView whether it should stretch its content height to fill the viewport or not.

open Unit
setSmoothScrollingEnabled(smoothScrollingEnabled: Boolean)

Set whether arrow scrolling will animate its transition.

open Unit

Sets the top edge effect color.

open Boolean

Unit
smoothScrollBy(dx: Int, dy: Int)

Like View#scrollBy, but scroll smoothly instead of immediately.

Unit

Like scrollTo, but scroll smoothly instead of immediately.

Protected methods
open Int

Compute the amount to scroll in the Y direction in order to get a rectangle completely on the screen (or, if taller than the screen, at least the first screen size chunk of it).

open Int

open Int

The scroll range of a scroll view is the overall height of all of its children.

open Float

open Float

open Unit
measureChild(child: View!, parentWidthMeasureSpec: Int, parentHeightMeasureSpec: Int)

open Unit
measureChildWithMargins(child: View!, parentWidthMeasureSpec: Int, widthUsed: Int, parentHeightMeasureSpec: Int, heightUsed: Int)

open Unit

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Unit
onOverScrolled(scrollX: Int, scrollY: Int, clampedX: Boolean, clampedY: Boolean)

open Boolean
onRequestFocusInDescendants(direction: Int, previouslyFocusedRect: Rect!)

When looking for focus in children of a scroll view, need to be a little more careful not to give focus to something that is scrolled off screen.

open Unit

open Parcelable?

open Unit
onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)

Inherited functions
Inherited properties

XML attributes

android:fillViewport

android:fillViewport
Defines whether the scrollview should stretch its content to fill the viewport.

May be a boolean value, such as "true" or "false".

Public constructors

ScrollView

Added in API level 1
ScrollView(context: Context!)

ScrollView

Added in API level 1
ScrollView(
    context: Context!,
    attrs: AttributeSet!)

ScrollView

Added in API level 1
ScrollView(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

ScrollView

Added in API level 21
ScrollView(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

addView

Added in API level 1
open fun addView(child: View!): Unit
Parameters
child View!: the child view to add

addView

Added in API level 1
open fun addView(
    child: View!,
    index: Int
): Unit
Parameters
child View!: the child view to add
index Int: the position at which to add the child

addView

Added in API level 1
open fun addView(
    child: View!,
    params: ViewGroup.LayoutParams!
): Unit
Parameters
view The view to be added to this window.
params ViewGroup.LayoutParams!: the layout parameters to set on the child
child View!: the child view to add

addView

Added in API level 1
open fun addView(
    child: View!,
    index: Int,
    params: ViewGroup.LayoutParams!
): Unit
Parameters
child View!: the child view to add
index Int: the position at which to add the child or -1 to add last
params ViewGroup.LayoutParams!: the layout parameters to set on the child

arrowScroll

Added in API level 1
open fun arrowScroll(direction: Int): Boolean

Handle scrolling in response to an up or down arrow click.

Parameters
direction Int: The direction corresponding to the arrow key that was pressed
Return
Boolean True if we consumed the event, false otherwise

computeScroll

Added in API level 1
open fun computeScroll(): Unit

dispatchKeyEvent

Added in API level 1
open fun dispatchKeyEvent(event: KeyEvent!): Boolean
Parameters
event KeyEvent!: The key event to be dispatched.
Return
Boolean True if the event was handled, false otherwise.

draw

Added in API level 1
open fun draw(canvas: Canvas): Unit
Parameters
canvas Canvas: The Canvas to which the View is rendered. This value cannot be null.

executeKeyEvent

Added in API level 1
open fun executeKeyEvent(event: KeyEvent!): Boolean

You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.

Parameters
event KeyEvent!: The key event to execute.
Return
Boolean Return true if the event was handled, else false.

fling

Added in API level 1
open fun fling(velocityY: Int): Unit

Fling the scroll view

Parameters
velocityY Int: The initial velocity in the Y direction. Positive numbers mean that the finger/cursor is moving down the screen, which means we want to scroll towards the top.

fullScroll

Added in API level 1
open fun fullScroll(direction: Int): Boolean

Handles scrolling in response to a "home/end" shortcut press. This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.

Parameters
direction Int: the scroll direction: android.view.View#FOCUS_UP to go the top of the view or android.view.View#FOCUS_DOWN to go the bottom
Return
Boolean true if the key event is consumed by this method, false otherwise

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

getBottomEdgeEffectColor

Added in API level 29
open fun getBottomEdgeEffectColor(): Int

Returns the bottom edge effect color.

Return
Int The bottom edge effect color.

getMaxScrollAmount

Added in API level 1
open fun getMaxScrollAmount(): Int
Return
Int The maximum amount this scroll view will scroll in response to an arrow event.

getTopEdgeEffectColor

Added in API level 29
open fun getTopEdgeEffectColor(): Int

Returns the top edge effect color.

Return
Int The top edge effect color.

isFillViewport

Added in API level 1
open fun isFillViewport(): Boolean

Indicates whether this ScrollView's content is stretched to fill the viewport.

Return
Boolean True if the content fills the viewport, false otherwise.

isSmoothScrollingEnabled

Added in API level 1
open fun isSmoothScrollingEnabled(): Boolean
Return
Boolean Whether arrow scrolling will animate its transition.

onGenericMotionEvent

Added in API level 12
open fun onGenericMotionEvent(event: MotionEvent!): Boolean
Parameters
event MotionEvent!: The generic motion event being processed.
Return
Boolean True if the event was handled, false otherwise.

onInterceptTouchEvent

Added in API level 1
open fun onInterceptTouchEvent(ev: MotionEvent!): Boolean
Parameters
ev MotionEvent!: The motion event being dispatched down the hierarchy.
Return
Boolean Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

onNestedFling

Added in API level 21
open fun onNestedFling(
    target: View,
    velocityX: Float,
    velocityY: Float,
    consumed: Boolean
): Boolean
Parameters
target View: View that initiated the nested scroll This value cannot be null.
velocityX Float: Horizontal velocity in pixels per second
velocityY Float: Vertical velocity in pixels per second
consumed Boolean: true if the child consumed the fling, false otherwise
Return
Boolean true if this parent consumed or otherwise reacted to the fling

onNestedScroll

Added in API level 21
open fun onNestedScroll(
    target: View,
    dxConsumed: Int,
    dyConsumed: Int,
    dxUnconsumed: Int,
    dyUnconsumed: Int
): Unit
Parameters
target View: The descendent view controlling the nested scroll This value cannot be null.
dxConsumed Int: Horizontal scroll distance in pixels already consumed by target
dyConsumed Int: Vertical scroll distance in pixels already consumed by target
dxUnconsumed Int: Horizontal scroll distance in pixels not consumed by target
dyUnconsumed Int: Vertical scroll distance in pixels not consumed by target

onNestedScrollAccepted

Added in API level 21
open fun onNestedScrollAccepted(
    child: View,
    target: View,
    axes: Int
): Unit
Parameters
child View: Direct child of this ViewParent containing target This value cannot be null.
target View: View that initiated the nested scroll This value cannot be null.
nestedScrollAxes Flags consisting of View#SCROLL_AXIS_HORIZONTAL, View#SCROLL_AXIS_VERTICAL or both

onStartNestedScroll

Added in API level 21
open fun onStartNestedScroll(
    child: View,
    target: View,
    nestedScrollAxes: Int
): Boolean
Parameters
child View: Direct child of this ViewParent containing target This value cannot be null.
target View: View that initiated the nested scroll This value cannot be null.
nestedScrollAxes Int: Flags consisting of View#SCROLL_AXIS_HORIZONTAL, View#SCROLL_AXIS_VERTICAL or both
Return
Boolean true if this ViewParent accepts the nested scroll operation

onStopNestedScroll

Added in API level 21
open fun onStopNestedScroll(target: View): Unit
Parameters
target View: View that initiated the nested scroll This value cannot be null.

onTouchEvent

Added in API level 1
open fun onTouchEvent(ev: MotionEvent!): Boolean
Parameters
event The motion event.
Return
Boolean True if the event was handled, false otherwise.

pageScroll

Added in API level 1
open fun pageScroll(direction: Int): Boolean

Handles scrolling in response to a "page up/down" shortcut press. This method will scroll the view by one page up or down and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.

Parameters
direction Int: the scroll direction: android.view.View#FOCUS_UP to go one page up or android.view.View#FOCUS_DOWN to go one page down
Return
Boolean true if the key event is consumed by this method, false otherwise

requestChildFocus

Added in API level 1
open fun requestChildFocus(
    child: View!,
    focused: View!
): Unit
Parameters
child View!: The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus.
focused View!: The view that is a descendant of child that actually has focus

requestChildRectangleOnScreen

Added in API level 1
open fun requestChildRectangleOnScreen(
    child: View,
    rectangle: Rect!,
    immediate: Boolean
): Boolean
Parameters
child View: The direct child making the request. This value cannot be null.
rectangle Rect!: The rectangle in the child's coordinates the child wishes to be on the screen.
immediate Boolean: True to forbid animated or delayed scrolling, false otherwise
Return
Boolean Whether the group scrolled to handle the operation

requestDisallowInterceptTouchEvent

Added in API level 1
open fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean): Unit
Parameters
disallowIntercept Boolean: True if the child does not want the parent to intercept touch events.

requestLayout

Added in API level 1
open fun requestLayout(): Unit

scrollTo

Added in API level 1
open fun scrollTo(
    x: Int,
    y: Int
): Unit

Set the scrolled position of your view. This will cause a call to onScrollChanged(int,int,int,int) and the view will be invalidated.

This version also clamps the scrolling to the bounds of our child.

Parameters
x Int: the x position to scroll to
y Int: the y position to scroll to

scrollToDescendant

Added in API level 29
open fun scrollToDescendant(child: View): Unit

Scrolls the view to the given child.

Parameters
child View: the View to scroll to This value cannot be null.

setBottomEdgeEffectColor

Added in API level 29
open fun setBottomEdgeEffectColor(color: Int): Unit

Sets the bottom edge effect color.

Parameters
color Int: The color for the bottom edge effect.

setEdgeEffectColor

Added in API level 29
open fun setEdgeEffectColor(color: Int): Unit

Sets the edge effect color for both top and bottom edge effects.

Parameters
color Int: The color for the edge effects.

setFillViewport

Added in API level 1
open fun setFillViewport(fillViewport: Boolean): Unit

Indicates this ScrollView whether it should stretch its content height to fill the viewport or not.

Parameters
fillViewport Boolean: True to stretch the content's height to the viewport's boundaries, false otherwise.

setSmoothScrollingEnabled

Added in API level 1
open fun setSmoothScrollingEnabled(smoothScrollingEnabled: Boolean): Unit

Set whether arrow scrolling will animate its transition.

Parameters
smoothScrollingEnabled Boolean: whether arrow scrolling will animate its transition

setTopEdgeEffectColor

Added in API level 29
open fun setTopEdgeEffectColor(color: Int): Unit

Sets the top edge effect color.

Parameters
color Int: The color for the top edge effect.

shouldDelayChildPressedState

Added in API level 14
open fun shouldDelayChildPressedState(): Boolean

smoothScrollBy

Added in API level 1
fun smoothScrollBy(
    dx: Int,
    dy: Int
): Unit

Like View#scrollBy, but scroll smoothly instead of immediately.

Parameters
dx Int: the number of pixels to scroll by on the X axis
dy Int: the number of pixels to scroll by on the Y axis

smoothScrollTo

Added in API level 1
fun smoothScrollTo(
    x: Int,
    y: Int
): Unit

Like scrollTo, but scroll smoothly instead of immediately.

Parameters
x Int: the position where to scroll on the X axis
y Int: the position where to scroll on the Y axis

Protected methods

computeScrollDeltaToGetChildRectOnScreen

Added in API level 1
protected open fun computeScrollDeltaToGetChildRectOnScreen(rect: Rect!): Int

Compute the amount to scroll in the Y direction in order to get a rectangle completely on the screen (or, if taller than the screen, at least the first screen size chunk of it).

Parameters
rect Rect!: The rect.
Return
Int The scroll delta.

computeVerticalScrollOffset

Added in API level 1
protected open fun computeVerticalScrollOffset(): Int
Return
Int the vertical offset of the scrollbar's thumb

computeVerticalScrollRange

Added in API level 1
protected open fun computeVerticalScrollRange(): Int

The scroll range of a scroll view is the overall height of all of its children.

Return
Int the total vertical range represented by the vertical scrollbar

The default range is the drawing height of this view.

getBottomFadingEdgeStrength

Added in API level 1
protected open fun getBottomFadingEdgeStrength(): Float
Return
Float the intensity of the bottom fade as a float between 0.0f and 1.0f

getTopFadingEdgeStrength

Added in API level 1
protected open fun getTopFadingEdgeStrength(): Float
Return
Float the intensity of the top fade as a float between 0.0f and 1.0f

measureChild

Added in API level 1
protected open fun measureChild(
    child: View!,
    parentWidthMeasureSpec: Int,
    parentHeightMeasureSpec: Int
): Unit
Parameters
child View!: The child to measure
parentWidthMeasureSpec Int: The width requirements for this view
parentHeightMeasureSpec Int: The height requirements for this view

measureChildWithMargins

Added in API level 1
protected open fun measureChildWithMargins(
    child: View!,
    parentWidthMeasureSpec: Int,
    widthUsed: Int,
    parentHeightMeasureSpec: Int,
    heightUsed: Int
): Unit
Parameters
child View!: The child to measure
parentWidthMeasureSpec Int: The width requirements for this view
widthUsed Int: Extra space that has been used up by the parent horizontally (possibly by other children of the parent)
parentHeightMeasureSpec Int: The height requirements for this view
heightUsed Int: Extra space that has been used up by the parent vertically (possibly by other children of the parent)

onDetachedFromWindow

Added in API level 1
protected open fun onDetachedFromWindow(): Unit

onLayout

Added in API level 1
protected open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit
Parameters
changed Boolean: This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent

onMeasure

Added in API level 1
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.

onOverScrolled

Added in API level 9
protected open fun onOverScrolled(
    scrollX: Int,
    scrollY: Int,
    clampedX: Boolean,
    clampedY: Boolean
): Unit
Parameters
scrollX Int: New X scroll value in pixels
scrollY Int: New Y scroll value in pixels
clampedX Boolean: True if scrollX was clamped to an over-scroll boundary
clampedY Boolean: True if scrollY was clamped to an over-scroll boundary

onRequestFocusInDescendants

Added in API level 1
protected open fun onRequestFocusInDescendants(
    direction: Int,
    previouslyFocusedRect: Rect!
): Boolean

When looking for focus in children of a scroll view, need to be a little more careful not to give focus to something that is scrolled off screen. This is more expensive than the default android.view.ViewGroup implementation, otherwise this behavior might have been made the default.

Parameters
direction Int: One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
previouslyFocusedRect Rect!: The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.
Return
Boolean Whether focus was taken.

onRestoreInstanceState

Added in API level 1
protected open fun onRestoreInstanceState(state: Parcelable!): Unit
Parameters
state Parcelable!: The frozen state that had previously been returned by onSaveInstanceState.

onSaveInstanceState

Added in API level 1
protected open fun onSaveInstanceState(): Parcelable?
Return
Parcelable? Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save.

onSizeChanged

Added in API level 1
protected open fun onSizeChanged(
    w: Int,
    h: Int,
    oldw: Int,
    oldh: Int
): Unit
Parameters
w Int: Current width of this view.
h Int: Current height of this view.
oldw Int: Old width of this view.
oldh Int: Old height of this view.