public class

HorizontalScrollView

extends FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.HorizontalScrollView

Class Overview

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A HorizontalScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a horizontal orientation, presenting a horizontal array of top-level items that the user can scroll through.

You should never use a HorizontalScrollView with a ListView, since ListView takes care of its own scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by HorizontalScrollView.

The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

HorizontalScrollView only supports horizontal scrolling.

Summary

XML Attributes
Attribute Name Related Method Description
android:fillViewport setFillViewport(boolean) Defines whether the scrollview should stretch its content to fill the viewport. 
[Expand]
Inherited XML Attributes
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
HorizontalScrollView(Context context)
HorizontalScrollView(Context context, AttributeSet attrs)
HorizontalScrollView(Context context, AttributeSet attrs, int defStyle)
Public Methods
void addView(View child)
Adds a child view.
void addView(View child, int index)
Adds a child view.
void addView(View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
void addView(View child, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
boolean arrowScroll(int direction)
Handle scrolling in response to a left or right arrow click.
void computeScroll()
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.
boolean dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
void draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
boolean executeKeyEvent(KeyEvent event)
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.
void fling(int velocityX)
Fling the scroll view
boolean fullScroll(int direction)

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

int getMaxScrollAmount()
boolean isFillViewport()
Indicates whether this HorizontalScrollView's content is stretched to fill the viewport.
boolean isSmoothScrollingEnabled()
boolean onGenericMotionEvent(MotionEvent event)
Implement this method to handle generic motion events.
void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
boolean onInterceptTouchEvent(MotionEvent ev)
Implement this method to intercept all touch screen motion events.
boolean onTouchEvent(MotionEvent ev)
Implement this method to handle touch screen motion events.
boolean pageScroll(int direction)

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

void requestChildFocus(View child, View focused)
Called when a child of this parent wants focus
boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)
Called when a child of this group wants a particular rectangle to be positioned onto the screen.
void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).
void requestLayout()
Call this when something has changed which has invalidated the layout of this view.
void scrollTo(int x, int y)
Set the scrolled position of your view.

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

void setFillViewport(boolean fillViewport)
Indicates this HorizontalScrollView whether it should stretch its content width to fill the viewport or not.
void setOverScrollMode(int mode)
Set the over-scroll mode for this view.
void setSmoothScrollingEnabled(boolean smoothScrollingEnabled)
Set whether arrow scrolling will animate its transition.
boolean shouldDelayChildPressedState()
Return true if the pressed state should be delayed for children or descendants of this ViewGroup.
final void smoothScrollBy(int dx, int dy)
Like scrollBy(int, int), but scroll smoothly instead of immediately.
final void smoothScrollTo(int x, int y)
Like scrollTo(int, int), but scroll smoothly instead of immediately.
Protected Methods
int computeHorizontalScrollOffset()

Compute the horizontal offset of the horizontal scrollbar's thumb within the horizontal range.

int computeHorizontalScrollRange()

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

int computeScrollDeltaToGetChildRectOnScreen(Rect rect)
Compute the amount to scroll in the X 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).
float getLeftFadingEdgeStrength()
Returns the strength, or intensity, of the left faded edge.
float getRightFadingEdgeStrength()
Returns the strength, or intensity, of the right faded edge.
void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding.
void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)
Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding and margins.
void onLayout(boolean changed, int l, int t, int r, int b)
Called from layout when this view should assign a size and position to each of its children.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
Called by overScrollBy(int, int, int, int, int, int, int, int, boolean) to respond to the results of an over-scroll operation.
boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
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.
void onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
[Expand]
Inherited Methods
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:fillViewport

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

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol fillViewport.

Related Methods

Public Constructors

public HorizontalScrollView (Context context)

Since: API Level 3

public HorizontalScrollView (Context context, AttributeSet attrs)

Since: API Level 3

public HorizontalScrollView (Context context, AttributeSet attrs, int defStyle)

Since: API Level 3

Public Methods

public void addView (View child)

Since: API Level 3

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Parameters
child the child view to add

public void addView (View child, int index)

Since: API Level 3

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Parameters
child the child view to add
index the position at which to add the child

public void addView (View child, int index, ViewGroup.LayoutParams params)

Since: API Level 3

Adds a child view with the specified layout parameters.

Parameters
child the child view to add
index the position at which to add the child
params the layout parameters to set on the child

public void addView (View child, ViewGroup.LayoutParams params)

Since: API Level 3

Adds a child view with the specified layout parameters.

Parameters
child the child view to add
params the layout parameters to set on the child

public boolean arrowScroll (int direction)

Since: API Level 3

Handle scrolling in response to a left or right arrow click.

Parameters
direction The direction corresponding to the arrow key that was pressed