material-components/material-components-android · error · IllegalArgumentException
The view is not a child of CoordinatorLayout
Error message
The view is not a child of CoordinatorLayout
What it means
BottomSheetBehavior.from(View) retrieves the behavior attached to a view by casting its ViewGroup.LayoutParams to CoordinatorLayout.LayoutParams. If the view's layout params are not that type, the view is not a direct child of a CoordinatorLayout and from() throws IllegalArgumentException.
Source
Thrown at lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java:2467
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
/**
* A utility function to get the {@link BottomSheetBehavior} associated with the {@code view}.
*
* @param view The {@link View} with {@link BottomSheetBehavior}.
* @return The {@link BottomSheetBehavior} associated with the {@code view}.
*/
@NonNull
@SuppressWarnings("unchecked")
public static <V extends View> BottomSheetBehavior<V> from(@NonNull V view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (!(params instanceof CoordinatorLayout.LayoutParams)) {
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
}
CoordinatorLayout.Behavior<?> behavior =
((CoordinatorLayout.LayoutParams) params).getBehavior();
if (!(behavior instanceof BottomSheetBehavior)) {
throw new IllegalArgumentException("The view is not associated with BottomSheetBehavior");
}
return (BottomSheetBehavior<V>) behavior;
}
/**
* Sets whether the BottomSheet should update the accessibility status of its {@link
* CoordinatorLayout} siblings when expanded.
*
* <p>Set this to true if the expanded state of the sheet blocks access to siblings (e.g., when
* the sheet expands over the full screen).
*/
public void setUpdateImportantForAccessibilityOnSiblings(
boolean updateImportantForAccessibilityOnSiblings) {View on GitHub (pinned to ac7e18efee)
Solutions
- Make the sheet a direct child of the CoordinatorLayout with app:layout_behavior="...BottomSheetBehavior".
- Call from() only after the view is inflated and attached (onCreateOptionsMenu of the fragment / after setContentView).
- If wrapping is unavoidable, apply the behavior to the outermost direct child instead and query that view.
Example fix
<!-- before -->
<androidx.coordinatorlayout.widget.CoordinatorLayout ...>
<FrameLayout ...>
<LinearLayout android:id="+id/bottom_sheet" ... /> <!-- not a direct child -->
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!-- after -->
<androidx.coordinatorlayout.widget.CoordinatorLayout ...>
<LinearLayout
android:id="+id/bottom_sheet"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
... />
</androidx.coordinatorlayout.widget.CoordinatorLayout> Defensive patterns
Strategy: type-guard
Type guard
private static boolean isCoordinatorChild(View v) {
return v.getLayoutParams() instanceof CoordinatorLayout.LayoutParams;
}
// usage: if (isCoordinatorChild(sheet)) BottomSheetBehavior.from(sheet); Prevention
- Keep the bottom sheet a direct child of CoordinatorLayout.
- Query with from() after the view hierarchy is attached.
When it happens
Trigger: Calling BottomSheetBehavior.from(bottomSheet) where bottomSheet is inside a NestedScrollView/FrameLayout wrapper, a different parent container, or not yet attached to the hierarchy (generic LayoutParams).
Common situations: The sheet view is wrapped in another container for layout reasons; calling from() before the view is laid out (e.g. in Activity.onCreate before setContentView completes, or on a programmatically created view not yet added to the CoordinatorLayout); accessing a sheet from the wrong fragment's view.
Related errors
- The view is not a child of CoordinatorLayout
- The view is not associated with BottomSheetBehavior
- The view is not associated with HideViewOnScrollBehavior
- ratio must be a float value between 0 and 1
- offset must be greater than or equal to 0
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/564798bb5f9131d7.
Report an issue: GitHub.