In this blog,
I showed how to achieve hiding/showing Toolbar when the list is scrolling by Design Support Library and we don’t need to do it manually.
I will show how to use CoordinatorLayout and create a simple Behavior to hiding/show Toolbar when list scrolls up/down.
What to do
In below example, I will use a CoordinatorLayout as a parent Layout and use a ToolBar with AppBarLayout. And a recylerView which will be scrolled.
So,
Step 1: Create a ToolBar separately for use it in an entire application
toolbar.xml
1 2 3 4 5 6 7 8 9 10 |
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextColor="#FFFFFF" app:layout_scrollFlags="scroll|enterAlways" /> |
toolbar: – A Toolbar is a generalized form of action bars
for use within application layouts.
Set this flag to your ToolBar.
1 |
app:layout_scrollFlags="scroll|enterAlways" |
this app:layout_scrollFlags
attribute, performs scroll events in the RecyclerView trigger changes inside views declared within AppBarLayout.
enterAlways
: The view will become visible when scrolling up.
snap
: Using this option will determine what to do when a view only has been partially reduced.
Step 2: Create the other XML file where you want to apply hide/show property
hide_show.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> |
CoordinatorLayout: – CoordinatorLayout has the ability to achieve many of the Google’s Material Design scrolling effects.
AppBarLayout: – AppBarLayout is a vertical LinearLayout
which performs many of the features of material designs app bar concept, namely scrolling gesticulation.
recylerView: – Android now give the RecyclerView widget, a new and enhanced view group for displaying very large collections of data.
Set the behavior in recycler view
1 |
app:layout_behavior="@string/appbar_scrolling_view_behavior" |
We have to define AppBarLayout and the View that will be scrolled.
Add a app:layout_behavior
to a RecyclerView or any other View prepared of nested scrolling such as NestedScrollView.
By Adding the above property in your XML, you can achieve the ability to hide and showing toolbar while scrolling.