javaandroidandroid-layoutandroid-recyclerviewandroid-coordinatorlayout

How to Show/Hide Action Bar on Swipe using Coordinator Layout?


I show/hide the bottom navigation bar on swiping with CoordinatorLayout but am unable to show/hide the action bar using CoordinatorLayout. What mistake I did in my layout?

On swiping, the bottom navigation bar in my layout is auto-hide and show but not on the top action bar?

This is My XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/noInternet_layout"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/no_wifi_image"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_wifi_off_24" />

    <TextView
        android:id="@+id/no_wifi_text"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="@string/noWifi"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/no_wifi_image"
        app:layout_constraintStart_toStartOf="@+id/no_wifi_image"
        app:layout_constraintTop_toBottomOf="@+id/no_wifi_image" />

    <Button
        android:id="@+id/no_wifi_button"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="TRY AGAIN"
        app:layout_constraintEnd_toEndOf="@+id/no_wifi_text"
        app:layout_constraintStart_toStartOf="@+id/no_wifi_text"
        app:layout_constraintTop_toBottomOf="@+id/no_wifi_text" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLyt"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar"
        layout="@layout/main_toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_scrollFlags="scroll|enterAlways" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/navHostFrag"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLyt"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintVertical_bias="1.0"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:itemTextColor="@drawable/bottom_text_selector"
    app:itemIconTint="@drawable/bottom_navigation_selector"
    app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
    app:itemTextAppearanceActive="@color/white"
    app:labelVisibilityMode="labeled"
    app:itemRippleColor="@color/menucolor"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_gravity="bottom"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Please Help me to solve this issue?


Solution

  • Finally I found.

    This is very simple to get swipeable (Show-Hide) both side of bottom Navigation and Top ActionBar without any backend code.

    Follow Below code

    <!--This is your parent layout-->
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            app:elevation="0dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                android:layout_height="?attr/actionBarSize"> 
            </androidx.appcompat.widget.Toolbar>
    
        </com.google.android.material.appbar.AppBarLayout>
    
    <!--Move your main layout of the screen to a different layout and call it with include. Without including that, you cannot hide one of them Top or Bottom. So move that layout and include it here. (example-  @layout/content_main)-->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <include layout="@layout/content_main"/>
    
        </FrameLayout>
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/custom_bottomBar_size"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
            style="@style/Widget.Custom.BottomNavigationView"
            app:itemRippleColor="?android:attr/windowBackground"
            app:itemTextAppearanceActive="@color/white"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/bottom_navigation" />
        
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    By using ablow proper method, you'll easily swipe up-down both BottomNavigation and Top ActionBar

    Output is like below

    enter image description here

    Hope It'll help someone.