Search code examples
javaandroidandroidx

BottomNavigationView stuck at top


I've been trying to get a BottonNavigationView working. It just keeps sticking to the top of the layout and it's driving me nuts.

I've looked at Material Design Documentation for BottomNavigationView and it seems really simple, but I can't get it to work, https://material.io/develop/android/components/bottom-navigation/. I even tried making a completely clean project where I tried it and the same thing happened.

<?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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:id="@+id/mainLayout">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        app:menu="@menu/bottom_bar_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/content"
        android:enabled="true"
        android:icon="@drawable/ic_menu_black_24dp"
        android:title="Content"/>
    <item
        android:id="@+id/document"
        android:enabled="true"
        android:icon="@drawable/ic_picture_as_pdf_black_24dp"
        android:title="Document"/>
    <item
        android:id="@+id/search"
        android:enabled="true"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="Search"/>
</menu>

Screenshot

Any ideas?


Solution

  • I would like to recommend to use ConstraintLayout and set constraints in Designer tool. Will be easier if you are new in android world.

    <?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"
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_bar_menu" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>