0

Need the BottomNavigationView to always show by default (without any user interaction).

<?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=".presentation.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppBarOverlay">

        <androidx.appcompat.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/AppPopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

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

</androidx.coordinatorlayout.widget.CoordinatorLayout>

The content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment_content_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/drawer_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

Home fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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=".presentation.ui.home.HomeFragment"
    android:orientation="vertical">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPagerContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <FrameLayout
        android:id="@+id/ad_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/tab_selector"
        app:itemTextColor="@color/tab_selector"
        app:menu="@menu/bottom_navigation_menu"/>

</androidx.appcompat.widget.LinearLayoutCompat>

Unfortunately it seems that there is no answer yet on this anywhere. I tried to set layout_behavior, scrollFlags and layout_anchorGravity to bottom on BottomNavigationView but nothing works.

enter image description here

If I remove the scrollFlags on Toolbar only then the BottomNavigationView shows, but need the Toolbar having a scroll behavior but leaving the BottomNavigationView untouch.

enter image description here

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67

1 Answers1

0

The problem that the BottomNavigationView is affected by the scrolling flag set with app:layout_behavior in the activity container.

There is a couple of approaches you'd use to fix this:

  • First: add the BNV just below the CoordinatorLayout to be a part of the activity; so the layout would be:
<ConstraintLayout>
    <CoordinatorLayout>
        <AppBarLayout>
        <content_main>
    <frameLayout>
    <BottomNavigationView>

Drawback: you could have other fragments transacted in the FragmentContainerView and the BNV won't be hidden in them. You can hide the BNV by setting its visibility.

  • Second: Transfer the ToolBar to the fragment; and set it when the fragment is created. The layout scheme would be:

Activity: just have the FragmentContainerView

Home Fragment

<ConstraintLayout>
    <CoordinatorLayout>
        <AppBarLayout>
        <content_main>
    <frameLayout>
    <BottomNavigationView>

The drawback to this that you've to create and change the ToolBar for any fragment transaction in the FragmentContainerView.

Also you'd have a look at this similar question.

Zain
  • 37,492
  • 7
  • 60
  • 84