Nicky Manali Nicky Manali - 27 days ago 6
Android Question

hiding toolbar and drawer on scrolling

I'm making an android app in which I'm hiding the toolbar on scroll, but something went wrong and I don't know what. I have tried everything from AppBarLayout to CollapsingToolbarLayout but nothing works and I don't know what is missing here my code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.manali.vivek.projectx.MainActivity">

<android.support.v4.widget.DrawerLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"


android:elevation="7dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
tools:context="com.manali.vivek.userregistration.startActivity">

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

android:theme="@style/ThemeOverlay.AppCompat.Dark">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
/>


<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:tabTextColor="#a6a2a2"
app:tabSelectedTextColor="#0099ff"
app:tabIndicatorColor="#0099ff"

android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"

android:background="#ffffff"
android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>


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


any help will be welcome.

Answer

Why you are adding LinearLayout? Remove LinearLayout and add this xml and try

Drawer Layout xml

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            android:id="@+id/includeAppBar"
            layout="@layout/app_bar_navigation_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <FrameLayout
                    android:id="@+id/containerMenu"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>
        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>

app_bar_navigation_drawer.xml

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:text="@string/app_name"
                    android:textAllCaps="true"
                    app:font="@{`bold`}"
                    android:textAppearance="?android:textAppearanceLarge"
                    android:textColor="@android:color/white" />

            </LinearLayout>
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:background="@color/colorPrimaryDar"
            app:tabMode="fixed"
            app:tabPaddingStart="0dp"
            app:tabPaddingEnd="0dp"
            app:tabGravity="fill"
            app:tabIndicatorHeight="0dp"
            android:layout_height="wrap_content" />

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

    <include layout="@layout/frame_layout" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:visibility="gone"
        android:clickable="true"
        android:focusable="true"
        android:background="?attr/selectableItemBackground"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_plus" />

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