material-components/material-components-android · error · IllegalArgumentException

Maximum number of items supported by %s is %d. Limit can be

Error message

Maximum number of items supported by %s is %d. Limit can be checked with %s#getMaxItemCount()

What it means

NavigationBarMenu.addInternal enforces maxItemCount (5 for BottomNavigationView per Material spec) and throws IllegalArgumentException when adding one more item would exceed it. The message names the view class, the limit, and points to getMaxItemCount() so callers can check programmatically.

Source

Thrown at lib/java/com/google/android/material/navigation/NavigationBarMenu.java:83

  @Override
  public SubMenu addSubMenu(int group, int id, int categoryOrder, @NonNull CharSequence title) {
    if (!subMenuSupported) {
      throw new UnsupportedOperationException(
          viewClass.getSimpleName() + " does not support submenus");
    }
    final MenuItemImpl item = (MenuItemImpl) addInternal(group, id, categoryOrder, title);
    final SubMenuBuilder subMenu = new NavigationBarSubMenu(getContext(), this, item);
    item.setSubMenu(subMenu);
    return subMenu;
  }

  @Override
  @NonNull
  protected MenuItem addInternal(
      int group, int id, int categoryOrder, @NonNull CharSequence title) {
    if (size() + 1 > maxItemCount) {
      String viewClassName = viewClass.getSimpleName();
      throw new IllegalArgumentException(
          "Maximum number of items supported by "
              + viewClassName
              + " is "
              + maxItemCount
              + ". Limit can be checked with "
              + viewClassName
              + "#getMaxItemCount()");
    }
    stopDispatchingItemsChanged();
    final MenuItem item = super.addInternal(group, id, categoryOrder, title);
    startDispatchingItemsChanged();
    return item;
  }
}

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Reduce menu items to at most view.getMaxItemCount() (5 by default)
  2. Move overflow items into a 'More' sheet/dialog or the app's primary navigation elsewhere
  3. When building the menu dynamically, truncate or validate the source list before adding

Example fix

// before
val items = listOf("A", "B", "C", "D", "E", "F")
items.forEach { bottomNav.menu.add(it) }
// after
val items = listOf("A", "B", "C", "D", "E", "F")
check(items.size <= bottomNav.maxItemCount) { "Too many bottom nav items" }
items.take(bottomNav.maxItemCount).forEach { bottomNav.menu.add(it) }
Defensive patterns

Strategy: validation

Validate before calling

fun safeAddItems(nav: NavigationBarView, items: List<MenuEntry>) {
    val max = nav.maxItemCount // BottomNavigationView: 5
    items.take(max).forEach { entry ->
        nav.menu.add(0, entry.id, Menu.NONE, entry.title)
    }
}

Try / catch

catch (e: IllegalArgumentException) { Log.e(TAG, "Too many nav items; truncating", e); rebuildMenuCapped() }

Prevention

When it happens

Trigger: Inflating or programmatically adding more than getMaxItemCount() items (typically 5) to a BottomNavigationView/NavigationBarView menu; e.g. a menu XML with 6 top-level <item> entries.

Common situations: Product adds a sixth tab; dynamically building the menu from a data-driven list without capping; reusing a drawer menu with many items in bottom navigation.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/e37fbe5fe9a4d6c5. Report an issue: GitHub.