How to hide ActionBar while scrolling ListView in android?
If you would like to obtain a list with this behaviour, you should:
- add the design support library with
compile 'com.android.support:design:22.2.0'
- Use a
CoordinatorLayout
with a Toolbar where you have to defineapp:layout_scrollFlags="scroll|enterAlways"
- Use a
RecyclerView
instead of aListView
. As described hereListView
and theGridView
have the expected behavior with theCoordinatorLayout
only with API>21. In this case you have to usesetNestedScrollingEnabled(true);
The official blog post shows this case:
<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">
<! -- Your Scrollable View -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Hide Action Bar on View scrolled
1.implement ViewTreeObserver.OnScrollChangedListener into your class,
2.Write this line into your onCreate "getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);" (without quotes)
3.Now get ActionBar Height for Hide animation as follows -
final TypedArray mstyled = getTheme().obtainStyledAttributes(new int[]{android.R.attr.actionBarSize });
mActionBarHeight = styledAttributes.getDimension(0, 0);
mstyled.recycle();
4.Get Action Bar.
mActionBar = getActionBar();
5.Intilize scrollview.
((ScrollView)findViewById(R.id.parent)).getViewTreeObserver().addOnScrollChangedListener(this);
6.override onScrollChange and mode your method like this.
@Override
public void onScrollChanged() {
float mfloat = ((ScrollView)findViewById(R.id.parent)).getScrollY();
if (mfloat >= mActionBarHeight && mActionBar.isShowing()) {
mActionBar.hide();
} else if ( mfloat==0 && !mActionBar.isShowing()) {
mActionBar.show();
}
}
Hope it Helps.
Android ActionBar hide/show when scrolling list view
To avoid the flickering caused by the readjustment of the layout you can use the overlaying property in the action bar.
https://developer.android.com/training/basics/actionbar/overlaying.html
But this solution causes that the action bar overlays your listview, so you need to add a header with this size:
"?android:attr/actionBarSize"
for example :
mVideosListView = (ListView) getActivity().findViewById(android.R.id.list);
mVideosListView.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.listview_header,null));
Be careful with other elements like navigation drawer which is also affected by this.
Related Topics
Comparing Bitmap Images in Android
Write to /Res/Drawable/ on the Fly
Send Post Request Along with Httpheaders on Android
How to Detect If User First Time in Firebase
How to Animate Addition or Removal of Android Listview Rows
Why Do Most Fields (Class Members) in Android Tutorial Start with 'M'
How to Create Xxhdpi, Xhdpi, Hdpi, Mdpi and Ldpi Drawables from a Large Scale Image
Will Google Android Ever Support .Net
How to Display a Yes/No Dialog Box on Android
When Should One Use Theme.Appcompat VS Themeoverlay.Appcompat
Switching to Landscape Mode in Android Emulator
Material Design Not Styling Alert Dialogs
Stopping an Android App from Console
Android:Inapp Purchase Receipt Validation Google Play
What Are the Differences Between Linearlayout, Relativelayout, and Absolutelayout
Custom Cut/Copy Action Bar for Edittext That Shows Text Selection Handles