Search code examples
androidandroid-layoutandroid-studiomaterial-designnavbar

Android Studio Bottom Navigation View disappearing after adding menu items


I have made the menu and the Bottom Navigation View and its menu but when I add app:menu"@menu/bottom_navigation_menu" the whole Bar disappears and the app crashes when I try to run it.

Activity Main seems to be implemented correctly:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context="com.last.first.lightswitch.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:menu="@menu/bottom_navigation_menu"/>


</LinearLayout>

As well as the menu items:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/buttonSlider"
        android:icon="@drawable/ic_slider"
        android:title="Sliders"
        />
    <item
        android:id="@+id/buttonPalette"
        android:icon="@drawable/ic_palette"
        android:title="Palette"
        />
    <item
        android:id="@+id/buttonShuffle"
        android:icon="@drawable/ic_shuffle"
        android:title="Shuffle"
        />
    <item
        android:id="@+id/buttonSchedule"
        android:icon="@drawable/ic_alarm_off"
        android:title="Schedule"
        />
    <item
        android:id="@+id/buttonConnect"
        android:icon="@drawable/ic_bt_off"
        android:title="Connect"
        />
    <item
        android:id="@+id/buttonStats"
        android:icon="@drawable/ic_timeline"
        android:title="Statistics"
        />
</menu>

Solution

  • If you check the stack trace for the crash, you'll find this:

    03-29 00:08:30.568  9850  9850 E AndroidRuntime:
        Caused by: java.lang.IllegalArgumentException:
        Maximum number of items supported by BottomNavigationView is 5.
        Limit can be checked with BottomNavigationView#getMaxItemCount()
    

    Indeed, if you check the documentation for BottomNavigationView, you will find:

    It should be used when application has three to five top-level destinations.

    The solution is simple enough: delete one of your menu items. If that is not an option for you, you will have to create your own implementation of bottom navigation rather than using BottomNavigationView.