Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than 3 items in bottom navigation bar android

I am new to android and I am trying to make an app with more than 3 elements in the bottom navigation bar. I am able to display them but they are getting clustered at the end and only three are visible properly. Here is my code:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="15dp"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_nav_items" />

Here is the image of the view: This is the snapshot

I am stuck please help..

like image 228
DonJon Avatar asked Feb 15 '17 00:02

DonJon


1 Answers

You can use below method for not getting clustered menu items. You have to call this method in onCreate method passing BottomNavigationView.

// Method for disabling ShiftMode of BottomNavigationView
private void disableShiftMode(BottomNavigationView view) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
    try {
        Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
        shiftingMode.setAccessible(true);
        shiftingMode.setBoolean(menuView, false);
        shiftingMode.setAccessible(false);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
            item.setShiftingMode(false);
            // set once again checked value, so view will be updated
            item.setChecked(item.getItemData().isChecked());
        }
    } catch (NoSuchFieldException e) {
        Log.e("BNVHelper", "Unable to get shift mode field", e);
    } catch (IllegalAccessException e) {
        Log.e("BNVHelper", "Unable to change value of shift mode", e);
    }
}
like image 74
Maulik Dodia Avatar answered Sep 21 '22 03:09

Maulik Dodia