Search code examples
androidandroid-layoutbottomnavigationview

Why my bottom navigation bar doesn't appear


I set my bottom navigation bar, but somehow I can't see it on my screen.

My setup proccess:

xml:

<androidx.appcompat.widget.LinearLayoutCompat 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"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="24dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph"
 />
<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/bottom_nav_menu"
    />

</androidx.appcompat.widget.LinearLayoutCompat>

menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/mainFragment"
        android:icon="@drawable/ic_launcher_background"
        android:title="@string/home" />
    <item
        android:id="@+id/newsList"
        android:icon="@drawable/ic_launcher_background"
        android:title="@string/news_list" />
    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_launcher_background"
        android:title="@string/settings" />
</menu>

MainActivity:

bottom_nav_view.setupWithNavController(navController)

Where am I doing wrong? I checked some tutorials, and that exactly the process of setting bottom navigation view


Solution

  • Use this height and weight for your fragment tag:

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
     />