Join us on the livestream at Android Dev Summit on 7-8 November 2018, starting at 10AM PDT!

CollapsingToolbarLayout

public class CollapsingToolbarLayout
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ com.google.android.material.appbar.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 shown 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

CollapsingToolbarLayout (Context context)

Parameters
context Context

CollapsingToolbarLayout

CollapsingToolbarLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

CollapsingToolbarLayout

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

FrameLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
FrameLayout.LayoutParams

getCollapsedTitleGravity

int getCollapsedTitleGravity ()

Returns the horizontal and vertical alignment for title when collapsed.

Related XML Attributes:

Returns
int

getCollapsedTitleTypeface

Typeface getCollapsedTitleTypeface ()

Returns the typeface used for the collapsed title.

Returns
Typeface

getContentScrim

Drawable getContentScrim ()

Returns the drawable which is used for the foreground scrim.

Related XML Attributes:

Returns
Drawable

getExpandedTitleGravity

int getExpandedTitleGravity ()

Returns the horizontal and vertical alignment for title when expanded.

Related XML Attributes:

Returns
int

getExpandedTitleMarginBottom

int getExpandedTitleMarginBottom ()

Related XML Attributes:

Returns
int the bottom expanded title margin in pixels

getExpandedTitleMarginEnd

int getExpandedTitleMarginEnd ()

Related XML Attributes:

Returns
int the ending expanded title margin in pixels

getExpandedTitleMarginStart

int getExpandedTitleMarginStart ()

Related XML Attributes:

Returns
int the starting expanded title margin in pixels

getExpandedTitleMarginTop

int getExpandedTitleMarginTop ()

Related XML Attributes:

Returns
int the top expanded title margin in pixels

getExpandedTitleTypeface

Typeface getExpandedTitleTypeface ()

Returns the typeface used for the expanded title.

Returns
Typeface

getScrimAnimationDuration

long getScrimAnimationDuration ()

Returns the duration in milliseconds used for scrim visibility animations.

Returns
long

getScrimVisibleHeightTrigger

int getScrimVisibleHeightTrigger ()

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

Returns
int

getStatusBarScrim

Drawable getStatusBarScrim ()

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

Related XML Attributes:

Returns
Drawable

getTitle

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

boolean isTitleEnabled ()

Returns whether this view is currently displaying its own title.

Related XML Attributes:

Returns
boolean

setCollapsedTitleGravity

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

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

void setCollapsedTitleTextColor (ColorStateList colors)

Sets the text colors of the collapsed title.

Parameters
colors ColorStateList: ColorStateList containing the new text colors

setCollapsedTitleTextColor

void setCollapsedTitleTextColor (int color)

Sets the text color of the collapsed title.

Parameters
color int: The new text color in ARGB format

setCollapsedTitleTypeface

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

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

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

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

void setExpandedTitleColor (int color)

Sets the text color of the expanded title.

Parameters
color int: The new text color in ARGB format

setExpandedTitleGravity

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

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

setExpandedTitleMarginBottom

void setExpandedTitleMarginBottom (int margin)

Sets the bottom expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the bottom title margin in pixels

setExpandedTitleMarginEnd

void setExpandedTitleMarginEnd (int margin)

Sets the ending expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the ending title margin in pixels

setExpandedTitleMarginStart

void setExpandedTitleMarginStart (int margin)

Sets the starting expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the starting title margin in pixels

setExpandedTitleMarginTop

void setExpandedTitleMarginTop (int margin)

Sets the top expanded title margin in pixels.

Related XML Attributes:

Parameters
margin int: the top title margin in pixels

setExpandedTitleTextAppearance

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

void setExpandedTitleTextColor (ColorStateList colors)

Sets the text colors of the expanded title.

Parameters
colors ColorStateList: ColorStateList containing the new text colors

setExpandedTitleTypeface

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

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

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

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

setScrimsShown

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

setStatusBarScrim

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

setStatusBarScrimColor

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

setStatusBarScrimResource

void setStatusBarScrimResource (int resId)

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

Related XML Attributes:

Parameters
resId int: drawable resource id

setTitle

void setTitle (CharSequence title)

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

Related XML Attributes:

Parameters
title CharSequence

setTitleEnabled

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

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

CollapsingToolbarLayout.LayoutParams generateDefaultLayoutParams ()

Returns
CollapsingToolbarLayout.LayoutParams

generateLayoutParams

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