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 throws IllegalArgumentException unless the orientation is exactly HORIZONTAL (LinearLayout.HORIZONTAL==0) or VERTICAL (LinearLayout.VERTICAL==1). The divider must know its drawing axis, so any other int is rejected at construction.
Source
Thrown at lib/base/src/main/java/com/blankj/base/rv/RecycleViewDivider.java:56
}
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 the RecycleViewDivider.HORIZONTAL or RecycleViewDivider.VERTICAL constants explicitly.
- If the orientation comes from a variable, validate it is 0 or 1 (or one of the two constants) before constructing the divider.
- Use the matching constant names (RecycleViewDivider.HORIZONTAL/VERTICAL) to avoid confusing them with other libraries' constants.
Example fix
// before
new RecycleViewDivider(ctx, layoutManager.getOrientation(), R.drawable.div);
// if getOrientation returns an unexpected value -> throws
// after
int o = layoutManager.getOrientation();
new RecycleViewDivider(ctx,
o == RecyclerView.VERTICAL ? RecycleViewDivider.VERTICAL : RecycleViewDivider.HORIZONTAL,
R.drawable.div); Defensive patterns
Strategy: validation
Validate before calling
int o = (orientation == LinearLayout.VERTICAL)
? RecycleViewDivider.VERTICAL
: RecycleViewDivider.HORIZONTAL;
new RecycleViewDivider(context, o, divider); Type guard
private static boolean isValidDividerOrientation(int o) {
return o == RecycleViewDivider.HORIZONTAL || o == RecycleViewDivider.VERTICAL;
} Prevention
- Pass RecycleViewDivider.HORIZONTAL/VERTICAL explicitly, not raw ints.
- Validate any externally sourced orientation to {0,1} before constructing the divider.
- Avoid confusing RecyclerView constants with the divider's constants.
When it happens
Trigger: Passing an arbitrary int, a wrong constant, or a recycled/initialised-to-default (-1 or 0 interpreted wrong) value as the orientation argument to any RecycleViewDivider constructor.
Common situations: Passing RecyclerView.HORIZONTAL/VERTICAL by mistake instead of RecycleViewDivider.HORIZONTAL/VERTICAL (values happen to match but reads wrong); passing a LayoutManager orientation constant stored in a variable that defaulted to 0/other; passing a configuration value read from resources as a raw int.
Related errors
- Invalid orientation. It should be either HORIZONTAL or VERTI
- onCreateViewHolder: get holder from view type failed.
- MediaType is not correct: "{}"
- Index: {}, array1 Length: 0
- invalid shadow size
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/b9fdfefcdcdcdba8.
Report an issue: GitHub.