DrKLO/Telegram · error · IllegalArgumentException
invalid orientation:{}
Error message
invalid orientation:{} What it means
LinearLayoutManager.setOrientation() only accepts HORIZONTAL (0) or VERTICAL (1). Any other int is rejected before rebuilding the OrientationHelper, because the layout pass has no defined axis for an unknown orientation.
Source
Thrown at TMessagesProj/src/main/java/androidx/recyclerview/widget/LinearLayoutManager.java:342
* 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 mOrientation;
}
/**
* Sets the orientation of the layout. {@link LinearLayoutManager}
* will do its best to keep scroll position.
*
* @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 (orientation != mOrientation || mOrientationHelper == null) {
mOrientationHelper =
OrientationHelper.createOrientationHelper(this, orientation);
mAnchorInfo.mOrientationHelper = mOrientationHelper;
mOrientation = orientation;
requestLayout();
}
}
/**
* Calculates the view layout order. (e.g. from end to start or start to end)
* RTL layout support is applied automatically. So if layout is RTL and
* {@link #getReverseLayout()} is {@code true}, elements will be laid out starting from left.
*/View on GitHub (pinned to 45ab8f4308)
Solutions
- Pass LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL explicitly.
- If mapping from Configuration.ORIENTATION_*, translate: Configuration.ORIENTATION_PORTRAIT -> VERTICAL is NOT automatic; choose your own mapping and only emit 0 or 1.
- Validate the value before calling: assert orientation == 0 || orientation == 1.
Example fix
// before (Configuration.ORIENTATION_LANDSCAPE == 2)
layoutManager.setOrientation(getResources().getConfiguration().orientation);
// after
int cfg = getResources().getConfiguration().orientation;
layoutManager.setOrientation(
cfg == Configuration.ORIENTATION_LANDSCAPE ? LinearLayoutManager.HORIZONTAL : LinearLayoutManager.VERTICAL); Defensive patterns
Strategy: validation
Validate before calling
static void setSafeOrientation(LinearLayoutManager lm, int orientation) {
if (orientation != LinearLayoutManager.HORIZONTAL
&& orientation != LinearLayoutManager.VERTICAL) {
throw new IllegalArgumentException("orientation must be 0 or 1, got " + orientation);
}
lm.setOrientation(orientation);
} Type guard
static boolean isValidOrientation(int o) {
return o == LinearLayoutManager.HORIZONTAL || o == LinearLayoutManager.VERTICAL;
} Try / catch
null
Prevention
- Never feed Configuration.ORIENTATION_* directly into setOrientation; map explicitly.
- Define orientation constants in one place and reference them everywhere.
- Add a unit test asserting setOrientation rejects out-of-range values.
When it happens
Trigger: Calling layoutManager.setOrientation(2), setOrientation(-1), or passing a raw int constant from another source (e.g. Configuration.ORIENTATION_LANDSCAPE = 2) instead of LinearLayoutManager.VERTICAL/HORIZONTAL.
Common situations: Confusing Configuration.ORIENTATION_* (which are 1/2) with RecyclerView.HORIZONTAL(0)/VERTICAL(1). Passing a resource integer or an enum ordinal that does not map to 0/1. Data-binding an orientation attribute whose backing value is out of range.
Related errors
- invalid orientation
- view is not a child, cannot hide {}
- Invalid orientation. It should be either HORIZONTAL or VERTI
- Layout positions must be non-negative
- Pixel distance must be non-negative
AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14).
Data as JSON: /api/errors/2e1c80607939d5e9.
Report an issue: GitHub.