Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomNavigationView getmaxitemcount

Hello member stackoverflow porblem with bottomnavihationview

in my app I used BottomNavigationView with 4 item . it make my app easy and beauty

  BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);
        bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id){
                    case R.id.action_one:

                        break;
                    case R.id.action_two:
                        FragmentTransaction manger= getSupportFragmentManager().beginTransaction();
                        pop_web_view  pop3 =new pop_web_view();
                        pop3.show(manger,null);

                        break;
                    case R.id.action_three:

                        break;
                    case R.id.action_four:

                        break;
                }

                return false;
            }
        });

in activity_main :

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="bottom"
        android:paddingTop="560dp"
        app:itemBackground="@color/colorDivider"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorPrimaryDark"
        app:menu="@menu/menu_bottom_navigation" />

in menu xml :

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_one"
        android:icon="@android:drawable/ic_secure"
        android:title="One"/>
    <item
        android:id="@+id/action_two"
        android:icon="@android:drawable/ic_dialog_info"
        android:title="Two"/>
    <item
        android:id="@+id/action_three"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="Three"/>
    <item
        android:id="@+id/action_four"
        android:icon="@android:drawable/ic_popup_reminder"
        android:title="Four"/>
</menu>

BUT I have problem Caused by:

java.lang.IllegalArgumentException: Maximum number of items supported by BottomNavigationView is 5. Limit can be checked with BottomNavigationView#getMaxItemCount()
like image 982
Ayham Showa Avatar asked Feb 10 '17 03:02

Ayham Showa


People also ask

How to use BottomNavigationView in android?

BottomNavigationView makes it easy for users to explore and switch between top-level views in a single tap. There should be a minimum of 3 top-level views and a maximum of 5. If Destinations are more than 5 then use the Navigation Drawer. When the user taps on the icon it will change the top-level view accordingly.

What is BottomNavigationView?

com.google.android.material.bottomnavigation.BottomNavigationView. Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap.


2 Answers

The error said that maximum number of items supported by BottomNavigationView is 5.

And try to delete

bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);

because you are already inflating it in app:menu="@menu/menu_bottom_navigation"

And you are modifying it by calling

bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);

The documentation says Existing items in the menu will not be modified or removed.

Check this documention

And check this answer

Implementation of BottomNavigationView has condition: when there is more than 3 items then use shift mode.

like image 84
Kiran Benny Joseph Avatar answered Nov 15 '22 23:11

Kiran Benny Joseph


I know you accepted the current answer, but it's incomplete. You added a menu in your XML to the BottomNavigationView, then you try to call inflateMenu(...), however, the documentation clearly says:

Existing items in the menu will not be modified or removed.

This means that you are adding menu items to this view, not replacing them.

What you can do to fix it is the following:

bottomNavigationView.getMenu().clear();
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);

Also it's worth mentioning that you do the same things twice: once in the XML (by adding app:menu="..." attribute to the layout item), once in the Java class by calling the inflateMenu(...) method. Delete either of them and it will work. Keep in mind though that if you want to change the menu items later dynamically, you'll need to clear the existing items the way I posted.

like image 44
Gergely Kőrössy Avatar answered Nov 16 '22 00:11

Gergely Kőrössy