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

  1. Stop overriding android:background on MaterialButtons; express the look with app:backgroundTint, app:strokeColor, app:rippleColor and app:shapeAppearance attributes.
  2. If the crash originates in library/fork code, report it upstream and temporarily remove the custom background on affected buttons.
  3. 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

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


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