material-components/material-components-android · error · IllegalArgumentException

The view is not associated with HideViewOnScrollBehavior

Error message

The view is not associated with HideViewOnScrollBehavior

What it means

HideViewOnScrollBehavior.from(View) first verifies the view is a CoordinatorLayout child, then checks that the LayoutParams' behavior is a HideViewOnScrollBehavior. If a different behavior (or none) is attached, the cast is impossible and IllegalArgumentException is thrown with this message.

Source

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

  }

  /**
   * 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. Set app:layout_behavior="com.google.android.material.behavior.HideViewOnScrollBehavior" on the view.
  2. Confirm you are calling HideViewOnScrollBehavior.from on the view that actually carries this behavior (not another CoordinatorLayout child).
  3. Check for typos in the behavior class string in XML — a bad class name silently leaves the behavior unset.

Example fix

<!-- before -->
<View
    android:id="+id/hidable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> <!-- no behavior -->

<!-- after -->
<View
    android:id="+id/hidable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.google.android.material.behavior.HideViewOnScrollBehavior" />
Defensive patterns

Strategy: type-guard

Type guard

private static boolean hasHideOnScrollBehavior(View v) {
  return v.getLayoutParams() instanceof CoordinatorLayout.LayoutParams
      && ((CoordinatorLayout.LayoutParams) v.getLayoutParams()).getBehavior()
          instanceof HideViewOnScrollBehavior;
}

Prevention

When it happens

Trigger: Calling HideViewOnScrollBehavior.from(view) on a CoordinatorLayout child whose app:layout_behavior is missing, or set to another behavior such as AppBarLayout.Behavior, BottomSheetBehavior, or FloatingActionButton.Behavior.

Common situations: Copying a CoordinatorLayout child (e.g. a FAB) whose behavior attribute got dropped in the copy; using BottomSheetBehavior.from or other from() by mistake on the wrong view; behavior XML attribute typo so the class fails to resolve and no behavior is attached.

Related errors


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