Home » Android » android – BottomNavigationView is not full width

android – BottomNavigationView is not full width

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am working on an Android app and implementing BottomNavigationView from the design library. I have looked at many examples and I can’t figure out what is wrong with my layout. The BottomNavigationView is not displayed as full width.

Another issue is the status bar color is not getting applied.

enter image description here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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="match_parent">

<!-- Include the toolbar -->
<include layout="@layout/toolbar"/>

<RelativeLayout android:id="@+id/fragment_container"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/bottom_navigation_main"/>

</android.support.design.widget.CoordinatorLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
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="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

Edit:
Added solution for Status Color not being set

android:fitsSystemWindows="true"

(colorPrimaryDark) status bar color not working on android v21?

How to&Answers:

The BottomNavigationView is not displayed as full width.

It is not supposed to.

According to design guidelines the width of an action can vary between 80dp and 168dp. The two actions you have defined are not sufficient to fill the whole area horizontally.
(As a side note, also according to guidelines the view should contain between three and five actions.)

If you want to fill the space behind the BottomNavigationView, you can set the background color of the view to be the same color as the items background:

<android.support.design.widget.BottomNavigationView
     android:background="@color/bottom_view_color"
     app:itemBackground="@color/bottom_view_color"

     // .... />

Answer:

That is doable. But it will againts default design specs, and I will suggest you to go with default design specs.


Now coming to my solution…

Define below dimensions into dimens.xml. These dimensions should be in values, values-large, values-large-land. And 600dp can be increased to 1000dp or more in values-large, values-large-land, if in tablet you are not seeing this change.

<resources xmlns:tools="http://schemas.android.com/tools">
    <dimen name="design_bottom_navigation_item_max_width" tools:override="true">600dp</dimen>
    <dimen name="design_bottom_navigation_active_item_max_width" tools:override="true">600dp</dimen>
</resources>

And thats it!!! Result will be like enter image description here


It’s not a mazic.

Why dimensions has been added with such name and value is 600dp

Both dimensions is being used by BottomNavigationMenuView.java (which is the class being used to represent menu in BottomNavigationView).
Below is code

public BottomNavigationMenuView(Context context, AttributeSet attrs) {
    super(context, attrs);
    ...
    mInactiveItemMaxWidth = res.getDimensionPixelSize(
            R.dimen.design_bottom_navigation_item_max_width);
    ....
    mActiveItemMaxWidth = res.getDimensionPixelSize(
            R.dimen.design_bottom_navigation_active_item_max_width);
    .....
}

Now these values is being used to create view with fixed width, as below

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    ....
    if (mShiftingMode) {
        final int inactiveCount = count - 1;
        final int activeMaxAvailable = width - inactiveCount * mInactiveItemMinWidth;
        final int activeWidth = Math.min(activeMaxAvailable, mActiveItemMaxWidth);
        final int inactiveMaxAvailable = (width - activeWidth) / inactiveCount;
        final int inactiveWidth = Math.min(inactiveMaxAvailable, mInactiveItemMaxWidth);
        ...
    } else {
        final int maxAvailable = width / count;
        final int childWidth = Math.min(maxAvailable, mActiveItemMaxWidth);
        .....
    }
    ....
}

To use value of activeMaxAvailable always, I set a dummy value to mActiveItemMaxWidth (in dimens above). So activeWidth will have
value of activeMaxAvailable. Same rule apply for inactiveWidth.

So when you build project, design_bottom_navigation_item_max_width and design_bottom_navigation_active_item_max_width defined
into design-support-lib, will be replaced by dimensions defined by us.

Code verified on maximum supported options (5 max) also.

Answer:

I suggest to only use

android:background="@color/bottom_view_color"

It will display the bar as full width and it will keep the ripple effect when the user clicks the items.

If you also add app:itemBackground you will lose the ripple effect.

In your case

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