Search code examples
androidandroid-constraintlayoutandroid-stylesbottomnavigationview

BottomNavigationView adding extra space to the left


Hi I have a BottomNavigationView but it adds a default space to the start.Below is my code.

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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:textAlignment="center"
    android:textSize="25dp"
    android:id="@+id/message"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:text="Home"
    />


<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/app_bar_layout"
    >

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/home_toolbar"
        >

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@android:color/holo_green_dark"
            app:labelVisibilityMode="unlabeled"
            app:menu="@menu/bottom_nav_menu" />

    </androidx.appcompat.widget.Toolbar>

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

This is my menu:

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

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/home"
    android:title="@string/title_home"
    />

<item
    android:id="@+id/navigation_search"
    android:icon="@drawable/search"
    android:title="@string/title_dashboard" />

<item
    android:id="@+id/nav_add_post"
    android:icon="@drawable/add"
    android:title="@string/title_notifications" />

<item
    android:id="@+id/navigation_notifications"
    android:icon="@drawable/heart"
    android:title="@string/title_notifications" />

<item
    android:id="@+id/nav_profile"
    android:icon="@drawable/profile_icon"
    android:title="@string/title_notifications" />

</menu>

This is how it looks when i run.

enter image description here

I have referred to this post : Bottom Navigation View With Left Righ Space Issue, but it didn't help me resolve the issue and i also tried app:itemBackground="@android:color/holo_green_dark" which also didn't solve my spacing issue.


Solution

  • You can remove the Toolbar if you don't need it and it will work normally , or if you need it then do the following

    <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/home_toolbar"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"// this value which removes the space 
            app:contentInsetLeft="0dp"
            >
    
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/nav_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:itemBackground="@android:color/holo_green_dark"
                app:labelVisibilityMode="unlabeled"
                app:menu="@menu/pop_up" />
    
    </androidx.appcompat.widget.Toolbar>