material-components/material-components-android · error · IllegalStateException
Attempted to get ShapeAppearanceModel from a MaterialButton
Error message
Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten background.
What it means
getShapeAppearanceModel() is the read counterpart of setShapeAppearanceModel(): it returns the ShapeAppearanceModel of the button's original shape drawable. When the background was replaced (isUsingOriginalBackground() false), no such model exists, so MaterialButton throws IllegalStateException (MaterialButton.java:1870) rather than returning null or a fabricated default. This keeps the get/set API symmetric — both fail on an overwritten background.
Source
Thrown at lib/java/com/google/android/material/button/MaterialButton.java:1870
+ " background.");
}
}
/**
* Returns the {@link ShapeAppearanceModel} used for this {@link MaterialButton}'s original
* drawables.
*
* <p>This {@link ShapeAppearanceModel} can be modified to change the component's shape.
*
* @throws IllegalStateException if the MaterialButton's background has been overwritten.
*/
@NonNull
@Override
public ShapeAppearanceModel getShapeAppearanceModel() {
if (isUsingOriginalBackground()) {
return materialButtonHelper.getShapeAppearanceModel();
} else {
throw new IllegalStateException(
"Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten"
+ " background.");
}
}
/**
* Sets the {@link ShapeAppearance} used for this {@link MaterialButton}'s original
* drawables.
*
* @throws IllegalStateException if the MaterialButton's background has been overwritten.
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public void setShapeAppearance(
@NonNull ShapeAppearance shapeAppearance) {
if (isUsingOriginalBackground()) {
if (materialButtonHelper.getCornerSpringForce() == null && shapeAppearance.isStateful()) {
materialButtonHelper.setCornerSpringForce(createSpringForce());View on GitHub (pinned to ac7e18efee)
Solutions
- Remove the android:background override (use app:backgroundTint, app:strokeColor, app:strokeWidth, app:shapeAppearance instead) so the original background survives.
- Make the utility defensive: check the button's background is a MaterialShapeDrawable or wrap the call in try/catch IllegalStateException before falling back to a fixed corner radius.
- If a custom drawable is unavoidable, track the intended shape separately (e.g. in the style/tag) instead of reading it from the button.
Example fix
// before
ShapeAppearanceModel model = button.getShapeAppearanceModel(); // throws if background overwritten
// after
if (button.getBackground() instanceof MaterialShapeDrawable) {
ShapeAppearanceModel model = button.getShapeAppearanceModel();
button.setShapeAppearanceModel(model.toBuilder().setAllCornerSizes(24f).build());
} else {
button.setBackground(createRoundedBackground(24f));
} Defensive patterns
Strategy: type-guard
Validate before calling
if (button.getBackground() instanceof MaterialShapeDrawable) {
ShapeAppearanceModel model = button.getShapeAppearanceModel();
} Type guard
static boolean hasOriginalShape(Button b) {
return b.getBackground() instanceof MaterialShapeDrawable;
} Try / catch
try { return button.getShapeAppearanceModel(); } catch (IllegalStateException e) { return ShapeAppearanceModel.builder().setAllCornerSizes(0).build(); } Prevention
- Never read the shape model of buttons inflated from arbitrary layouts without a background-type check.
- Keep custom backgrounds out of MaterialButton styles.
- Store intended corner radii in the style/tag rather than reverse-engineering them from the view.
When it happens
Trigger: Calling button.getShapeAppearanceModel() (e.g. to read current corner sizes before modifying them) on a MaterialButton inflated with android:background set, or after code called setBackground/setBackgroundResource on it.
Common situations: Utility code that walks a view hierarchy and 'reads then tweaks' every MaterialButton's corners. Team A ships a themed button style with a custom android:background; team B's corner-customization helper calls getShapeAppearanceModel() and crashes at runtime only for those styles.
Related errors
- Attempted to set ShapeAppearanceModel on a MaterialButton wh
- Attempted to set ShapeAppearance on a MaterialButton which h
- Attempted to get ShapeAppearance from a MaterialButton which
- 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/3f481e2c5b5f611b.
Report an issue: GitHub.