{"record":{"id":"fb5abd53f3dc91b9","repo":"material-components/material-components-android","slug":"the-layout-of-this-snackbar-does-not-include-a-clo","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/snackbar/Snackbar.java","lineNumber":640,"sourceCode":"    }\n  }\n\n  private TextView getMessageView() {\n    return getContentLayout().getMessageView();\n  }\n\n  private Button getActionView() {\n    return getContentLayout().getActionView();\n  }\n\n  private SnackbarContentLayout getContentLayout() {\n    return (SnackbarContentLayout) view.getChildAt(0);\n  }\n\n  @NonNull\n  private MaterialButton getCloseViewOrThrow() {\n    if (!(getContentLayout().getCloseView() instanceof MaterialButton)) {\n      throw new IllegalStateException(\n          \"The layout of this snackbar does not include a close MaterialButton. This might be\"\n              + \" because the context's theme is not a Material 3 or later theme or because the\"\n              + \" Snackbar's layout has been replaced with a custom layout.\");\n    }\n    return (MaterialButton) getContentLayout().getCloseView();\n  }\n\n  private SnackbarBaseLayout getSnackbarLayout() {\n    return view;\n  }\n}\n","sourceCodeStart":622,"sourceCodeEnd":652,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/snackbar/Snackbar.java#L622-L652","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Make the hosting activity/fragment theme a Material 3 theme (Theme.Material3.*) or apply an M3 snackbar overlay.","If you must stay on M2, do not call close-icon APIs; remove setCloseIcon* calls.","If using a custom snackbar layout, include a close MaterialButton or stop calling these APIs."],"exampleFix":"// before\n<style name=\"AppTheme\" parent=\"Theme.MaterialComponents.DayNight.DarkActionBar\" />\n// ... Snackbar.make(...).setCloseIconVisible(true) -> crashes\n\n// after\n<style name=\"AppTheme\" parent=\"Theme.Material3.DayNight\" />","handlingStrategy":"validation","validationCode":"private static boolean isMaterial3Theme(@NonNull Context context) {\n  TypedValue a = new TypedValue();\n  return context.getTheme().resolveAttribute(com.google.android.material.R.attr.isMaterial3Theme, a, false) && a.data != 0;\n}\n// before close-icon calls: if (isMaterial3Theme(ctx)) snackbar.setCloseIconVisible(true);","typeGuard":null,"tryCatchPattern":"try { snackbar.setCloseIconVisible(true); } catch (IllegalStateException e) { /* non-M3 theme: skip close icon */ }","preventionTips":["Standardize on Theme.Material3 for screens using M3 snackbar APIs.","Do not override the snackbar content layout without keeping a close MaterialButton.","Gate M3-only calls behind a theme capability check."],"tags":["snackbar","material3","theming","ui"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}