AppBarLayout
public
class
AppBarLayout
extends LinearLayout
implements
CoordinatorLayout.AttachedBehavior
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.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 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 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.
<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>
Summary
Nested classes | |
---|---|
class |
AppBarLayout.BaseBehavior<T extends AppBarLayout>
The default |
interface |
AppBarLayout.BaseOnOffsetChangedListener<T extends AppBarLayout>
Interface definition for a callback to be invoked when an |
class |
AppBarLayout.Behavior
The default |
class |
AppBarLayout.LayoutParams
A |
interface |
AppBarLayout.OnOffsetChangedListener
Interface definition for a callback to be invoked when an |
class |
AppBarLayout.ScrollingViewBehavior
Behavior which should be used by |
XML attributes | |
---|---|
AppBarLayout_elevation |
|
AppBarLayout_expanded |
|
AppBarLayout_statusBarForeground |
Inherited constants |
---|
![]()
android.widget.LinearLayout
|
![]()
android.view.ViewGroup
|
![]()
android.view.View
|
Inherited fields |
---|
![]()
android.view.View
|
Public constructors | |
---|---|
AppBarLayout(Context context)
|
|
AppBarLayout(Context context, AttributeSet attrs)
|
|
AppBarLayout(Context context, AttributeSet attrs, int defStyleAttr)
|
Public methods | |
---|---|
void
|
addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener listener)
|
void
|
addOnOffsetChangedListener(BaseOnOffsetChangedListener listener)
Add a listener that will be called when the offset of this |
void
|
draw(Canvas canvas)
|
AppBarLayout.LayoutParams
|
generateLayoutParams(AttributeSet attrs)
|
Behavior<AppBarLayout>
|
getBehavior()
|
int
|
getLiftOnScrollTargetViewId()
Returns the id of the view that the |
final
int
|
getMinimumHeightForVisibleOverlappingContent()
|
Drawable
|
getStatusBarForeground()
Returns the drawable which is used for the status bar foreground. |
float
|
getTargetElevation()
This method is deprecated.
target elevation is now deprecated. AppBarLayout's elevation is now controlled via
a |
final
int
|
getTotalScrollRange()
Returns the scroll range of all children. |
boolean
|
isLiftOnScroll()
Returns whether the |
void
|
removeOnOffsetChangedListener(BaseOnOffsetChangedListener listener)
Remove the previously added |
void
|
removeOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener listener)
|
void
|
setElevation(float elevation)
|
void
|
setExpanded(boolean expanded, boolean animate)
Sets whether this |
void
|
setExpanded(boolean expanded)
Sets whether this |
void
|
setLiftOnScroll(boolean liftOnScroll)
Sets whether the |
void
|
setLiftOnScrollTargetViewId(int liftOnScrollTargetViewId)
Sets the id of the view that the |
boolean
|
setLiftable(boolean liftable)
Sets whether the |
boolean
|
setLifted(boolean lifted)
Sets whether the |
void
|
setOrientation(int orientation)
|
void
|
setStatusBarForeground(Drawable drawable)
Set the drawable to use for the status bar foreground drawable. |
void
|
setStatusBarForegroundColor(int color)
Set the color to use for the status bar foreground. |
void
|
setStatusBarForegroundResource(int resId)
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. AppBarLayout's elevation is now controlled via
a |
void
|
setVisibility(int visibility)
|
Protected methods | |
---|---|
boolean
|
checkLayoutParams(ViewGroup.LayoutParams p)
|
void
|
drawableStateChanged()
|
AppBarLayout.LayoutParams
|
generateDefaultLayoutParams()
|
AppBarLayout.LayoutParams
|
generateLayoutParams(ViewGroup.LayoutParams p)
|
void
|
onAttachedToWindow()
|
int[]
|
onCreateDrawableState(int extraSpace)
|
void
|
onDetachedFromWindow()
|
void
|
onLayout(boolean changed, int l, int t, int r, int b)
|
void
|
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
boolean
|
verifyDrawable(Drawable who)
|
XML attributes
AppBarLayout_elevation
Related methods:
AppBarLayout_expanded
Related methods:
AppBarLayout_statusBarForeground
Related methods:
Public constructors
AppBarLayout
AppBarLayout (Context context)
Parameters | |
---|---|
context |
Context |
AppBarLayout
AppBarLayout (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
AppBarLayout
AppBarLayout (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
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.] |
draw
void draw (Canvas canvas)
Parameters | |
---|---|
canvas |
Canvas |
generateLayoutParams
AppBarLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
Parameters | |
---|---|
attrs |
AttributeSet |
Returns | |
---|---|
AppBarLayout.LayoutParams |
getLiftOnScrollTargetViewId
int getLiftOnScrollTargetViewId ()
Returns the id of the view that the AppBarLayout
should use to determine whether it
should be lifted.
Returns | |
---|---|
int |
getMinimumHeightForVisibleOverlappingContent
int getMinimumHeightForVisibleOverlappingContent ()
Returns | |
---|---|
int |
getStatusBarForeground
Drawable getStatusBarForeground ()
Returns the drawable which is used for the status bar foreground.
Related XML Attributes:
Returns | |
---|---|
Drawable |
See also:
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 |
isLiftOnScroll
boolean isLiftOnScroll ()
Returns whether the AppBarLayout
lifts on scroll or not.
Returns | |
---|---|
boolean |
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 |
setElevation
void setElevation (float elevation)
Parameters | |
---|---|
elevation |
float |
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 |
setLiftOnScroll
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.).
Parameters | |
---|---|
liftOnScroll |
boolean |
setLiftOnScrollTargetViewId
void setLiftOnScrollTargetViewId (int liftOnScrollTargetViewId)
Sets the id of the view that the AppBarLayout
should use to determine whether it should
be lifted.
Parameters | |
---|---|
liftOnScrollTargetViewId |
int |
setLiftable
boolean setLiftable (boolean liftable)
Sets whether the AppBarLayout
is liftable or not.
Parameters | |
---|---|
liftable |
boolean |
Returns | |
---|---|
boolean |
true if the liftable state changed |
setLifted
boolean setLifted (boolean lifted)
Sets whether the AppBarLayout
is in a lifted state or not.
Parameters | |
---|---|
lifted |
boolean |
Returns | |
---|---|
boolean |
true if the lifted state changed |
setOrientation
void setOrientation (int orientation)
Parameters | |
---|---|
orientation |
int |
setStatusBarForeground
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.
Related XML Attributes:
Parameters | |
---|---|
drawable |
Drawable : the drawable to display |
See also:
setStatusBarForegroundColor
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.
Related XML Attributes:
Parameters | |
---|---|
color |
int : the color to display |
See also:
setStatusBarForegroundResource
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.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : drawable resource id |
See also:
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.
Related XML Attributes:
Parameters | |
---|---|
elevation |
float |
setVisibility
void setVisibility (int visibility)
Parameters | |
---|---|
visibility |
int |
Protected methods
checkLayoutParams
boolean checkLayoutParams (ViewGroup.LayoutParams p)
Parameters | |
---|---|
p |
ViewGroup.LayoutParams |
Returns | |
---|---|
boolean |
drawableStateChanged
void drawableStateChanged ()
generateDefaultLayoutParams
AppBarLayout.LayoutParams generateDefaultLayoutParams ()
Returns | |
---|---|
AppBarLayout.LayoutParams |
generateLayoutParams
AppBarLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)
Parameters | |
---|---|
p |
ViewGroup.LayoutParams |
Returns | |
---|---|
AppBarLayout.LayoutParams |
onAttachedToWindow
void onAttachedToWindow ()
onCreateDrawableState
int[] onCreateDrawableState (int extraSpace)
Parameters | |
---|---|
extraSpace |
int |
Returns | |
---|---|
int[] |
onDetachedFromWindow
void onDetachedFromWindow ()
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 |
verifyDrawable
boolean verifyDrawable (Drawable who)
Parameters | |
---|---|
who |
Drawable |
Returns | |
---|---|
boolean |
Interfaces
Classes
- AppBarLayout
- AppBarLayout.BaseBehavior
- AppBarLayout.BaseBehavior.BaseDragCallback
- AppBarLayout.BaseBehavior.SavedState
- AppBarLayout.Behavior
- AppBarLayout.Behavior.DragCallback
- AppBarLayout.LayoutParams
- AppBarLayout.ScrollingViewBehavior
- CollapsingToolbarLayout
- CollapsingToolbarLayout.LayoutParams
- MaterialToolbar
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-07-20 UTC.