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

ExpandableBehavior.from(view, klass) (ExpandableBehavior.java:169) retrieves a transformation behavior attached to a view. It first checks whether the view's LayoutParams are CoordinatorLayout.LayoutParams; since behaviors are stored on CoordinatorLayout child params, a view that is not a direct child of a CoordinatorLayout cannot carry a behavior, and the method throws IllegalArgumentException immediately.

Source

Thrown at lib/java/com/google/android/material/transformation/ExpandableBehavior.java:169

      return currentState == STATE_UNINITIALIZED || currentState == STATE_COLLAPSED;
    } else {
      // Can only collapse from expanded state. Uninitialized is equivalent to collapsed state.
      return currentState == STATE_EXPANDED;
    }
  }

  /**
   * A utility function to get the {@link ExpandableBehavior} attached to the {@code view}.
   *
   * @param view The {@link View} that the {@link ExpandableBehavior} is attached to.
   * @param klass The expected {@link Class} of the attached {@link ExpandableBehavior}.
   * @return The {@link ExpandableBehavior} attached to the {@code view}.
   */
  @Nullable
  public static <T extends ExpandableBehavior> T from(@NonNull View view, @NonNull Class<T> klass) {
    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 ExpandableBehavior)) {
      throw new IllegalArgumentException("The view is not associated with ExpandableBehavior");
    }
    return klass.cast(behavior);
  }
}

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Move the view so it is a direct child of the CoordinatorLayout that also hosts the FAB/dependency.
  2. Ensure from() is called only after the view is attached (e.g. in onAttachedToWindow or after setContentView/inflation completes).
  3. If the view must live elsewhere, restructure so the behavior is attached to a CoordinatorLayout child (e.g. an intermediate wrapper inside the CoordinatorLayout).

Example fix

<!-- before -->
<androidx.coordinatorlayout.widget.CoordinatorLayout ...>
  <com.google.android.material.floatingactionbutton.FloatingActionButton ... />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<LinearLayout ...>
  <View android:id="+id/expandable_card" ... /> <!-- from() crashes -->
</LinearLayout>

<!-- after -->
<androidx.coordinatorlayout.widget.CoordinatorLayout ...>
  <com.google.android.material.floatingactionbutton.FloatingActionButton ... />
  <View android:id="+id/expandable_card"
      app:layout_behavior="...ExpandableTransformationBehavior" ... />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Defensive patterns

Strategy: validation

Validate before calling

boolean isCoordinatorChild(View view) {
  return view.getLayoutParams() instanceof CoordinatorLayout.LayoutParams;
}
// guard:
if (!isCoordinatorChild(v)) {
  throw new IllegalStateException(v + " must be a direct child of CoordinatorLayout");
}

Type guard

static boolean canResolveExpandableBehavior(View v) {
  ViewGroup.LayoutParams p = v.getLayoutParams();
  if (!(p instanceof CoordinatorLayout.LayoutParams)) return false;
  return ((CoordinatorLayout.LayoutParams) p).getBehavior() instanceof ExpandableBehavior;
}

Prevention

When it happens

Trigger: Calling ExpandableTransformationBehavior.from() (or subclasses) on a view inflated into a FrameLayout/ConstraintLayout/RecyclerView; calling from() on a view before it is attached/added to the CoordinatorLayout so getLayoutParams() returns a generic ViewGroup.LayoutParams.

Common situations: Using ExpansionLayout/FAB transformation patterns where the dependent view (e.g. a toolbar or card meant to expand from a FAB) sits in a different layout container than the CoordinatorLayout; refactoring a layout from CoordinatorLayout to ConstraintLayout while keeping the java call; calling from() in onCreate before the view is added to the hierarchy.

Related errors


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