{"record":{"id":"9193a520d53d939d","repo":"material-components/material-components-android","slug":"attempted-to-set-shapeappearance-on-a-materialbutt","errorCode":null,"errorMessage":"Attempted to set ShapeAppearance on a MaterialButton which has an overwritten background.","messagePattern":"Attempted to set ShapeAppearance on a MaterialButton which has an overwritten background\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/button/MaterialButton.java","lineNumber":1892,"sourceCode":"  }\n\n  /**\n   * Sets the {@link ShapeAppearance} used for this {@link MaterialButton}'s original\n   * drawables.\n   *\n   * @throws IllegalStateException if the MaterialButton's background has been overwritten.\n   * @hide\n   */\n  @RestrictTo(LIBRARY_GROUP)\n  public void setShapeAppearance(\n      @NonNull ShapeAppearance shapeAppearance) {\n    if (isUsingOriginalBackground()) {\n      if (materialButtonHelper.getCornerSpringForce() == null && shapeAppearance.isStateful()) {\n        materialButtonHelper.setCornerSpringForce(createSpringForce());\n      }\n      materialButtonHelper.setShapeAppearance(shapeAppearance);\n    } else {\n      throw new IllegalStateException(\n          \"Attempted to set ShapeAppearance on a MaterialButton which has an\"\n              + \" overwritten background.\");\n    }\n  }\n\n  /**\n   * Returns the {@link ShapeAppearance} used for this {@link MaterialButton}'s\n   * original drawables.\n   *\n   * <p>This {@link ShapeAppearance} can be modified to change the component's shape.\n   *\n   * @throws IllegalStateException if the MaterialButton's background has been overwritten.\n   * @hide\n   */\n  @NonNull\n  @RestrictTo(LIBRARY_GROUP)\n  public ShapeAppearance getShapeAppearance() {\n    if (isUsingOriginalBackground()) {","sourceCodeStart":1874,"sourceCodeEnd":1910,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/button/MaterialButton.java#L1874-L1910","documentation":"setShapeAppearance(ShapeAppearance) is the @RestrictTo(LIBRARY_GROUP) programmatic variant that can install a stateful shape (optionally wiring a corner SpringForce for animated corners). Like the public setShapeAppearanceModel, it only works while the button still uses its original material background; otherwise it throws IllegalStateException (MaterialButton.java:1892). Because it is library-group restricted, hitting it as an app developer usually means another Material component or a copy of library internals is calling it on your behalf.","triggerScenarios":"Internal library code (or app code calling the restricted API, e.g. via reflection or a fork) invoking setShapeAppearance(shapeAppearance) on a MaterialButton whose background was replaced via android:background or setBackground(). A stateful ShapeAppearance additionally triggers creation of a corner spring force before the helper applies it.","commonSituations":"Using a library fork or bleeding-edge snapshot where a component (e.g. a button group or split button reshaping children) calls setShapeAppearance on child buttons that a theme or developer style gave a custom android:background. Differs from error 21 only in being the restricted/annotated entry point.","solutions":["Remove the android:background / setBackground override on the affected button so the original background is preserved.","Reproduce with the stable released library version instead of a fork; if the crash comes from library-internal calls, file an issue with the fork/snapshot and your XML.","As a last resort in a fork, guard internal setShapeAppearance calls with isUsingOriginalBackground() checks."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (button.getBackground() instanceof MaterialShapeDrawable) {\n  button.setShapeAppearance(shapeAppearance);\n}","typeGuard":"static boolean canApplyShapeAppearance(MaterialButton b) {\n  return b.getBackground() instanceof MaterialShapeDrawable;\n}","tryCatchPattern":"try { button.setShapeAppearance(shapeAppearance); } catch (IllegalStateException e) { Log.w(TAG, \"skipping shape apply on overwritten background\"); }","preventionTips":["Treat @RestrictTo(LIBRARY_GROUP) APIs as off-limits in app code; use the public setShapeAppearanceModel.","In forks, wrap every internal setShapeAppearance call with a background-type check.","Reproduce against stable releases to isolate fork/snapshot-only crashes."],"tags":["android","material-components","materialbutton","shape","restricted-api","background"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}