Blankj/AndroidUtilCode · error · IllegalArgumentException
Invalid orientation. It should be either HORIZONTAL or VERTI
Error message
Invalid orientation. It should be either HORIZONTAL or VERTICAL
What it means
RecycleViewDivider.setOrientation validates that orientation is exactly HORIZONTAL or VERTICAL (the class's own constants), throwing IllegalArgumentException otherwise. ItemDecoration divider drawing is axis-specific (drawVertical/drawHorizontal), so an unknown orientation would silently draw nothing or misdraw. The guard converts that silent failure into a loud contract violation at construction.
Source
Thrown at lib/utildebug/src/main/java/com/blankj/utildebug/base/rv/RecycleViewDivider.java:57
}
public RecycleViewDivider(Context context, int orientation, @NonNull Drawable divider) {
this(context, orientation, divider, false);
}
public RecycleViewDivider(Context context, int orientation, @DrawableRes int resId, boolean showFooterDivider) {
this(context, orientation, ContextCompat.getDrawable(context, resId), showFooterDivider);
}
public RecycleViewDivider(Context context, int orientation, @NonNull Drawable divider, boolean showFooterDivider) {
setOrientation(orientation);
mDivider = divider;
mShowFooterDivider = showFooterDivider;
}
private void setOrientation(int orientation) {
if (orientation != HORIZONTAL && orientation != VERTICAL) {
throw new IllegalArgumentException(
"Invalid orientation. It should be either HORIZONTAL or VERTICAL");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (parent.getLayoutManager() == null) {
return;
}
if (mOrientation == VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
@SuppressLint("NewApi")View on GitHub (pinned to 7b4caf9e54)
Solutions
- Pass one of RecycleViewDivider.HORIZONTAL or RecycleViewDivider.VERTICAL explicitly.
- If the orientation comes from a LayoutManager, map it to the divider's own constants before constructing.
- Assert at the call site that orientation is one of the two allowed values.
- Verify the constant values align (or keep them documented) so callers do not pass equivalent-but-wrong integers.
Example fix
// before new RecycleViewDivider(ctx, 2, R.drawable.divider, true); // throws // after new RecycleViewDivider(ctx, RecycleViewDivider.VERTICAL, R.drawable.divider, true);
Defensive patterns
Strategy: validation
Validate before calling
if (orientation != RecycleViewDivider.HORIZONTAL && orientation != RecycleViewDivider.VERTICAL) {
throw new IllegalArgumentException("orientation must be HORIZONTAL or VERTICAL");
}
new RecycleViewDivider(ctx, orientation, resId, showFooter); Try / catch
try {
new RecycleViewDivider(ctx, orientation, resId, showFooter);
} catch (IllegalArgumentException e) {
orientation = (orientation != RecycleViewDivider.HORIZONTAL)
? RecycleViewDivider.VERTICAL : RecycleViewDivider.HORIZONTAL;
new RecycleViewDivider(ctx, orientation, resId, showFooter);
} Prevention
- Always pass RecycleViewDivider.HORIZONTAL/VERTICAL constants.
- Map LayoutManager orientation to the divider's own constants before constructing.
- Add a constant-value check if integrating with third-party orientation enums.
- Unit-test both orientations in the divider.
When it happens
Trigger: Constructing new RecycleViewDivider(context, orientation, resId, showFooter) where orientation is neither HORIZONTAL nor VERTICAL — e.g. passing LinearLayoutManager.HORIZONTAL but from a different constant set, or a raw int like 2/3, or a layout manager's default.
Common situations: Passing RecyclerView.HORIZONTAL/VERTICAL instead of RecycleViewDivider's own constants when their values differ; passing a computed orientation that can be a third value; refactoring the constant values so callers pass stale numbers; copy-paste from a different divider API.
Related errors
- Invalid orientation. It should be either HORIZONTAL or VERTI
- invalid shadow size
- onCreateViewHolder: get holder from view type failed.
- onCreateViewHolder: get holder from view type failed.
- MediaType is not correct: "{}"
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/c14735d644f43d81.
Report an issue: GitHub.