Most visited

Recently visited

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

android.support.design:elevation Deprecated. 
android.support.design:expanded The initial expanded state for the AppBarLayout. 

Inherited XML attributes

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

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)

Returns a new set of layout parameters based on the supplied attributes set.

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

Should the layout be a column or a row.

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 attibute, a new state list animator is created which uses the given elevation value.

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
AppBarLayout.LayoutParams generateDefaultLayoutParams()

Returns a set of layout parameters with a width of MATCH_PARENT and a height of WRAP_CONTENT when the layout's orientation is VERTICAL.

AppBarLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params.

int[] onCreateDrawableState(int extraSpace)

Generate the new Drawable state for this view.

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

Called from layout when this view should assign a size and position to each of its children.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

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

android.support.design:elevation

Deprecated. Elevation is now controlled via a state list animator.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

May be an integer value, such as "100".

May be a boolean value, such as "true" or "false".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb#aarrggbb".

May be a floating point value, such as "1.2".

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

Related methods:

android.support.design:expanded

The initial expanded state for the AppBarLayout. This only takes effect when this view is a direct child of a CoordinatorLayout.

May be a boolean value, such as "true" or "false".

Related methods:

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)

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

AppBarLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs AttributeSet: the attributes to build the layout parameters from
Returns
AppBarLayout.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

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 (AppBarLayout.OnOffsetChangedListener listener)

Remove the previously added AppBarLayout.OnOffsetChangedListener.

Parameters
listener AppBarLayout.OnOffsetChangedListener: the listener to remove.

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.

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

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)

Should the layout be a column or a row.

Parameters
orientation int: Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL.

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 attibute, 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

AppBarLayout.LayoutParams generateDefaultLayoutParams ()

Returns a set of layout parameters with a width of MATCH_PARENT and a height of WRAP_CONTENT when the layout's orientation is VERTICAL. When the orientation is HORIZONTAL, the width is set to WRAP_CONTENT and the height to WRAP_CONTENT.

Returns
AppBarLayout.LayoutParams a set of default layout parameters or null

generateLayoutParams

AppBarLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters
p ViewGroup.LayoutParams: The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns
AppBarLayout.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

onCreateDrawableState

int[] onCreateDrawableState (int extraSpace)

Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use getDrawableState().

Parameters
extraSpace int: if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.
Returns
int[] Returns an array holding the current Drawable state of the view.

onLayout

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

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view
l int: Left position, relative to parent
t int: Top position, relative to parent
r int: Right position, relative to parent
b int: Bottom position, relative to parent

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

Hooray!