material-components/material-components-android · error · IllegalArgumentException
invalid orientation
Error message
invalid orientation
What it means
CarouselOrientationHelper.createOrientationHelper (CarouselOrientationHelper.java:59) is the factory behind the layout manager's orientation handling: its switch has cases only for HORIZONTAL and VERTICAL, and the default falls through to throw IllegalArgumentException("invalid orientation"). It is defense-in-depth — setOrientation normally screens values first — so hitting it usually means the helper was called directly with a raw int, or validation order changed between versions.
Source
Thrown at lib/java/com/google/android/material/carousel/CarouselOrientationHelper.java:59
/**
* Creates an OrientationHelper for the given LayoutManager and orientation.
*
* @param layoutManager CarouselLayoutManager to attach to
* @param orientation Desired orientation. Should be {@link CarouselLayoutManager#HORIZONTAL} or
* {@link CarouselLayoutManager#VERTICAL}
* @return A new OrientationHelper
*/
static CarouselOrientationHelper createOrientationHelper(
CarouselLayoutManager layoutManager, @RecyclerView.Orientation int orientation) {
switch (orientation) {
case HORIZONTAL:
return createHorizontalHelper(layoutManager);
case VERTICAL:
return createVerticalHelper(layoutManager);
default: // fall out
}
throw new IllegalArgumentException("invalid orientation");
}
/** Returns the x-coordinate of the left edge of the parent recycler view. */
abstract int getParentLeft();
/**
* Returns the coordinate of the start edge of the parent recycler view accounting for layout
* direction. It returns the x-coordinate if horizontal and y-coordinate if vertical.
*/
abstract int getParentStart();
/** Returns the x-coordinate of the right edge of the parent recycler view. */
abstract int getParentRight();
/**
* Returns the coordinate of the end edge of the parent recycler view accounting for layout
* direction.
*/View on GitHub (pinned to ac7e18efee)
Solutions
- Route orientation through CarouselLayoutManager.setOrientation instead of calling the factory directly.
- Validate the int against HORIZONTAL/VERTICAL before any direct factory use.
- In tests, restrict parameterized orientation values to the two valid constants.
Example fix
// before
CarouselOrientationHelper.createOrientationHelper(layoutManager, someInt); // throws for 2
// after
if (someInt == RecyclerView.HORIZONTAL || someInt == RecyclerView.VERTICAL) {
layoutManager.setOrientation(someInt);
} Defensive patterns
Strategy: validation
Validate before calling
if (orientation == RecyclerView.HORIZONTAL || orientation == RecyclerView.VERTICAL) {
layoutManager.setOrientation(orientation);
} Type guard
static boolean isValidOrientation(int o) {
return o == RecyclerView.HORIZONTAL || o == RecyclerView.VERTICAL;
} Prevention
- Never call package-private factories directly; go through the public API.
- Keep test code out of library packages or restrict parameterized values to valid constants.
- Validate before any direct helper construction in forks.
When it happens
Trigger: Calling createOrientationHelper from code inside the same compilation unit (it is package-private, so mainly library internals, forks, or code placed in the same package), or a fork/version mismatch where setOrientation's guard was bypassed (e.g. via reflection or a modified binary).
Common situations: Maintaining a fork of Material Components that constructs orientation helpers directly. Test code in the com.google.android.material.carousel package calling the factory with parameterized values including invalid ones. Message lacks the offending value (unlike error 30), so identify the caller from the stack trace.
Related errors
- invalid orientation:
- All children of a RecyclerView using CarouselLayoutManager m
- Keylines being linearly interpolated must have the same item
- Keylines being linearly interpolated must have the same numb
- Anchor keylines cannot be focal.
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/da554454958a89b0.
Report an issue: GitHub.