Skip to content

Most visited

Recently visited

navigation
added in version 24.1.0
belongs to Maven artifact com.android.support:design:27.0.0

CollapsingToolbarLayout

public class CollapsingToolbarLayout
extends FrameLayout

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


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.

Do not manually add views to the Toolbar at run time. We will add a 'dummy view' to the Toolbar which allows us to work out the available space for the title. This can interfere with any views which you add.

Summary

Nested classes

class CollapsingToolbarLayout.LayoutParams

 

XML attributes

CollapsingToolbarLayout_collapsedTitleGravity  
CollapsingToolbarLayout_collapsedTitleTextAppearance  
CollapsingToolbarLayout_contentScrim  
CollapsingToolbarLayout_expandedTitleGravity  
CollapsingToolbarLayout_expandedTitleMargin  
CollapsingToolbarLayout_expandedTitleMarginBottom  
CollapsingToolbarLayout_expandedTitleMarginEnd  
CollapsingToolbarLayout_expandedTitleMarginStart  
CollapsingToolbarLayout_expandedTitleMarginTop  
CollapsingToolbarLayout_expandedTitleTextAppearance  
CollapsingToolbarLayout_scrimAnimationDuration  
CollapsingToolbarLayout_scrimVisibleHeightTrigger  
CollapsingToolbarLayout_statusBarScrim  
CollapsingToolbarLayout_title  
CollapsingToolbarLayout_titleEnabled  
CollapsingToolbarLayout_toolbarId  

Inherited constants

From class android.view.ViewGroup
From class android.view.View

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)
FrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
int getCollapsedTitleGravity()

Returns the horizontal and vertical alignment for title when collapsed.

Typeface getCollapsedTitleTypeface()

Returns the typeface used for the collapsed title.

Drawable getContentScrim()

Returns the drawable which is used for the foreground scrim.

int getExpandedTitleGravity()

Returns the horizontal and vertical alignment for title when expanded.

int getExpandedTitleMarginBottom()
int getExpandedTitleMarginEnd()
int getExpandedTitleMarginStart()
int getExpandedTitleMarginTop()
Typeface getExpandedTitleTypeface()

Returns the typeface used for the expanded title.

long getScrimAnimationDuration()

Returns the duration in milliseconds used for scrim visibility animations.

int getScrimVisibleHeightTrigger()

Returns the amount of visible height in pixels used to define when to trigger a scrim visibility change.

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(ColorStateList colors)

Sets the text colors of the collapsed title.

void setCollapsedTitleTextColor(int color)

Sets the text color of the collapsed title.

void setCollapsedTitleTypeface(Typeface typeface)

Set the typeface to use for 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 setExpandedTitleMargin(int start, int top, int end, int bottom)

Sets the expanded title margins.

void setExpandedTitleMarginBottom(int margin)

Sets the bottom expanded title margin in pixels.

void setExpandedTitleMarginEnd(int margin)

Sets the ending expanded title margin in pixels.

void setExpandedTitleMarginStart(int margin)

Sets the starting expanded title margin in pixels.

void setExpandedTitleMarginTop(int margin)

Sets the top expanded title margin in pixels.

void setExpandedTitleTextAppearance(int resId)

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

void setExpandedTitleTextColor(ColorStateList colors)

Sets the text colors of the expanded title.

void setExpandedTitleTypeface(Typeface typeface)

Set the typeface to use for the expanded title.

void setScrimAnimationDuration(long duration)

Set the duration used for scrim visibility animations.

void setScrimVisibleHeightTrigger(int height)

Set the amount of visible height in pixels used to define when to trigger a scrim visibility change.

void setScrimsShown(boolean shown, boolean animate)

Set whether the content scrim and/or status bar scrim should be shown or not.

void setScrimsShown(boolean shown)

Set whether the content scrim and/or status bar scrim should be shown or not.

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.

void setVisibility(int visibility)

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
boolean drawChild(Canvas canvas, View child, long drawingTime)
void drawableStateChanged()
CollapsingToolbarLayout.LayoutParams generateDefaultLayoutParams()
FrameLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
void onAttachedToWindow()
void onDetachedFromWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onSizeChanged(int w, int h, int oldw, int oldh)
boolean verifyDrawable(Drawable who)

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

CollapsingToolbarLayout_collapsedTitleGravity

Related methods:

CollapsingToolbarLayout_collapsedTitleTextAppearance

Related methods:

CollapsingToolbarLayout_contentScrim

Related methods:

CollapsingToolbarLayout_expandedTitleGravity

Related methods:

CollapsingToolbarLayout_expandedTitleMargin

Related methods:

CollapsingToolbarLayout_expandedTitleMarginBottom

Related methods:

CollapsingToolbarLayout_expandedTitleMarginEnd

Related methods:

CollapsingToolbarLayout_expandedTitleMarginStart

Related methods:

CollapsingToolbarLayout_expandedTitleMarginTop

Related methods:

CollapsingToolbarLayout_expandedTitleTextAppearance

Related methods:

CollapsingToolbarLayout_scrimAnimationDuration

Related methods:

CollapsingToolbarLayout_scrimVisibleHeightTrigger

Related methods:

CollapsingToolbarLayout_statusBarScrim

Related methods:

CollapsingToolbarLayout_title

Related methods:

CollapsingToolbarLayout_titleEnabled

Related methods:

CollapsingToolbarLayout_toolbarId

Public constructors

CollapsingToolbarLayout

added in version 24.1.0
CollapsingToolbarLayout (Context context)

Parameters
context Context

CollapsingToolbarLayout

added in version 24.1.0
CollapsingToolbarLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

CollapsingToolbarLayout

added in version 24.1.0
CollapsingToolbarLayout (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

draw

void draw (Canvas canvas)

Parameters
canvas Canvas

generateLayoutParams

added in version 26.1.0
FrameLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
FrameLayout.LayoutParams

getCollapsedTitleGravity

added in version 24.1.0
int getCollapsedTitleGravity ()

Returns the horizontal and vertical alignment for title when collapsed.

Related XML Attributes:

Returns
int

getCollapsedTitleTypeface

added in version 24.1.0
Typeface getCollapsedTitleTypeface ()

Returns the typeface used for the collapsed title.

Returns
Typeface

getContentScrim

added in version 24.1.0
Drawable getContentScrim ()

Returns the drawable which is used for the foreground scrim.

Related XML Attributes:

Returns
Drawable

See also:

getExpandedTitleGravity

added in version 24.1.0
int getExpandedTitleGravity ()

Returns the horizontal and vertical alignment for title when expanded.

Related XML Attributes:

Returns
int

getExpandedTitleMarginBottom

added in version 24.1.0
int getExpandedTitleMarginBottom ()

Related XML Attributes:

Returns
int the bottom expanded title margin in pixels

See also:

getExpandedTitleMarginEnd

added in version 24.1.0
int getExpandedTitleMarginEnd ()

Related XML Attributes:

Returns
int the ending expanded title margin in pixels

See also:

getExpandedTitleMarginStart

added in version 24.1.0
int getExpandedTitleMarginStart ()

Related XML Attributes:

Returns
int the starting expanded title margin in pixels

See also:

getExpandedTitleMarginTop

added in version 24.1.0
int getExpandedTitleMarginTop ()

Related XML Attributes:

Returns
int the top expanded title margin in pixels

See also:

getExpandedTitleTypeface

added in version 24.1.0
Typeface getExpandedTitleTypeface ()

Returns the typeface used for the expanded title.

Returns
Typeface

getScrimAnimationDuration

added in version 24.1.0
long getScrimAnimationDuration ()

Returns the duration in milliseconds used for scrim visibility animations.

Returns
long

getScrimVisibleHeightTrigger

added in version 24.1.0
int getScrimVisibleHeightTrigger ()

Returns the amount of visible height in pixels used to define when to trigger a scrim visibility change.

Returns
int

See also:

getStatusBarScrim

added in version 24.1.0
Drawable getStatusBarScrim ()

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

Related XML Attributes:

Returns
Drawable

See also:

getTitle

added in version 24.1.0
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:

Returns
CharSequence

isTitleEnabled

added in version 24.1.0
boolean isTitleEnabled ()

Returns whether this view is currently displaying its own title.

Related XML Attributes:

Returns
boolean

See also:

setCollapsedTitleGravity

added in version 24.1.0
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.

Related XML Attributes:

Parameters
gravity int

setCollapsedTitleTextAppearance

added in version 24.1.0
void setCollapsedTitleTextAppearance (int resId)

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

Related XML Attributes:

Parameters
resId int

setCollapsedTitleTextColor

added in version 25.1.0
void setCollapsedTitleTextColor (ColorStateList colors)

Sets the text colors of the collapsed title.

Parameters
colors ColorStateList: ColorStateList containing the new text colors

setCollapsedTitleTextColor

added in version 24.1.0
void setCollapsedTitleTextColor (int color)

Sets the text color of the collapsed title.

Parameters
color int: The new text color in ARGB format

setCollapsedTitleTypeface

added in version 24.1.0
void setCollapsedTitleTypeface (Typeface typeface)

Set the typeface to use for the collapsed title.

Parameters
typeface Typeface: typeface to use, or null to use the default.

setContentScrim

added in version 24.1.0
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 Drawable: the drawable to display

See also:

setContentScrimColor

added in version 24.1.0
void setContentScrimColor (int color)

Set the color to use for the content scrim.

Related XML Attributes:

Parameters
color int: the color to display

See also:

setContentScrimResource

added in version 24.1.0
void setContentScrimResource (int resId)

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

Related XML Attributes:

Parameters
resId int: drawable resource id

See also:

setExpandedTitleColor

added in version 24.1.0
void setExpandedTitleColor (int color)

Sets the text color of the expanded title.

Parameters
color int: The new text color in ARGB format

setExpandedTitleGravity

added in version 24.1.0
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.

Related XML Attributes:

Parameters
gravity int

setExpandedTitleMargin

added in version 24.1.0
void setExpandedTitleMargin (int start, 
                int top, 
                int end, 
                int bottom)

Sets the expanded title margins.

Related XML Attributes:

Parameters
start int: the starting title margin in pixels

top int: the top title margin in pixels

end int: the ending title margin in pixels

bottom int: the bottom title margin in pixels

See also:

setExpandedTitleMarginBottom

added in version 24.1.0
void setExpandedTitleMarginBottom (int margin)

Sets the bottom expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the bottom title margin in pixels

See also:

setExpandedTitleMarginEnd

added in version 24.1.0
void setExpandedTitleMarginEnd (int margin)

Sets the ending expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the ending title margin in pixels

See also:

setExpandedTitleMarginStart

added in version 24.1.0
void setExpandedTitleMarginStart (int margin)

Sets the starting expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the starting title margin in pixels

See also:

setExpandedTitleMarginTop

added in version 24.1.0
void setExpandedTitleMarginTop (int margin)

Sets the top expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the top title margin in pixels

See also:

setExpandedTitleTextAppearance

added in version 24.1.0
void setExpandedTitleTextAppearance (int resId)

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

Related XML Attributes:

Parameters
resId int

setExpandedTitleTextColor

added in version 25.1.0
void setExpandedTitleTextColor (ColorStateList colors)

Sets the text colors of the expanded title.

Parameters
colors ColorStateList: ColorStateList containing the new text colors

setExpandedTitleTypeface

added in version 24.1.0
void setExpandedTitleTypeface (Typeface typeface)

Set the typeface to use for the expanded title.

Parameters
typeface Typeface: typeface to use, or null to use the default.

setScrimAnimationDuration

added in version 24.1.0
void setScrimAnimationDuration (long duration)

Set the duration used for scrim visibility animations.

Related XML Attributes:

Parameters
duration long: the duration to use in milliseconds

setScrimVisibleHeightTrigger

added in version 24.1.0
void setScrimVisibleHeightTrigger (int height)

Set the amount of visible height in pixels used to define when to trigger a scrim visibility change.

If the visible height of this view is less than the given value, the scrims will be made visible, otherwise they are hidden.

Related XML Attributes:

Parameters
height int: value in pixels used to define when to trigger a scrim visibility change

setScrimsShown

added in version 24.1.0
void setScrimsShown (boolean shown, 
                boolean animate)

Set whether the content scrim and/or status bar scrim should be shown or not. Any change in the vertical scroll may overwrite this value.

Parameters
shown boolean: whether the scrims should be shown

animate boolean: whether to animate the visibility change

See also:

setScrimsShown

added in version 24.1.0
void setScrimsShown (boolean shown)

Set whether the content scrim and/or status bar scrim should be shown or not. Any change in the vertical scroll may overwrite this value. Any visibility change will be animated if this view has already been laid out.

Parameters
shown boolean: whether the scrims should be shown

See also:

setStatusBarScrim

added in version 24.1.0
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 Drawable: the drawable to display

See also:

setStatusBarScrimColor

added in version 24.1.0
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 int: the color to display

See also:

setStatusBarScrimResource

added in version 24.1.0
void setStatusBarScrimResource (int resId)

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

Related XML Attributes:

Parameters
resId int: drawable resource id

See also:

setTitle

added in version 24.1.0
void setTitle (CharSequence title)

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

Related XML Attributes:

Parameters
title CharSequence

See also:

setTitleEnabled

added in version 24.1.0
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:

Parameters
enabled boolean

See also:

setVisibility

void setVisibility (int visibility)

Parameters
visibility int

Protected methods

checkLayoutParams

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

drawChild

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

Parameters
canvas Canvas

child View

drawingTime long

Returns
boolean

drawableStateChanged

void drawableStateChanged ()

generateDefaultLayoutParams

added in version 26.1.0
CollapsingToolbarLayout.LayoutParams generateDefaultLayoutParams ()

Returns
CollapsingToolbarLayout.LayoutParams

generateLayoutParams

added in version 26.1.0
FrameLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
FrameLayout.LayoutParams

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onLayout

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

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onSizeChanged

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

Parameters
w int

h int

oldw int

oldh int

verifyDrawable

boolean verifyDrawable (Drawable who)

Parameters
who Drawable

Returns
boolean

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)