8

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()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ayham Showa
  • 135
  • 1
  • 2
  • 10

3 Answers3

20

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.

Community
  • 1
  • 1
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
4

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.

Gergely Kőrössy
  • 5,620
  • 3
  • 28
  • 44
0

In my app I used BottomNavigationView with 2 item, but there are 4 items like this: duplicate items

My XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_navigation_constraintlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    tools:context=".BottomNavigation">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@android:color/holo_red_dark"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

and my code:

public class BottomNavigation extends AppCompatActivity {

    private static final String TAG = "BottomNavigation";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bottom_navigation);
            updateMenu();
        } catch (Exception e) {
            Log.e(TAG, "onCreate: ", e);;
        }
    }

    @UiThread
    private void updateMenu() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        if (bottomNavigationView != null) {
            bottomNavigationView.inflateMenu(R.menu.navigation);

            for (Integer i = 0;
                 i < bottomNavigationView.getMenu().size();
                 ++i   ) {
                Log.i(TAG, "updateMenu: " + bottomNavigationView.getMenu().getItem(i).getItemId());
            }
        }
    }
}

Delete either of them and it will work. thanks.

Languoguang
  • 2,166
  • 2
  • 10
  • 15