0

I am so tried to do that. I have completed my android project and it also listed on google playstore. I want to make my app toolbar auto hide while scroll and auto show when scroll down/up.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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:orientation="vertical"
    android:background="@color/colorBG"
    tools:context="net.gurujibd.ajkerkhobor.MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>

    <android.support.design.widget.TabLayout
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/tabLayout"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:background="@color/colorPrimaryDark">

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


    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v4.view.ViewPager>

</LinearLayout>

it's my main activity. and i have a custom toolbar file.

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

and I am using fragment layout for tabbed activity. it's my home activity.

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Home">

    <!-- TODO: Update blank fragment layout -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:background="@color/colorBG"
            android:weightSum="10"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:layout_weight="2"
                android:layout_width="match_parent"
                android:layout_height="0dp">

                <TextView
                    android:id="@+id/textGrid"
                    android:text="Ajker Khobor"
                    android:textSize="34sp"
                    android:textColor="@android:color/white"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>

            <GridLayout
                android:id="@+id/mainGrid"
                android:layout_width="match_parent"
                android:layout_height="736dp"
                android:layout_weight="8"
                android:alignmentMode="alignMargins"
                android:columnCount="2"
                android:columnOrderPreserved="false"
                android:padding="14dp"
                android:rowCount="3">

                <!-- Row 1 -->

                <!-- Column 1 -->
                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/prothomalo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/prothomalo" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <!-- Column 2 -->
                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/noyadigonto"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/noyadigonto" />


                    </LinearLayout>

                </android.support.v7.widget.CardView>


                <!-- Row 2 -->

                <!-- Column 1 -->
                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/bangladeshprotidin"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/bangladeshprotidin" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <!-- Column 2 -->
                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/inclub"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/inclub" />


                    </LinearLayout>

                </android.support.v7.widget.CardView>


                <!-- Row 2 -->

                <!-- Column 1 -->
                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/manobkontho"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/manobkontho" />


                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <!-- Column 2 -->
                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/ittefaq"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/ittefaq" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/jugantor"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/jugantor" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/somoynews"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/somoynews" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/amadersomoy"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/amadersomoy" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/manobjomin"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/manobjomin" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/kalerkontho"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/kalerkontho" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/bonikbarta"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/bonikbarta" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/bbcbangla"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/bbcbangla" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/songbad"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/songbad" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/jonokontho"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/jonokontho" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_rowWeight="1"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="8dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_margin="16dp"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/vorerkagoj"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:src="@drawable/vorerkagoj" />

                    </LinearLayout>

                </android.support.v7.widget.CardView>

            </GridLayout>

        </LinearLayout>

    </ScrollView>

</FrameLayout>

please help me to solve this problem. advanced thanks.

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
Liton
  • 5
  • 4
  • 1
    Possible duplicate of [How to hide ToolBar when i scrolling content up in android](https://stackoverflow.com/questions/35475884/how-to-hide-toolbar-when-i-scrolling-content-up-in-android) – Sniffer Sep 08 '18 at 05:18

1 Answers1

1

Toolbar scrolling behavior was introduced in Material Design and implemented along CoordinatorLayout. To use this feature you must use CoordinatorLayout along Appbar in your app layout.

For more info check this tutorial: https://guides.codepath.com/android/handling-scrolls-with-coordinatorlayout

For example in your code:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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:background="@color/colorBG"
    tools:context="net.gurujibd.ajkerkhobor.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            app:layout_scrollFlags="scroll"/>

        <android.support.design.widget.TabLayout
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/tabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"
            android:background="@color/colorPrimaryDark"
            app:layout_scrollFlags="scroll"/>
     </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

and don't forget to change your scrollView to NestedScrollView:

<FrameLayout>
    <ScrollView>
        <Your Content .../>
    </ScrollView>
</FrameLayout>

, changes to:

<android.support.v4.widget.NestedScrollView>
    <Your Content .../>
</android.support.v4.widget.NestedScrollView>
Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
  • i was read this type of instructions. but i can't do that with my code. can you please edit my code and post it an Answer for me? i can't understand where i need to put CoordinatorLayout. edit my code and past it here. thanks – Liton Sep 08 '18 at 05:46
  • not working... when i'm scrolling the toolbar not effected anything. please help. – Liton Sep 08 '18 at 06:05
  • can i need to do anything on java file? – Liton Sep 08 '18 at 06:08
  • thanks. it working by adding app:layout_scrollFlags="scroll|enterAlways" in toolbar... but one more problems. while i'm scrolling it was hiding toolbar withe viewpager. i want to show viwepager alwasy, only hide toolbar. how to do that? – Liton Sep 08 '18 at 06:32
  • many many thanks bro... now all problem is solved. thanks for your help. – Liton Sep 08 '18 at 06:38
  • urw, try experiment with the tutorial to test other scrolling behavior! – Keivan Esbati Sep 08 '18 at 07:21