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

AppBarLayout

public class AppBarLayout
extends LinearLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.support.design.widget.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>

     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

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

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

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

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

See also:

Summary

Nested classes

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. 

XML attributes

AppBarLayout_elevation  
AppBarLayout_expanded  

Inherited constants

From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

void addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener listener)

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

AppBarLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
float getTargetElevation()

This method was deprecated in API level 24.1.0. 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(AppBarLayout.OnOffsetChangedListener listener)

Remove the previously added AppBarLayout.OnOffsetChangedListener.

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 was deprecated in API level 24.1.0. 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 android.widget.LinearLayout
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

AppBarLayout_elevation

Related methods:

AppBarLayout_expanded

Related methods:

Public constructors

AppBarLayout

added in version 24.1.0
AppBarLayout (Context context)

Parameters
context Context

AppBarLayout

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

Parameters
context Context

attrs AttributeSet

Public methods

addOnOffsetChangedListener

added in version 24.1.0
void addOnOffsetChangedListener (AppBarLayout.OnOffsetChangedListener listener)

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

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

See also:

generateLayoutParams

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

Parameters
attrs AttributeSet

Returns
AppBarLayout.LayoutParams

getTargetElevation

added in version 24.1.0
float getTargetElevation ()

This method was deprecated in API level 24.1.0.
target elevation is now deprecated. AppBarLayout's elevation is now controlled via a StateListAnimator. This method now always returns 0.

Returns
float

getTotalScrollRange

added in version 24.1.0
int getTotalScrollRange ()

Returns the scroll range of all children.

Returns
int the scroll range in px

removeOnOffsetChangedListener

added in version 24.1.0
void removeOnOffsetChangedListener (AppBarLayout.OnOffsetChangedListener listener)

Remove the previously added AppBarLayout.OnOffsetChangedListener.

Parameters
listener AppBarLayout.OnOffsetChangedListener: the listener to remove.

setExpanded

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

Related XML Attributes:

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

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

Related XML Attributes:

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

added in version 24.1.0
void setTargetElevation (float elevation)

This method was deprecated in API level 24.1.0.
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.

Related XML Attributes:

Parameters
elevation float

Protected methods

checkLayoutParams

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateDefaultLayoutParams

added in version 26.1.0
AppBarLayout.LayoutParams generateDefaultLayoutParams ()

Returns
AppBarLayout.LayoutParams

generateLayoutParams

added in version 26.1.0
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

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)