material-components/material-components-android · error · IllegalArgumentException
invalid orientation:
Error message
invalid orientation:
What it means
CarouselLayoutManager.setOrientation (CarouselLayoutManager.java:1631) only accepts RecyclerView.HORIZONTAL (0) or RecyclerView.VERTICAL (1); any other int throws IllegalArgumentException with the offending value in the message. This mirrors LinearLayoutManager's long-standing validation, but note that unlike some RecyclerView components there is no 'defValue' fallback — the invalid value fails immediately.
Source
Thrown at lib/java/com/google/android/material/carousel/CarouselLayoutManager.java:1631
/**
* Returns the current orientation of the layout.
*
* @return Current orientation, either {@link #HORIZONTAL} or {@link #VERTICAL}
* @see #setOrientation(int)
*/
@RecyclerView.Orientation
public int getOrientation() {
return orientationHelper.orientation;
}
/**
* Sets the orientation of the layout.
*
* @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
*/
public void setOrientation(@RecyclerView.Orientation int orientation) {
if (orientation != HORIZONTAL && orientation != VERTICAL) {
throw new IllegalArgumentException("invalid orientation:" + orientation);
}
assertNotInLayoutOrScroll(null);
if (orientationHelper == null || orientation != orientationHelper.orientation) {
orientationHelper = CarouselOrientationHelper.createOrientationHelper(this, orientation);
refreshKeylineState();
}
}
@Override
public void onItemsAdded(@NonNull RecyclerView recyclerView, int positionStart, int itemCount) {
super.onItemsAdded(recyclerView, positionStart, itemCount);
updateItemCount();
}
@Override
public void onItemsRemoved(@NonNull RecyclerView recyclerView, int positionStart, int itemCount) {View on GitHub (pinned to ac7e18efee)
Solutions
- Pass only RecyclerView.HORIZONTAL or RecyclerView.VERTICAL constants.
- Sanitize external values before use: map anything unexpected to a default orientation.
- If the value comes from saved state, validate before restoring it into the layout manager.
Example fix
// before
layoutManager.setOrientation(intent.getIntExtra("orientation", 2));
// after
int raw = intent.getIntExtra("orientation", RecyclerView.HORIZONTAL);
int orientation = (raw == RecyclerView.VERTICAL) ? RecyclerView.VERTICAL : RecyclerView.HORIZONTAL;
layoutManager.setOrientation(orientation); Defensive patterns
Strategy: validation
Validate before calling
int safe = (orientation == RecyclerView.VERTICAL) ? RecyclerView.VERTICAL : RecyclerView.HORIZONTAL; layoutManager.setOrientation(safe);
Type guard
static boolean isValidOrientation(int o) {
return o == RecyclerView.HORIZONTAL || o == RecyclerView.VERTICAL;
} Prevention
- Only pass RecyclerView orientation constants, never raw ints.
- Validate values from remote config/intents/saved state before use.
- Unit-test any orientation-mapping code with out-of-range inputs.
When it happens
Trigger: Calling setOrientation with a hardcoded number other than 0/1, or reading an orientation from an external source (config, JSON, intent extra, saved state) and passing it through unvalidated. The annotated @RecyclerView.Orientation parameter is compile-time only — Kotlin or reflection call sites bypass it.
Common situations: Parsing orientation from a remote-config flag or deep link where the value is out of range (e.g. 2, -1). Refactoring constants and accidentally passing LinearLayoutManager.HORIZONTAL-like unrelated ints. Crash message 'invalid orientation: 2' makes this one easy to diagnose.
Related errors
- All children of a RecyclerView using CarouselLayoutManager m
- invalid orientation
- 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/daa8f13c82838646.
Report an issue: GitHub.