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

HideViewOnScrollBehavior.from(View) retrieves the behavior by reading view.getLayoutParams() and casting to CoordinatorLayout.LayoutParams. If the view's layout params are not CoordinatorLayout.LayoutParams, the view is not a direct child of a CoordinatorLayout and the lookup is invalid, so IllegalArgumentException is thrown.

Source

Thrown at lib/java/com/google/android/material/behavior/HideViewOnScrollBehavior.java:507

   * Returns whether or not this behavior is disabled if touch exploration is enabled.
   */
  public boolean isDisabledOnTouchExploration() {
    return disableOnTouchExploration;
  }

  /**
   * A utility function to get the {@link HideViewOnScrollBehavior} associated with the {@code
   * view}.
   *
   * @param view The {@link View} with {@link HideViewOnScrollBehavior}.
   * @return The {@link HideViewOnScrollBehavior} associated with the {@code view}.
   */
  @NonNull
  @SuppressWarnings("unchecked")
  public static <V extends View> HideViewOnScrollBehavior<V> from(@NonNull V view) {
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (!(params instanceof LayoutParams)) {
      throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
    }
    CoordinatorLayout.Behavior<?> behavior = ((LayoutParams) params).getBehavior();
    if (!(behavior instanceof HideViewOnScrollBehavior)) {
      throw new IllegalArgumentException(
          "The view is not associated with HideViewOnScrollBehavior");
    }
    return (HideViewOnScrollBehavior<V>) behavior;
  }
}

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Ensure the view is a direct child of a CoordinatorLayout with app:layout_behavior set on it.
  2. Call from() after layout inflation/attachment (e.g. after setContentView or in onViewCreated), not before.
  3. If the view must live in another container, move it under the CoordinatorLayout or restructure so the behavior applies to a direct child.

Example fix

<!-- before -->
<FrameLayout ...>
  <androidx.appcompat.widget.Toolbar ... />
</FrameLayout>

<!-- after -->
<androidx.coordinatorlayout.widget.CoordinatorLayout ...>
  <androidx.appcompat.widget.Toolbar
      app:layout_behavior="com.google.android.material.beh.HideViewOnScrollBehavior"
      ... />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Defensive patterns

Strategy: type-guard

Type guard

private static boolean isCoordinatorChild(View v) {
  return v.getLayoutParams() instanceof CoordinatorLayout.LayoutParams;
}

Prevention

When it happens

Trigger: Calling HideViewOnScrollBehavior.from(view) on a view whose parent is not a CoordinatorLayout — e.g. a view inside a FrameLayout, ConstraintLayout, or a view not yet attached (default ViewGroup.LayoutParams).

Common situations: Calling from() in onCreate() before the view is inflated/attached so its LayoutParams are still the generic default; refactoring layouts so the FAB or toolbar is no longer a direct CoordinatorLayout child; calling from() on a view from a different fragment's hierarchy.

Related errors


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