Android BottomAppBar Distribute Buttons horizontally

2.3k Views Asked by At

I want to use BottomAppBar without the Navigation drawer control and Floating Action Button. When The buttons are added they are justified to right or left.

enter image description here

  • I want to distribute the action icons horizontally. I couldn't find a solution around and in the Android developer reference. How is it possible to achieve this?
2

There are 2 best solutions below

7
On BEST ANSWER

NOTE

if you want remove Navigation drawer control just remove app:navigationIcon="@drawable/ic_drawer" from BottomAppBar

output when your removed app:navigationIcon="@drawable/ic_drawer"

SAMPLE CODE

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_gravity="bottom">


    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorPrimaryDark"
        app:navigationIcon="@drawable/ic_drawer">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:orientation="horizontal">


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

        </LinearLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

OUTPUT

enter image description here

1
On

Remove the notification icon by removing the line:

    app:navigationIcon="@drawable/ic_drawer"

From

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:layout_gravity="bottom"
    android:backgroundTint="@color/colorPrimaryDark"
    app:navigationIcon="@drawable/ic_drawer">

Then you can add left and right padding in bottomappbar:

<android.support.design.bottomappbar.BottomAppBar
        android:theme="@style/Widget.MaterialComponents.BottomAppBar"
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:paddingStart="40dp"
        android:paddingLeft="40dp"
        android:paddingEnd="40dp"
        android:paddingRight="40dp"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"/>

That will center the four icons in the middle of the bottomAppBar.

Last step is to add padding/margins on the icons/imageviews and you should have yourself a nice even horizontally distributed four icons. I think the accepted answer doesn't quite achieve this.