AppBarLayout

public class AppBarLayout
extends LinearLayout

java.lang.Object
   ↳ LinearLayout
     ↳ com.google.android.material.appbar.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(int) 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 it's functionality will not work.

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

 <android.support.design.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">

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

         <!-- Your scrolling content -->

     </android.support.v4.widget.NestedScrollView>

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

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

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

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

 </android.support.design.widget.CoordinatorLayout>
 

Summary

Nested classes

class AppBarLayout.BaseBehavior<T extends AppBarLayout>

The default AppBarLayout.Behavior for AppBarLayout

interface AppBarLayout.BaseOnOffsetChangedListener<T extends AppBarLayout>

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

class AppBarLayout.Behavior

The default AppBarLayout.Behavior for AppBarLayout

class AppBarLayout.LayoutParams

 

interface AppBarLayout.OnOffsetChangedListener

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

class AppBarLayout.ScrollingViewBehavior

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)

Public methods

void addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener listener)
void addOnOffsetChangedListener(BaseOnOffsetChangedListener listener)

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

AppBarLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
final int getMinimumHeightForVisibleOverlappingContent()
float getTargetElevation()

This method is deprecated. target elevation is now deprecated. AppBarLayout's elevation is now controlled via a StateListAnimator. This method now always returns 0.

final int getTotalScrollRange()

Returns the scroll range of all children.

void removeOnOffsetChangedListener(BaseOnOffsetChangedListener listener)

Remove the previously added AppBarLayout.OnOffsetChangedListener.

void removeOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener listener)
void setExpanded(boolean expanded, boolean animate)

Sets whether this AppBarLayout is expanded or not.

void setExpanded(boolean expanded)

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

void setOrientation(int orientation)
void setTargetElevation(float elevation)

This method is deprecated. target elevation is now deprecated. AppBarLayout's elevation is now controlled via a StateListAnimator. If a target elevation is set, either by this method or the app:elevation attribute, a new state list animator is created which uses the given elevation value.

Protected methods

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

Inherited methods

From class java.lang.Object

Public constructors

AppBarLayout

AppBarLayout (Context context)

Parameters
context Context

AppBarLayout

AppBarLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Public methods

addOnOffsetChangedListener

void addOnOffsetChangedListener (AppBarLayout.OnOffsetChangedListener listener)

Parameters
listener AppBarLayout.OnOffsetChangedListener

addOnOffsetChangedListener

void addOnOffsetChangedListener (BaseOnOffsetChangedListener listener)

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

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

generateLayoutParams

AppBarLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
AppBarLayout.LayoutParams

getMinimumHeightForVisibleOverlappingContent

int getMinimumHeightForVisibleOverlappingContent ()

Returns
int

getTargetElevation

float getTargetElevation ()

This method is deprecated.
target elevation is now deprecated. AppBarLayout's elevation is now controlled via a StateListAnimator. This method now always returns 0.

Returns
float

getTotalScrollRange

int getTotalScrollRange ()

Returns the scroll range of all children.

Returns
int the scroll range in px

removeOnOffsetChangedListener

void removeOnOffsetChangedListener (BaseOnOffsetChangedListener listener)

Remove the previously added AppBarLayout.OnOffsetChangedListener.

Parameters
listener BaseOnOffsetChangedListener: the listener to remove.

removeOnOffsetChangedListener

void removeOnOffsetChangedListener (AppBarLayout.OnOffsetChangedListener listener)

Parameters
listener AppBarLayout.OnOffsetChangedListener

setExpanded

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.

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

animate boolean: Whether to animate to the new state

setExpanded

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.

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

setOrientation

void setOrientation (int orientation)

Parameters
orientation int

setTargetElevation

void setTargetElevation (float elevation)

This method is deprecated.
target elevation is now deprecated. AppBarLayout's elevation is now controlled via a StateListAnimator. If a target elevation is set, either by this method or the app:elevation attribute, a new state list animator is created which uses the given elevation value.

Parameters
elevation float

Protected methods

checkLayoutParams

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateDefaultLayoutParams

AppBarLayout.LayoutParams generateDefaultLayoutParams ()

Returns
AppBarLayout.LayoutParams

generateLayoutParams

AppBarLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
AppBarLayout.LayoutParams

onCreateDrawableState

int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]

onLayout

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

Parameters
changed boolean

l int

t int

r int

b int

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int