Android APIs
public class

CollapsingToolbarLayout

extends FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.design.widget.CollapsingToolbarLayout

Class Overview

CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout. CollapsingToolbarLayout contains the following features:

Collapsing title

A title which is larger when the layout is fully visible but collapses and becomes smaller as the layout is scrolled off screen. You can set the title to display via setTitle(CharSequence). The title appearance can be tweaked via the collapsedTextAppearance and expandedTextAppearance attributes.

Content scrim

A full-bleed scrim which is show or hidden when the scroll position has hit a certain threshold. You can change this via setContentScrim(Drawable).

Status bar scrim

A scrim which is show or hidden behind the status bar when the scroll position has hit a certain threshold. You can change this via setStatusBarScrim(Drawable). This only works on LOLLIPOP devices when we set to fit system windows.

Parallax scrolling children

Child views can opt to be scrolled within this layout in a parallax fashion. See COLLAPSE_MODE_PARALLAX and setParallaxMultiplier(float).

Pinned position children

Child views can opt to be pinned in space globally. This is useful when implementing a collapsing as it allows the Toolbar to be fixed in place even though this layout is moving. See COLLAPSE_MODE_PIN.

Summary

Nested Classes
class CollapsingToolbarLayout.LayoutParams  
XML Attributes
Attribute Name Related Method Description
android.support.design:collapsedTitleGravity setCollapsedTitleGravity(int) Specifies how the title should be positioned when collapsed. 
android.support.design:collapsedTitleTextAppearance setCollapsedTitleTextAppearance(int) The text appearance of the CollapsingToolbarLayouts title when it is fully 'collapsed'

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name". 

android.support.design:contentScrim setContentScrimResource(int) The drawable to use as a scrim on top of the CollapsingToolbarLayouts content when it has been scrolled sufficiently off screen. 
android.support.design:expandedTitleGravity setExpandedTitleGravity(int) Specifies how the title should be positioned when expanded. 
android.support.design:expandedTitleMargin Specifies extra space on the start, top, end and bottom sides of the the expanded title text. 
android.support.design:expandedTitleMarginBottom Specifies extra space on the bottom side of the the expanded title text. 
android.support.design:expandedTitleMarginEnd Specifies extra space on the end side of the the expanded title text. 
android.support.design:expandedTitleMarginStart Specifies extra space on the start side of the the expanded title text. 
android.support.design:expandedTitleTextAppearance setExpandedTitleTextAppearance(int) The text appearance of the CollapsingToolbarLayout's title when it is fully 'expanded'

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name". 

android.support.design:statusBarScrim setStatusBarScrimResource(int) The drawable to use as a scrim for the status bar content when the CollapsingToolbarLayout has been scrolled sufficiently off screen. 
android.support.design:title setTitle(CharSequence) The title to show when titleEnabled is set to true. 
android.support.design:titleEnabled setTitleEnabled(boolean) Whether the CollapsingToolbarLayout should draw its own shrinking/growing title. 
android.support.design:toolbarId The id of the primary Toolbar child that you wish to use for the purpose of collapsing. 
[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
CollapsingToolbarLayout(Context context)
CollapsingToolbarLayout(Context context, AttributeSet attrs)
CollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr)
Public Methods
void draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
FrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
int getCollapsedTitleGravity()
Returns the horizontal and vertical alignment for title when collapsed.
Drawable getContentScrim()
Returns the drawable which is used for the foreground scrim.
int getExpandedTitleGravity()
Returns the horizontal and vertical alignment for title when expanded.
Drawable getStatusBarScrim()
Returns the drawable which is used for the status bar scrim.
CharSequence getTitle()
Returns the title currently being displayed by this view.
boolean isTitleEnabled()
Returns whether this view is currently displaying its own title.
void setCollapsedTitleGravity(int gravity)
Sets the horizontal alignment of the collapsed title and the vertical gravity that will be used when there is extra space in the collapsed bounds beyond what is required for the title itself.
void setCollapsedTitleTextAppearance(int resId)
Sets the text color and size for the collapsed title from the specified TextAppearance resource.
void setCollapsedTitleTextColor(int color)
Sets the text color of the collapsed title.
void setContentScrim(Drawable drawable)
Set the drawable to use for the content scrim from resources.
void setContentScrimColor(int color)
Set the color to use for the content scrim.
void setContentScrimResource(int resId)
Set the drawable to use for the content scrim from resources.
void setExpandedTitleColor(int color)
Sets the text color of the expanded title.
void setExpandedTitleGravity(int gravity)
Sets the horizontal alignment of the expanded title and the vertical gravity that will be used when there is extra space in the expanded bounds beyond what is required for the title itself.
void setExpandedTitleTextAppearance(int resId)
Sets the text color and size for the expanded title from the specified TextAppearance resource.
void setStatusBarScrim(Drawable drawable)
Set the drawable to use for the status bar scrim from resources.
void setStatusBarScrimColor(int color)
Set the color to use for the status bar scrim.
void setStatusBarScrimResource(int resId)
Set the drawable to use for the content scrim from resources.
void setTitle(CharSequence title)
Sets the title to be displayed by this view, if enabled.
void setTitleEnabled(boolean enabled)
Sets whether this view should display its own title.
Protected Methods
boolean checkLayoutParams(ViewGroup.LayoutParams p)
boolean drawChild(Canvas canvas, View child, long drawingTime)
Draw one child of this View Group.
CollapsingToolbarLayout.LayoutParams generateDefaultLayoutParams()
Returns a set of layout parameters with a width of MATCH_PARENT, and a height of MATCH_PARENT.
FrameLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
Returns a safe set of layout parameters based on the supplied layout params.
void onAttachedToWindow()
This is called when the view is attached to a window.
void onDetachedFromWindow()
This is called when the view is detached from a window.
void onLayout(boolean changed, int left, int top, int right, int bottom)
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 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.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android.support.design:collapsedTitleGravity

Specifies how the title should be positioned when collapsed.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
top0x30 Push title to the top of its container, not changing its size.
bottom0x50 Push title to the bottom of its container, not changing its size.
left0x03 Push title to the left of its container, not changing its size.
right0x05 Push title to the right of its container, not changing its size.
center_vertical0x10 Place title in the vertical center of its container, not changing its size.
fill_vertical0x70 Grow the vertical size of the title if needed so it completely fills its container.
center_horizontal0x01 Place title in the horizontal center of its container, not changing its size.
center0x11 Place the title in the center of its container in both the vertical and horizontal axis, not changing its size.
start0x00800003 Push title to the beginning of its container, not changing its size.
end0x00800005 Push title to the end of its container, not changing its size.

This is a private symbol.

android.support.design:collapsedTitleTextAppearance

The text appearance of the CollapsingToolbarLayouts title when it is fully 'collapsed'

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This is a private symbol.

android.support.design:contentScrim

The drawable to use as a scrim on top of the CollapsingToolbarLayouts content when it has been scrolled sufficiently off screen.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

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 is a private symbol.

android.support.design:expandedTitleGravity

Specifies how the title should be positioned when expanded.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
top0x30 Push title to the top of its container, not changing its size.
bottom0x50 Push title to the bottom of its container, not changing its size.
left0x03 Push title to the left of its container, not changing its size.
right0x05 Push title to the right of its container, not changing its size.
center_vertical0x10 Place title in the vertical center of its container, not changing its size.
fill_vertical0x70 Grow the vertical size of the title if needed so it completely fills its container.
center_horizontal0x01 Place title in the horizontal center of its container, not changing its size.
center0x11 Place the title in the center of its container in both the vertical and horizontal axis, not changing its size.
start0x00800003 Push title to the beginning of its container, not changing its size.
end0x00800005 Push title to the end of its container, not changing its size.

This is a private symbol.

android.support.design:expandedTitleMargin

Specifies extra space on the start, top, end and bottom sides of the the expanded title text. Margin values should be positive.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

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 is a private symbol.

Related Methods

android.support.design:expandedTitleMarginBottom

Specifies extra space on the bottom side of the the expanded title text. Margin values should be positive.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

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 is a private symbol.

Related Methods

android.support.design:expandedTitleMarginEnd

Specifies extra space on the end side of the the expanded title text. Margin values should be positive.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

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 is a private symbol.

Related Methods

android.support.design:expandedTitleMarginStart

Specifies extra space on the start side of the the expanded title text. Margin values should be positive.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

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 is a private symbol.

Related Methods

android.support.design:expandedTitleTextAppearance

The text appearance of the CollapsingToolbarLayout's title when it is fully 'expanded'

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This is a private symbol.

android.support.design:statusBarScrim

The drawable to use as a scrim for the status bar content when the CollapsingToolbarLayout has been scrolled sufficiently off screen. Only works on Lollipop with the correct setup.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

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 is a private symbol.

android.support.design:title

The title to show when titleEnabled is set to true.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

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 is a private symbol.

Related Methods

android.support.design:titleEnabled

Whether the CollapsingToolbarLayout should draw its own shrinking/growing title.

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 is a private symbol.

Related Methods

android.support.design:toolbarId

The id of the primary Toolbar child that you wish to use for the purpose of collapsing. If you do not set this then the first Toolbar child found will be used.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This is a private symbol.

Related Methods

Public Constructors

public CollapsingToolbarLayout (Context context)

public CollapsingToolbarLayout (Context context, AttributeSet attrs)

public CollapsingToolbarLayout (Context context, AttributeSet attrs, int defStyleAttr)

Public Methods

public void draw (Canvas canvas)

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
canvas The Canvas to which the View is rendered.

public FrameLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs the attributes to build the layout parameters from
Returns

public int getCollapsedTitleGravity ()

Returns the horizontal and vertical alignment for title when collapsed.

public Drawable getContentScrim ()

Returns the drawable which is used for the foreground scrim.

Related XML Attributes

public int getExpandedTitleGravity ()

Returns the horizontal and vertical alignment for title when expanded.

public Drawable getStatusBarScrim ()

Returns the drawable which is used for the status bar scrim.

Related XML Attributes

public CharSequence getTitle ()

Returns the title currently being displayed by this view. If the title is not enabled, then this will return null.

Related XML Attributes

public boolean isTitleEnabled ()

Returns whether this view is currently displaying its own title.

Related XML Attributes

public void setCollapsedTitleGravity (int gravity)

Sets the horizontal alignment of the collapsed title and the vertical gravity that will be used when there is extra space in the collapsed bounds beyond what is required for the title itself.

public void setCollapsedTitleTextAppearance (int resId)

Sets the text color and size for the collapsed title from the specified TextAppearance resource.

public void setCollapsedTitleTextColor (int color)

Sets the text color of the collapsed title.

Parameters
color The new text color in ARGB format

public void setContentScrim (Drawable drawable)

Set the drawable to use for the content scrim from resources. Providing null will disable the scrim functionality.

Related XML Attributes
Parameters
drawable the drawable to display

public void setContentScrimColor (int color)

Set the color to use for the content scrim.

Related XML Attributes
Parameters
color the color to display

public void setContentScrimResource (int resId)

Set the drawable to use for the content scrim from resources.

Related XML Attributes
Parameters
resId drawable resource id

public void setExpandedTitleColor (int color)

Sets the text color of the expanded title.

Parameters
color The new text color in ARGB format

public void setExpandedTitleGravity (int gravity)

Sets the horizontal alignment of the expanded title and the vertical gravity that will be used when there is extra space in the expanded bounds beyond what is required for the title itself.

public void setExpandedTitleTextAppearance (int resId)

Sets the text color and size for the expanded title from the specified TextAppearance resource.

public void setStatusBarScrim (Drawable drawable)

Set the drawable to use for the status bar scrim from resources. Providing null will disable the scrim functionality.

This scrim is only shown when we have been given a top system inset.

Related XML Attributes
Parameters
drawable the drawable to display

public void setStatusBarScrimColor (int color)

Set the color to use for the status bar scrim.

This scrim is only shown when we have been given a top system inset.

Related XML Attributes
Parameters
color the color to display

public void setStatusBarScrimResource (int resId)

Set the drawable to use for the content scrim from resources.

Related XML Attributes
Parameters
resId drawable resource id

public void setTitle (CharSequence title)

Sets the title to be displayed by this view, if enabled.

Related XML Attributes

public void setTitleEnabled (boolean enabled)

Sets whether this view should display its own title.

The title displayed by this view will shrink and grow based on the scroll offset.

Related XML Attributes

Protected Methods

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

protected boolean drawChild (Canvas canvas, View child, long drawingTime)

Draw one child of this View Group. This method is responsible for getting the canvas in the right state. This includes clipping, translating so that the child's scrolled origin is at 0, 0, and applying any animation transformations.

Parameters
canvas The canvas on which to draw the child
child Who to draw
drawingTime The time at which draw is occurring
Returns
  • True if an invalidate() was issued

protected CollapsingToolbarLayout.LayoutParams generateDefaultLayoutParams ()

Returns a set of layout parameters with a width of MATCH_PARENT, and a height of MATCH_PARENT.

Returns
  • a set of default layout parameters or null

protected FrameLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters
p The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns

protected void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

protected void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

protected void onLayout (boolean changed, int left, int top, int right, int bottom)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed 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

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

protected void onSizeChanged (int w, int h, int oldw, int oldh)

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters
w Current width of this view.
h Current height of this view.
oldw Old width of this view.
oldh Old height of this view.