material-components/material-components-android · error · IllegalStateException

The layout of this snackbar does not include a close Materia

Error message

The layout of this snackbar does not include a close MaterialButton. This might be because the context's theme is not a Material 3 or later theme or because the Snackbar's layout has been replaced with a custom layout.

What it means

Material 3 Snackbars can show a close (dismiss) icon button, and APIs that configure it call getCloseViewOrThrow(). That method expects the inflated snackbar content layout to contain a close MaterialButton, which only happens with a Material 3 theme (or an M3-styled snackbar layout). If the context theme is Material Components (M2) or the layout has been swapped for a custom one without a close button, it throws IllegalStateException.

Source

Thrown at lib/java/com/google/android/material/snackbar/Snackbar.java:640

    }
  }

  private TextView getMessageView() {
    return getContentLayout().getMessageView();
  }

  private Button getActionView() {
    return getContentLayout().getActionView();
  }

  private SnackbarContentLayout getContentLayout() {
    return (SnackbarContentLayout) view.getChildAt(0);
  }

  @NonNull
  private MaterialButton getCloseViewOrThrow() {
    if (!(getContentLayout().getCloseView() instanceof MaterialButton)) {
      throw new IllegalStateException(
          "The layout of this snackbar does not include a close MaterialButton. This might be"
              + " because the context's theme is not a Material 3 or later theme or because the"
              + " Snackbar's layout has been replaced with a custom layout.");
    }
    return (MaterialButton) getContentLayout().getCloseView();
  }

  private SnackbarBaseLayout getSnackbarLayout() {
    return view;
  }
}

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Make the hosting activity/fragment theme a Material 3 theme (Theme.Material3.*) or apply an M3 snackbar overlay.
  2. If you must stay on M2, do not call close-icon APIs; remove setCloseIcon* calls.
  3. If using a custom snackbar layout, include a close MaterialButton or stop calling these APIs.

Example fix

// before
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar" />
// ... Snackbar.make(...).setCloseIconVisible(true) -> crashes

// after
<style name="AppTheme" parent="Theme.Material3.DayNight" />
Defensive patterns

Strategy: validation

Validate before calling

private static boolean isMaterial3Theme(@NonNull Context context) {
  TypedValue a = new TypedValue();
  return context.getTheme().resolveAttribute(com.google.android.material.R.attr.isMaterial3Theme, a, false) && a.data != 0;
}
// before close-icon calls: if (isMaterial3Theme(ctx)) snackbar.setCloseIconVisible(true);

Try / catch

try { snackbar.setCloseIconVisible(true); } catch (IllegalStateException e) { /* non-M3 theme: skip close icon */ }

Prevention

When it happens

Trigger: Calling close-icon APIs such as setCloseIconVisible()/setCloseIcon() (or setAnchorView-adjacent M3 APIs that touch the close view) on a Snackbar created with a non-M3 activity theme, or after overriding the snackbar layout with a custom design_layout_snackbar.

Common situations: App theme is Theme.MaterialComponents.* while the code uses M3 snackbar APIs; snackbar layout replaced via theme overlays or reflection; library upgrade where new M3 methods were called on old themes.

Related errors


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