AppBarLayout

public class AppBarLayout


AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures.

Children should provide their desired scrolling behavior through setScrollFlags and the associated layout xml attribute: app:layout_scrollFlags.

This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of its functionality will not work.

AppBarLayout also requires a separate scrolling sibling in order to know when to scroll. The binding is done through the ScrollingViewBehavior behavior class, meaning that you should set your scrolling view's behavior to be an instance of ScrollingViewBehavior. A string resource containing the full class name is available.

<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- Your scrolling content -->

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.appbar.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">

        <androidx.appcompat.widget.Toolbar
                ...
                app:layout_scrollFlags="scroll|enterAlways"/>

        <com.google.android.material.tabs.TabLayout
                ...
                app:layout_scrollFlags="scroll|enterAlways"/>

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

For more information, see the component developer guidance and design guidelines.

Summary

Nested types

protected class AppBarLayout.BaseBehavior<T extends AppBarLayout>

The default Behavior for AppBarLayout.

public abstract class AppBarLayout.BaseBehavior.BaseDragCallback<T extends AppBarLayout>

Callback to allow control over any AppBarLayout dragging.

A Parcelable implementation for AppBarLayout.

Interface definition for a callback to be invoked when an AppBarLayout's vertical offset changes.

The default Behavior for AppBarLayout.

Callback to allow control over any AppBarLayout dragging.

public abstract class AppBarLayout.ChildScrollEffect

An effect class that should be implemented and used by AppBarLayout children to be given effects when the AppBarLayout's offset changes.

A class which handles updating an AppBarLayout child, if marked with the app:layout_scrollEffectcompress, at each step in the AppBarLayout's offset animation.

A ViewGroup.LayoutParams implementation for AppBarLayout.

This interface is deprecated.

Use LiftOnScrollProgressListener instead

Definition for a callback to be invoked when the lift on scroll progress has changed.

Interface definition for a callback to be invoked when an AppBarLayout's vertical offset changes.

Behavior which should be used by Views which can scroll vertically and support nested scrolling to automatically scroll any AppBarLayout siblings.

Public constructors

AppBarLayout(Context context)
AppBarLayout(Context context, AttributeSet attrs)
AppBarLayout(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void

This method is deprecated.

Use addLiftOnScrollProgressListener instead.

void

Add a LiftOnScrollProgressListener that will be called when the lift on scroll progress changes

void

Add a listener that will be called when the offset of this AppBarLayout changes.

void
void

This method is deprecated.

Use clearLiftOnScrollProgressListener instead.

void

Remove all previously added LiftOnScrollProgressListeners.

void
draw(Canvas canvas)
AppBarLayout.LayoutParams
generateLayoutParams(AttributeSet attrs)
Behavior<AppBarLayout>
int

Returns the id of the view that the AppBarLayout should use to determine whether it should be lifted.

MaterialShapeDrawable
final int
Drawable

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

float

This method is deprecated.

target elevation is now deprecated.

final int

Returns the scroll range of all children.

boolean

Returns whether the AppBarLayout lifts on scroll or not.

boolean

Returns whether the AppBarLayout is in a lifted state or not.

boolean

This method is deprecated.

Use removeLiftOnScrollProgressListener instead.

boolean

Remove a previously added LiftOnScrollProgressListener.

void

Remove the previously added OnOffsetChangedListener.

void
void
setBackground(Drawable background)
void
setElevation(float elevation)
void
setExpanded(boolean expanded)

Sets whether this AppBarLayout is expanded or not, animating if it has already been laid out.

void
setExpanded(boolean expanded, boolean animate)

Sets whether this AppBarLayout is expanded or not.

void
setLiftOnScroll(boolean liftOnScroll)

Sets whether the AppBarLayout lifts on scroll or not.

void
setLiftOnScrollColor(ColorStateList liftOnScrollColor)

Sets the color of the AppBarLayout when it is fully lifted.

void
setLiftOnScrollTargetView(View liftOnScrollTargetView)

Sets the view that the AppBarLayout should use to determine whether it should be lifted.

void
setLiftOnScrollTargetViewId(int liftOnScrollTargetViewId)

Sets the id of the view that the AppBarLayout should use to determine whether it should be lifted.

boolean
setLiftable(boolean liftable)

Sets whether the AppBarLayout is liftable or not.

void
setLiftableOverrideEnabled(boolean enabled)

Sets whether the AppBarLayout lifted state corresponding to setLiftable and setLifted will be overridden manually.

boolean
setLifted(boolean lifted)

Sets whether the AppBarLayout is in a lifted state or not.

void
setOrientation(int orientation)
void
setStatusBarForeground(Drawable drawable)

Set the drawable to use for the status bar foreground drawable.

void

Set the color to use for the status bar foreground.

void

Set the drawable to use for the status bar foreground from resources.

void
setTargetElevation(float elevation)

This method is deprecated.

target elevation is now deprecated.

void
setVisibility(int visibility)

Protected methods

boolean
checkLayoutParams(LayoutParams p)
void
AppBarLayout.LayoutParams
AppBarLayout.LayoutParams
generateLayoutParams(LayoutParams p)
void
int[]
onCreateDrawableState(int extraSpace)
void
void
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
boolean
verifyDrawable(Drawable who)

Public fields

liftOnScrollTargetViewId

public int liftOnScrollTargetViewId

statusBarForeground

public Drawable statusBarForeground

totalScrollRange

public int totalScrollRange

Public constructors

AppBarLayout

public AppBarLayout(Context context)

AppBarLayout

public AppBarLayout(Context context, AttributeSet attrs)

AppBarLayout

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

Public methods

addLiftOnScrollListener

public void addLiftOnScrollListener(
    AppBarLayout.LiftOnScrollListener liftOnScrollListener
)

Add a LiftOnScrollListener that will be called when the lift on scroll elevation and background color of this AppBarLayout change.

addLiftOnScrollProgressListener

public void addLiftOnScrollProgressListener(
    AppBarLayout.LiftOnScrollProgressListener liftProgressListener
)

Add a LiftOnScrollProgressListener that will be called when the lift on scroll progress changes

addOnOffsetChangedListener

public void addOnOffsetChangedListener(
    AppBarLayout.BaseOnOffsetChangedListener listener
)

Add a listener that will be called when the offset of this AppBarLayout changes.

Parameters
AppBarLayout.BaseOnOffsetChangedListener listener

The listener that will be called when the offset changes.]

addOnOffsetChangedListener

public void addOnOffsetChangedListener(
    AppBarLayout.OnOffsetChangedListener listener
)

clearLiftOnScrollListener

public void clearLiftOnScrollListener()

Remove all previously added LiftOnScrollListeners.

clearLiftOnScrollProgressListener

public void clearLiftOnScrollProgressListener()

Remove all previously added LiftOnScrollProgressListeners.

draw

public void draw(Canvas canvas)

generateLayoutParams

public AppBarLayout.LayoutParams generateLayoutParams(AttributeSet attrs)

getBehavior

public Behavior<AppBarLayout> getBehavior()

getLiftOnScrollTargetViewId

public int getLiftOnScrollTargetViewId()

Returns the id of the view that the AppBarLayout should use to determine whether it should be lifted.

getMaterialShapeBackground

public MaterialShapeDrawable getMaterialShapeBackground()

getMinimumHeightForVisibleOverlappingContent

public final int getMinimumHeightForVisibleOverlappingContent()

getStatusBarForeground

public Drawable getStatusBarForeground()

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

ref R.styleable#AppBarLayout_statusBarForeground

getTargetElevation

public float getTargetElevation()

getTotalScrollRange

public final int getTotalScrollRange()

Returns the scroll range of all children.

Returns
int

the scroll range in px

isLiftOnScroll

public boolean isLiftOnScroll()

Returns whether the AppBarLayout lifts on scroll or not.

isLifted

public boolean isLifted()

Returns whether the AppBarLayout is in a lifted state or not.

removeLiftOnScrollListener

public boolean removeLiftOnScrollListener(
    AppBarLayout.LiftOnScrollListener liftOnScrollListener
)

Remove a previously added LiftOnScrollListener.

removeLiftOnScrollProgressListener

public boolean removeLiftOnScrollProgressListener(
    AppBarLayout.LiftOnScrollProgressListener liftProgressListener
)

Remove a previously added LiftOnScrollProgressListener.

removeOnOffsetChangedListener

public void removeOnOffsetChangedListener(
    AppBarLayout.BaseOnOffsetChangedListener listener
)

Remove the previously added OnOffsetChangedListener.

Parameters
AppBarLayout.BaseOnOffsetChangedListener listener

the listener to remove.

removeOnOffsetChangedListener

public void removeOnOffsetChangedListener(
    AppBarLayout.OnOffsetChangedListener listener
)

setBackground

public void setBackground(Drawable background)

setElevation

public void setElevation(float elevation)

setExpanded

public void setExpanded(boolean expanded)

Sets whether this AppBarLayout is expanded or not, animating if it has already been laid out.

As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout.

ref com.google.android.material.R.styleable#AppBarLayout_expanded

Parameters
boolean expanded

true if the layout should be fully expanded, false if it should be fully collapsed

setExpanded

public void setExpanded(boolean expanded, boolean animate)

Sets whether this AppBarLayout is expanded or not.

As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout.

ref com.google.android.material.R.styleable#AppBarLayout_expanded

Parameters
boolean expanded

true if the layout should be fully expanded, false if it should be fully collapsed

boolean animate

Whether to animate to the new state

setLiftOnScroll

public void setLiftOnScroll(boolean liftOnScroll)

Sets whether the AppBarLayout lifts on scroll or not.

If set to true, the AppBarLayout will animate to the lifted, or elevated, state when content is scrolled beneath it. Requires `app:layout_behavior="@string/appbar_scrolling_view_behavior` to be set on the scrolling sibling (e.g., `NestedScrollView`, `RecyclerView`, etc.).

setLiftOnScrollColor

public void setLiftOnScrollColor(ColorStateList liftOnScrollColor)

Sets the color of the AppBarLayout when it is fully lifted.

setLiftOnScrollTargetView

public void setLiftOnScrollTargetView(View liftOnScrollTargetView)

Sets the view that the AppBarLayout should use to determine whether it should be lifted.

setLiftOnScrollTargetViewId

public void setLiftOnScrollTargetViewId(int liftOnScrollTargetViewId)

Sets the id of the view that the AppBarLayout should use to determine whether it should be lifted.

setLiftable

public boolean setLiftable(boolean liftable)

Sets whether the AppBarLayout is liftable or not.

Returns
boolean

true if the liftable state changed

setLiftableOverrideEnabled

public void setLiftableOverrideEnabled(boolean enabled)

Sets whether the AppBarLayout lifted state corresponding to setLiftable and setLifted will be overridden manually.

If true, this means that the AppBarLayout will not manage its own lifted state and it should instead be manually updated via setLifted. If false, the will manage its lifted state based on the scrolling sibling view.

Note that calling setLiftable will result in this liftable override being enabled and set to true by default.

setLifted

public boolean setLifted(boolean lifted)

Sets whether the AppBarLayout is in a lifted state or not.

Returns
boolean

true if the lifted state changed

setOrientation

public void setOrientation(int orientation)

setStatusBarForeground

public void setStatusBarForeground(Drawable drawable)

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

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

ref R.styleable#AppBarLayout_statusBarForeground

Parameters
Drawable drawable

the drawable to display

setStatusBarForegroundColor

public void setStatusBarForegroundColor(int color)

Set the color to use for the status bar foreground.

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

ref R.styleable#AppBarLayout_statusBarForeground

Parameters
int color

the color to display

setStatusBarForegroundResource

public void setStatusBarForegroundResource(int resId)

Set the drawable to use for the status bar foreground from resources.

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

ref R.styleable#AppBarLayout_statusBarForeground

Parameters
int resId

drawable resource id

setTargetElevation

public void setTargetElevation(float elevation)

ref com.google.android.material.R.styleable#AppBarLayout_elevation

setVisibility

public void setVisibility(int visibility)

Protected methods

checkLayoutParams

protected boolean checkLayoutParams(LayoutParams p)

drawableStateChanged

protected void drawableStateChanged()

generateDefaultLayoutParams

protected AppBarLayout.LayoutParams generateDefaultLayoutParams()

generateLayoutParams

protected AppBarLayout.LayoutParams generateLayoutParams(LayoutParams p)

onAttachedToWindow

protected void onAttachedToWindow()

onCreateDrawableState

protected int[] onCreateDrawableState(int extraSpace)

onDetachedFromWindow

protected void onDetachedFromWindow()

onLayout

protected void onLayout(boolean changed, int l, int t, int r, int b)

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

verifyDrawable

protected boolean verifyDrawable(Drawable who)