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

The wrap overflow mode is not compatible with wrap_content l

Error message

The wrap overflow mode is not compatible with wrap_content layout width.

What it means

In wrap overflow mode, MaterialButtonGroup.onMeasure (MaterialButtonGroup.java:386) must know the width available to decide which buttons wrap. A wrap_content width gives the parent an AT_MOST measure spec, which makes the available width self-referential (the group's width depends on the children, whose wrapping depends on the width). The group therefore throws IllegalArgumentException when widthMeasureSpec mode is AT_MOST while overflowMode is OVERFLOW_MODE_WRAP.

Source

Thrown at lib/java/com/google/android/material/button/MaterialButtonGroup.java:386

  private void recoverAllChildrenLayoutParams() {
    for (int i = 0; i < getChildCount(); i++) {
      MaterialButton child = getChildButton(i);
      child.recoverOriginalLayoutParams();
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    adjustChildMarginsAndUpdateLayout();
    int wrappedHeight = 0;
    if (overflowMode == OVERFLOW_MODE_WRAP) {
      if (getOrientation() == VERTICAL) {
        throw new IllegalArgumentException(
            "The wrap overflow mode is not compatible to the vertical orientation.");
      }
      if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
        throw new IllegalArgumentException(
            "The wrap overflow mode is not compatible with wrap_content layout width.");
      }
      wrappedHeight = maybeWrapButtons(widthMeasureSpec, heightMeasureSpec);
    }
    maybeUpdateOverflowMenu(widthMeasureSpec, heightMeasureSpec);
    updateChildShapes();
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (overflowMode == OVERFLOW_MODE_WRAP && wrappedHeight != getMeasuredHeight()) {
      setMeasuredDimension(getMeasuredWidth(), wrappedHeight);
    }
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    if (changed) {
      recoverAllChildrenLayoutParams();
      adjustChildSizeChange();

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Give the group a bounded width: android:layout_width="match_parent" (with a bounded parent) or a fixed dp value such as 0dp with constraints in ConstraintLayout.
  2. Ensure no ancestor re-measures the group with AT_MOST — avoid wrap_content parents around it.
  3. If the width genuinely must be content-driven, drop overflowMode=wrap and use the default overflow-menu mode.

Example fix

<!-- before -->
<com.google.android.material.button.MaterialButtonGroup
    android:layout_width="wrap_content"
    app:overflowMode="wrap" />

<!-- after -->
<com.google.android.material.button.MaterialButtonGroup
    android:layout_width="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:overflowMode="wrap" />
Defensive patterns

Strategy: validation

Validate before calling

// In XML, ensure a bounded width; programmatically confirm parent is not wrap_content before enabling:
ViewParent p = group.getParent();
if (!(p instanceof Viewparent) || ((View) p).getLayoutParams().width != ViewGroup.LayoutParams.WRAP_CONTENT) {
  group.setOverflowMode(MaterialButtonGroup.OVERFLOW_MODE_WRAP);
}

Prevention

When it happens

Trigger: Declaring the MaterialButtonGroup with android:layout_width="wrap_content" together with app:overflowMode="wrap". The throw happens during onMeasure, i.e. at first layout/inflation, and also occurs if a parent (e.g. a ConstraintLayout chain or a centered LinearLayout) passes an AT_MOST width spec even when layout_width is match_parent.

Common situations: Wrapping the group in a parent that measures children with AT_MOST (wrap_content LinearLayout, dialog custom content, some RecyclerView item roots). The developer sees a crash at layout time with no obvious link to their layout_width, because any ancestor that forwards an AT_MOST spec triggers it.

Related errors


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