{"record":{"id":"121761b826e540b5","repo":"material-components/material-components-android","slug":"the-wrap-overflow-mode-is-not-compatible-with-wrap","errorCode":null,"errorMessage":"The wrap overflow mode is not compatible with wrap_content layout width.","messagePattern":"The wrap overflow mode is not compatible with wrap_content layout width\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/button/MaterialButtonGroup.java","lineNumber":386,"sourceCode":"\n  private void recoverAllChildrenLayoutParams() {\n    for (int i = 0; i < getChildCount(); i++) {\n      MaterialButton child = getChildButton(i);\n      child.recoverOriginalLayoutParams();\n    }\n  }\n\n  @Override\n  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {\n    adjustChildMarginsAndUpdateLayout();\n    int wrappedHeight = 0;\n    if (overflowMode == OVERFLOW_MODE_WRAP) {\n      if (getOrientation() == VERTICAL) {\n        throw new IllegalArgumentException(\n            \"The wrap overflow mode is not compatible to the vertical orientation.\");\n      }\n      if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {\n        throw new IllegalArgumentException(\n            \"The wrap overflow mode is not compatible with wrap_content layout width.\");\n      }\n      wrappedHeight = maybeWrapButtons(widthMeasureSpec, heightMeasureSpec);\n    }\n    maybeUpdateOverflowMenu(widthMeasureSpec, heightMeasureSpec);\n    updateChildShapes();\n    super.onMeasure(widthMeasureSpec, heightMeasureSpec);\n    if (overflowMode == OVERFLOW_MODE_WRAP && wrappedHeight != getMeasuredHeight()) {\n      setMeasuredDimension(getMeasuredWidth(), wrappedHeight);\n    }\n  }\n\n  @Override\n  protected void onLayout(boolean changed, int l, int t, int r, int b) {\n    super.onLayout(changed, l, t, r, b);\n    if (changed) {\n      recoverAllChildrenLayoutParams();\n      adjustChildSizeChange();","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/button/MaterialButtonGroup.java#L368-L404","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Ensure no ancestor re-measures the group with AT_MOST — avoid wrap_content parents around it.","If the width genuinely must be content-driven, drop overflowMode=wrap and use the default overflow-menu mode."],"exampleFix":"<!-- before -->\n<com.google.android.material.button.MaterialButtonGroup\n    android:layout_width=\"wrap_content\"\n    app:overflowMode=\"wrap\" />\n\n<!-- after -->\n<com.google.android.material.button.MaterialButtonGroup\n    android:layout_width=\"0dp\"\n    app:layout_constraintStart_toStartOf=\"parent\"\n    app:layout_constraintEnd_toEndOf=\"parent\"\n    app:overflowMode=\"wrap\" />","handlingStrategy":"validation","validationCode":"// In XML, ensure a bounded width; programmatically confirm parent is not wrap_content before enabling:\nViewParent p = group.getParent();\nif (!(p instanceof Viewparent) || ((View) p).getLayoutParams().width != ViewGroup.LayoutParams.WRAP_CONTENT) {\n  group.setOverflowMode(MaterialButtonGroup.OVERFLOW_MODE_WRAP);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always give wrap-mode groups a 0dp/match_parent/fixed width with a bounded ancestor chain.","Avoid wrap_content ancestors (centered LinearLayouts, dialog content) around the group.","Prefer ConstraintLayout with start/end constraints for the group."],"tags":["android","material-components","materialbuttongroup","layout","measure","xml-config"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}