material-components/material-components-android · error · IllegalStateException
Attempted to get ShapeAppearance from a MaterialButton which
Error message
Attempted to get ShapeAppearance from a MaterialButton which has an overwritten background.
What it means
getShapeAppearance() is the @RestrictTo(LIBRARY_GROUP) getter that returns the ShapeAppearance of the button's original drawables. Symmetric with the other shape accessors, it throws IllegalStateException (MaterialButton.java:1913) when the button's background has been overwritten, because no original shape exists to read. App developers normally hit this only indirectly through library-internal code paths.
Source
Thrown at lib/java/com/google/android/material/button/MaterialButton.java:1913
}
}
/**
* Returns the {@link ShapeAppearance} used for this {@link MaterialButton}'s
* original drawables.
*
* <p>This {@link ShapeAppearance} can be modified to change the component's shape.
*
* @throws IllegalStateException if the MaterialButton's background has been overwritten.
* @hide
*/
@NonNull
@RestrictTo(LIBRARY_GROUP)
public ShapeAppearance getShapeAppearance() {
if (isUsingOriginalBackground()) {
return materialButtonHelper.getShapeAppearance();
} else {
throw new IllegalStateException(
"Attempted to get ShapeAppearance from a MaterialButton which has an"
+ " overwritten background.");
}
}
/**
* Sets the corner spring force for this {@link MaterialButton}.
*
* @param springForce The new {@link SpringForce} object.
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public void setCornerSpringForce(@NonNull SpringForce springForce) {
materialButtonHelper.setCornerSpringForce(springForce);
}
/**
* Returns the corner spring force for this {@link MaterialButton}.View on GitHub (pinned to ac7e18efee)
Solutions
- Stop overriding android:background on MaterialButtons; express the look with app:backgroundTint, app:strokeColor, app:rippleColor and app:shapeAppearance attributes.
- If the crash originates in library/fork code, report it upstream and temporarily remove the custom background on affected buttons.
- In forks, guard internal getShapeAppearance() reads with isUsingOriginalBackground().
Defensive patterns
Strategy: type-guard
Validate before calling
if (button.getBackground() instanceof MaterialShapeDrawable) {
ShapeAppearance appearance = button.getShapeAppearance();
} Type guard
static boolean hasOriginalShapeAppearance(MaterialButton b) {
return b.getBackground() instanceof MaterialShapeDrawable;
} Try / catch
try { return button.getShapeAppearance(); } catch (IllegalStateException e) { return null; } Prevention
- Avoid restricted APIs in app code; use getShapeAppearanceModel().
- Guard any internal read with a MaterialShapeDrawable background check.
- Report fork-internal crashes upstream with the button's XML.
When it happens
Trigger: Library-internal or forked code calling getShapeAppearance() on a MaterialButton that had its background replaced (android:background in XML, or setBackground/setBackgroundResource at runtime).
Common situations: A button style in the app theme sets android:background (e.g. for a legacy 9-patch or gradient), and a Material component or fork reads the child button's shape to compute corner geometry, crashing only for those themed buttons. Mirror of error 22 through the restricted API.
Related errors
- Attempted to set ShapeAppearance on a MaterialButton which h
- Attempted to set ShapeAppearanceModel on a MaterialButton wh
- Attempted to get ShapeAppearanceModel from a MaterialButton
- secondaryIconGravity cannot have the same alignment as iconG
- Scroll bar must contain a child to calculate interpolation.
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/105ae4a906d06291.
Report an issue: GitHub.